view.test.php 4.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php

class ViewTest extends PHPUnit_Framework_TestCase {

	/**
	 * Tear down the testing environment.
	 */
	public function tearDown()
	{
		View::$shared = array();
		unset(Event::$events['composing: test.basic']);
	}

	/**
	 * Test the View::make method.
	 *
	 * @group laravel
	 */
	public function testMakeMethodReturnsAViewInstance()
	{
		$this->assertInstanceOf('Laravel\\View', View::make('home.index'));
	}

	/**
	 * Test the View class constructor.
	 *
	 * @group laravel
	 */
	public function testViewNameIsSetByConstrutor()
	{
		$view = new View('home.index');

		$this->assertEquals('home.index', $view->view);
	}

	/**
	 * Test the View class constructor.
	 *
	 * @group laravel
	 */
	public function testViewIsCreatedWithCorrectPath()
	{
		$view = new View('home.index');
		
		$this->assertEquals(
			str_replace(DS, '/', path('app')).'views/home/index.php',
			str_replace(DS, '/', $view->path)
		);
	}

	/**
	 * Test the View class constructor for bundles.
	 *
	 * @group laravel
	 */
	public function testBundleViewIsCreatedWithCorrectPath()
	{
58
		$view = new View('home.index');
59 60

		$this->assertEquals(
61
			str_replace(DS, '/', Bundle::path(DEFAULT_BUNDLE)).'views/home/index.php',
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
			str_replace(DS, '/', $view->path)
		);
	}

	/**
	 * Test the View class constructor.
	 *
	 * @group laravel
	 */
	public function testDataIsSetOnViewByConstructor()
	{
		$view = new View('home.index', array('name' => 'Taylor'));

		$this->assertEquals('Taylor', $view->data['name']);
	}

	/**
	 * Test the View::name method.
	 *
	 * @group laravel
	 */
	public function testNameMethodRegistersAViewName()
	{
		View::name('home.index', 'home');

		$this->assertEquals('home.index', View::$names['home']);
	}

	/**
	 * Test the View::shared method.
	 *
	 * @group laravel
	 */
	public function testSharedMethodAddsDataToSharedArray()
	{
		View::share('comment', 'Taylor');

		$this->assertEquals('Taylor', View::$shared['comment']);
	}

	/**
	 * Test the View::with method.
	 *
	 * @group laravel
	 */
	public function testViewDataCanBeSetUsingWithMethod()
	{
		$view = View::make('home.index')->with('comment', 'Taylor');

		$this->assertEquals('Taylor', $view->data['comment']);
	}

	/**
	 * Test the View class constructor.
	 *
	 * @group laravel
	 */
	public function testEmptyMessageContainerSetOnViewWhenNoErrorsInSession()
	{
		$view = new View('home.index');

		$this->assertInstanceOf('Laravel\\Messages', $view->data['errors']);
	}

	/**
	 * Test the View __set method.
	 *
	 * @group laravel
	 */
	public function testDataCanBeSetOnViewsThroughMagicMethods()
	{
		$view = new View('home.index');

		$view->comment = 'Taylor';

		$this->assertEquals('Taylor', $view->data['comment']);
	}

	/**
	 * Test the View __get method.
	 *
	 * @group laravel
	 */
	public function testDataCanBeRetrievedFromViewsThroughMagicMethods()
	{
		$view = new View('home.index');

		$view->comment = 'Taylor';

		$this->assertEquals('Taylor', $view->comment);
	}

	/**
	 * Test the View's ArrayAccess implementation.
	 *
	 * @group laravel
	 */
	public function testDataCanBeSetOnTheViewThroughArrayAccess()
	{
		$view = new View('home.index');

		$view['comment'] = 'Taylor';

		$this->assertEquals('Taylor', $view->data['comment']);
	}

	/**
	 * Test the View's ArrayAccess implementation.
	 *
	 * @group laravel
	 */
	public function testDataCanBeRetrievedThroughArrayAccess()
	{
		$view = new View('home.index');

		$view['comment'] = 'Taylor';

		$this->assertEquals('Taylor', $view['comment']);
	}

	/**
	 * Test the View::nest method.
	 *
	 * @group laravel
	 */
	public function testNestMethodSetsViewInstanceInData()
	{
		$view = View::make('home.index')->nest('partial', 'tests.basic');

		$this->assertEquals('tests.basic', $view->data['partial']->view);

		$this->assertInstanceOf('Laravel\\View', $view->data['partial']);
	}

	/**
	 * Test that the registered data is passed to the view correctly.
	 *
	 * @group laravel
	 */
	public function testDataIsPassedToViewCorrectly()
	{
		View::share('name', 'Taylor');

		$view = View::make('tests.basic')->with('age', 25)->render();

		$this->assertEquals('Taylor is 25', $view);
	}

	/**
	 * Test that the View class renders nested views.
	 *
	 * @group laravel
	 */
	public function testNestedViewsAreRendered()
	{
		$view = View::make('tests.basic')
								->with('age', 25)
								->nest('name', 'tests.nested');

		$this->assertEquals('Taylor is 25', $view->render());
	}

	/**
	 * Test that the View class renders nested responses.
	 *
	 * @group laravel
	 */
	public function testNestedResponsesAreRendered()
	{
		$view = View::make('tests.basic')
								->with('age', 25)
								->with('name', Response::view('tests.nested'));

		$this->assertEquals('Taylor is 25', $view->render());
	}

	/**
	 * Test the View class raises a composer event.
	 *
	 * @group laravel
	 */
	public function testComposerEventIsCalledWhenViewIsRendering()
	{
		View::composer('tests.basic', function($view)
		{
			$view->data = array('name' => 'Taylor', 'age' => 25);
		});

		$view = View::make('tests.basic')->render();

		$this->assertEquals('Taylor is 25', $view);
	}

}