controller.test.php 6.13 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 58 59 60 61 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
<?php

class ControllerTest extends PHPUnit_Framework_TestCase {

	/**
	 * Setup the testing environment.
	 */
	public function setUp()
	{
		$_SERVER['REQUEST_METHOD'] = 'GET';
	}

	/**
	 * Tear down the testing environment.
	 */
	public function tearDown()
	{
		unset(Filter::$filters['test-all-before']);
		unset(Filter::$filters['test-all-after']);
		unset(Filter::$filters['test-profile-before']);
		unset($_SERVER['REQUEST_METHOD']);
	}

	/**
	 * Test the Controller::call method.
	 *
	 * @group laravel
	 */
	public function testBasicControllerActionCanBeCalled()
	{
		$this->assertEquals('action_index', Controller::call('auth@index')->content);
		$this->assertEquals('Admin_Panel_Index', Controller::call('admin.panel@index')->content);
		$this->assertEquals('Taylor', Controller::call('auth@profile', array('Taylor'))->content);
		$this->assertEquals('Dashboard_Panel_Index', Controller::call('dashboard::panel@index')->content);
	}

	/**
	 * Test basic controller filters are called.
	 *
	 * @group laravel
	 */
	public function testAssignedBeforeFiltersAreRun()
	{
		$_SERVER['test-all-after'] = false;
		$_SERVER['test-all-before'] = false;

		Controller::call('filter@index');

		$this->assertTrue($_SERVER['test-all-after']);
		$this->assertTrue($_SERVER['test-all-before']);
	}

	/**
	 * Test that "only" filters only apply to their assigned methods.
	 *
	 * @group laravel
	 */
	public function testOnlyFiltersOnlyApplyToTheirAssignedMethods()
	{
		$_SERVER['test-profile-before'] = false;

		Controller::call('filter@index');

		$this->assertFalse($_SERVER['test-profile-before']);

		Controller::call('filter@profile');

		$this->assertTrue($_SERVER['test-profile-before']);
	}

	/**
	 * Test that "except" filters only apply to the excluded methods.
	 *
	 * @group laravel
	 */
	public function testExceptFiltersOnlyApplyToTheExlucdedMethods()
	{
		$_SERVER['test-except'] = false;

		Controller::call('filter@index');
		Controller::call('filter@profile');

		$this->assertFalse($_SERVER['test-except']);

		Controller::call('filter@show');

		$this->assertTrue($_SERVER['test-except']);
	}

	/**
	 * Test that filters can be constrained by the request method.
	 *
	 * @group laravel
	 */
	public function testFiltersCanBeConstrainedByRequestMethod()
	{
		$_SERVER['test-on-post'] = false;

99
		Request::$foundation->setMethod('GET');
100 101 102 103
		Controller::call('filter@index');

		$this->assertFalse($_SERVER['test-on-post']);

104
		Request::$foundation->setMethod('POST');
105 106 107 108 109 110
		Controller::call('filter@index');

		$this->assertTrue($_SERVER['test-on-post']);

		$_SERVER['test-on-get-put'] = false;

111
		Request::$foundation->setMethod('POST');
112 113 114 115
		Controller::call('filter@index');

		$this->assertFalse($_SERVER['test-on-get-put']);

116
		Request::$foundation->setMethod('PUT');
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
		Controller::call('filter@index');

		$this->assertTrue($_SERVER['test-on-get-put']);
	}

	public function testGlobalBeforeFilterIsNotCalledByController()
	{
		$_SERVER['before'] = false;
		$_SERVER['after'] = false;

		Controller::call('auth@index');

		$this->assertFalse($_SERVER['before']);
		$this->assertFalse($_SERVER['after']);
	}

	/**
	 * Test that before filters can override the controller response.
	 *
	 * @group laravel
	 */
	public function testBeforeFiltersCanOverrideResponses()
	{
		$this->assertEquals('Filtered!', Controller::call('filter@login')->content);
	}

	/**
	 * Test that after filters do not affect the response.
	 *
	 * @group laravel
	 */
	public function testAfterFiltersDoNotAffectControllerResponse()
	{
		$this->assertEquals('action_logout', Controller::call('filter@logout')->content);
	}

	/**
	 * Test that filter parameters are passed to the filter.
	 *
	 * @group laravel
	 */
	public function testFilterParametersArePassedToTheFilter()
	{
		$this->assertEquals('12', Controller::call('filter@edit')->content);
	}

	/**
	 * Test that multiple filters can be assigned to a single method.
	 *
	 * @group laravel
	 */
	public function testMultipleFiltersCanBeAssignedToAnAction()
	{
		$_SERVER['test-multi-1'] = false;
		$_SERVER['test-multi-2'] = false;

		Controller::call('filter@save');

		$this->assertTrue($_SERVER['test-multi-1']);
		$this->assertTrue($_SERVER['test-multi-2']);
	}

	/**
	 * Test Restful controllers respond by request method.
	 *
	 * @group laravel
	 */
	public function testRestfulControllersRespondWithRestfulMethods()
	{
186 187
		Request::$foundation->setMethod('GET');
		//$_SERVER['REQUEST_METHOD'] = 'GET';
188 189 190

		$this->assertEquals('get_index', Controller::call('restful@index')->content);

191 192
		//$_SERVER['REQUEST_METHOD'] = 'PUT';
		Request::$foundation->setMethod('PUT');
193

194
		$this->assertEquals(404, Controller::call('restful@index')->status());
195

196 197
		//$_SERVER['REQUEST_METHOD'] = 'POST';
		Request::$foundation->setMethod('POST');
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 256 257 258 259 260 261 262 263 264 265 266 267

		$this->assertEquals('post_index', Controller::call('restful@index')->content);
	}

	/**
	 * Test that the template is returned by template controllers.
	 *
	 * @group laravel
	 */
	public function testTemplateControllersReturnTheTemplate()
	{
		$response = Controller::call('template.basic@index');

		$home = file_get_contents(path('app').'views/home/index.php');

		$this->assertEquals($home, $response->content);
	}

	/**
	 * Test that controller templates can be named views.
	 *
	 * @group laravel
	 */
	public function testControllerTemplatesCanBeNamedViews()
	{
		View::name('home.index', 'home');

		$response = Controller::call('template.named@index');

		$home = file_get_contents(path('app').'views/home/index.php');

		$this->assertEquals($home, $response->content);

		View::$names = array();
	}

	/**
	 * Test that the "layout" method is called on the controller.
	 *
	 * @group laravel
	 */
	public function testTheTemplateCanBeOverriden()
	{
		$this->assertEquals('Layout', Controller::call('template.override@index')->content);
	}

	/**
	 * Test the Controller::resolve method.
	 *
	 * @group laravel
	 */
	public function testResolveMethodChecksTheIoCContainer()
	{
		IoC::register('controller: home', function()
		{
			require_once path('app').'controllers/home.php';

			$controller = new Home_Controller;

			$controller->foo = 'bar';

			return $controller;
		});

		$controller = Controller::resolve(DEFAULT_BUNDLE, 'home');

		$this->assertEquals('bar', $controller->foo);
	}

}