PageRenderTime 375ms CodeModel.GetById 181ms app.highlight 12ms RepoModel.GetById 179ms app.codeStats 0ms

/src/Joomla/Router/Tests/RouterTest.php

https://github.com/dianaprajescu/joomla-framework
PHP | 349 lines | 265 code | 13 blank | 71 comment | 0 complexity | 3bbc617f71426926041bec2482e4bb93 MD5 | raw file
  1<?php
  2/**
  3 * @copyright  Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  4 * @license    GNU General Public License version 2 or later; see LICENSE
  5 */
  6
  7namespace Joomla\Router\Tests;
  8
  9use Joomla\Router\Router;
 10use Joomla\Test\TestHelper;
 11
 12require_once __DIR__ . '/Stubs/Bar.php';
 13require_once __DIR__ . '/Stubs/Baz.php';
 14require_once __DIR__ . '/Stubs/Foo.php';
 15require_once __DIR__ . '/Stubs/GooGet.php';
 16
 17/**
 18 * Tests for the Joomla\Router\Router class.
 19 *
 20 * @since  1.0
 21 */
 22class RouterTest extends \PHPUnit_Framework_TestCase
 23{
 24	/**
 25	 * An instance of the object to be tested.
 26	 *
 27	 * @var    Router
 28	 * @since  1.0
 29	 */
 30	private $instance;
 31
 32	/**
 33	 * Provides test data for the testParseRoute method.
 34	 *
 35	 * @return  array
 36	 *
 37	 * @since   1.0
 38	 */
 39	public static function seedTestParseRoute()
 40	{
 41		// Route, Exception, ControllerName, InputData, MapSet
 42		return array(
 43			array('', false, 'home', array(), 1),
 44			array('articles/4', true, 'home', array(), 1),
 45			array('', false, 'index', array(), 2),
 46			array('login', false, 'login', array('_rawRoute' => 'login'), 2),
 47			array('articles', false, 'articles', array('_rawRoute' => 'articles'), 2),
 48			array('articles/4', false, 'article', array('article_id' => 4, '_rawRoute' => 'articles/4'), 2),
 49			array('articles/4/crap', true, '', array(), 2),
 50			array('test', true, '', array(), 2),
 51			array('test/foo', true, '', array(), 2),
 52			array('test/foo/path', true, '', array(), 2),
 53			array('test/foo/path/bar', false, 'test', array('seg1' => 'foo', 'seg2' => 'bar', '_rawRoute' => 'test/foo/path/bar'), 2),
 54			array('content/article-1/*', false, 'content', array('_rawRoute' => 'content/article-1/*'), 2),
 55			array('content/cat-1/article-1', false,
 56				'article', array('category' => 'cat-1', 'article' => 'article-1', '_rawRoute' => 'content/cat-1/article-1'), 2),
 57			array('content/cat-1/cat-2/article-1', false,
 58				'article', array('category' => 'cat-1/cat-2', 'article' => 'article-1', '_rawRoute' => 'content/cat-1/cat-2/article-1'), 2),
 59			array('content/cat-1/cat-2/cat-3/article-1', false,
 60				'article', array('category' => 'cat-1/cat-2/cat-3', 'article' => 'article-1', '_rawRoute' => 'content/cat-1/cat-2/cat-3/article-1'), 2)
 61		);
 62	}
 63
 64	/**
 65	 * Setup the router maps to option 1.
 66	 *
 67	 * This has no routes but has a default controller for the home page.
 68	 *
 69	 * @return  void
 70	 *
 71	 * @since   1.0
 72	 */
 73	protected function setMaps1()
 74	{
 75		$this->instance->addMaps(array());
 76		$this->instance->setDefaultController('home');
 77	}
 78
 79	/**
 80	 * Setup the router maps to option 2.
 81	 *
 82	 * @return  void
 83	 *
 84	 * @since   1.0
 85	 */
 86	protected function setMaps2()
 87	{
 88		$this->instance->addMaps(
 89			array(
 90				'login' => 'login',
 91				'logout' => 'logout',
 92				'articles' => 'articles',
 93				'articles/:article_id' => 'article',
 94				'test/:seg1/path/:seg2' => 'test',
 95				'content/:/\*' => 'content',
 96				'content/*category/:article' => 'article'
 97			)
 98		);
 99		$this->instance->setDefaultController('index');
100	}
101
102	/**
103	 * Tests the Joomla\Router\Router::__construct method.
104	 *
105	 * @return  void
106	 *
107	 * @covers  Joomla\Router\Router::__construct
108	 * @since   1.0
109	 */
110	public function test__construct()
111	{
112		$this->assertAttributeInstanceOf('Joomla\\Input\\Input', 'input', $this->instance);
113	}
114
115	/**
116	 * Tests the Joomla\Router\Router::addMap method.
117	 *
118	 * @return  void
119	 *
120	 * @covers  Joomla\Router\Router::addMap
121	 * @since   1.0
122	 */
123	public function testAddMap()
124	{
125		$this->assertAttributeEmpty('maps', $this->instance);
126		$this->instance->addMap('foo', 'MyApplicationFoo');
127		$this->assertAttributeEquals(
128			array(
129				array(
130					'regex' => chr(1) . '^foo$' . chr(1),
131					'vars' => array(),
132					'controller' => 'MyApplicationFoo'
133				)
134			),
135			'maps',
136			$this->instance
137		);
138	}
139
140	/**
141	 * Tests the Joomla\Router\Router::addMaps method.
142	 *
143	 * @return  void
144	 *
145	 * @covers  Joomla\Router\Router::addMaps
146	 * @since   1.0
147	 */
148	public function testAddMaps()
149	{
150		$maps = array(
151			'login' => 'login',
152			'logout' => 'logout',
153			'requests' => 'requests',
154			'requests/:request_id' => 'request'
155		);
156
157		$rules = array(
158			array(
159				'regex' => chr(1) . '^login$' . chr(1),
160				'vars' => array(),
161				'controller' => 'login'
162			),
163			array(
164				'regex' => chr(1) . '^logout$' . chr(1),
165				'vars' => array(),
166				'controller' => 'logout'
167			),
168			array(
169				'regex' => chr(1) . '^requests$' . chr(1),
170				'vars' => array(),
171				'controller' => 'requests'
172			),
173			array(
174				'regex' => chr(1) . '^requests/([^/]*)$' . chr(1),
175				'vars' => array('request_id'),
176				'controller' => 'request'
177			)
178		);
179
180		$this->assertAttributeEmpty('maps', $this->instance);
181		$this->instance->addMaps($maps);
182		$this->assertAttributeEquals($rules, 'maps', $this->instance);
183	}
184
185	/**
186	 * Tests the Joomla\Router\Router::getController method.
187	 *
188	 * @return  void
189	 *
190	 * @covers  Joomla\Router\Router::getController
191	 * @since   1.0
192	 */
193	public function testGetController()
194	{
195		$this->instance->setControllerPrefix('\Joomla\Router\Tests\Stubs\\')
196			->addMap('articles/:article_id', 'GooGet');
197
198		$controller = $this->instance->getController('articles/3');
199		$this->assertInstanceOf('\Joomla\Router\Tests\Stubs\GooGet', $controller);
200
201		$input = $controller->getInput();
202		$this->assertEquals('3', $input->get('article_id'));
203	}
204
205	/**
206	 * Tests the Joomla\Router\Router::parseRoute method.
207	 *
208	 * @param   string   $r  The route to parse.
209	 * @param   boolean  $e  True if an exception is expected.
210	 * @param   string   $c  The expected controller name.
211	 * @param   array    $i  The expected input object data.
212	 * @param   integer  $m  The map set to use for setting up the router.
213	 *
214	 * @return  void
215	 *
216	 * @covers        Joomla\Router\Router::parseRoute
217	 * @dataProvider  seedTestParseRoute
218	 * @since         1.0
219	 */
220	public function testParseRoute($r, $e, $c, $i, $m)
221	{
222		// Setup the router maps.
223		$mapSetup = 'setMaps' . $m;
224		$this->$mapSetup();
225
226		// If we should expect an exception set that up.
227		if ($e)
228		{
229			$this->setExpectedException('InvalidArgumentException');
230		}
231
232		// Execute the route parsing.
233		$actual = TestHelper::invoke($this->instance, 'parseRoute', $r);
234
235		// Test the assertions.
236		$this->assertEquals($c, $actual, 'Incorrect controller name found.');
237	}
238
239	/**
240	 * Tests the Joomla\Router\Router::setControllerPrefix method.
241	 *
242	 * @return  void
243	 *
244	 * @covers  Joomla\Router\Router::setControllerPrefix
245	 * @since   1.0
246	 */
247	public function testSetControllerPrefix()
248	{
249		$this->instance->setControllerPrefix('MyApplication');
250		$this->assertAttributeEquals('MyApplication', 'controllerPrefix', $this->instance);
251	}
252
253	/**
254	 * Tests the Joomla\Router\Router::setDefaultController method.
255	 *
256	 * @return  void
257	 *
258	 * @covers  Joomla\Router\Router::setDefaultController
259	 * @since   1.0
260	 */
261	public function testSetDefaultController()
262	{
263		$this->instance->setDefaultController('foobar');
264		$this->assertAttributeEquals('foobar', 'default', $this->instance);
265	}
266
267	/**
268	 * Tests the Joomla\Router\Router::fetchController method if the controller class is missing.
269	 *
270	 * @return  void
271	 *
272	 * @covers  Joomla\Router\Router::fetchController
273	 * @since   1.0
274	 */
275	public function testFetchControllerWithMissingClass()
276	{
277		$this->setExpectedException('RuntimeException');
278		$controller = TestHelper::invoke($this->instance, 'fetchController', 'goober');
279	}
280
281	/**
282	 * Tests the Joomla\Router\Router::fetchController method if the class not a controller.
283	 *
284	 * @return  void
285	 *
286	 * @covers  Joomla\Router\Router::fetchController
287	 * @since   1.0
288	 */
289	public function testFetchControllerWithNonController()
290	{
291		$this->setExpectedException('RuntimeException');
292		$controller = TestHelper::invoke($this->instance, 'fetchController', 'MyTestControllerBaz');
293	}
294
295	/**
296	 * Tests the Joomla\Router\Router::fetchController method with a prefix set.
297	 *
298	 * @return  void
299	 *
300	 * @covers  Joomla\Router\Router::fetchController
301	 * @since   1.0
302	 */
303	public function testFetchControllerWithPrefixSet()
304	{
305		$this->instance->setControllerPrefix('MyTestController');
306		$controller = TestHelper::invoke($this->instance, 'fetchController', 'Foo');
307	}
308
309	/**
310	 * Tests the Joomla\Router\Router::fetchController method without a prefix set even though it is necessary.
311	 *
312	 * @return  void
313	 *
314	 * @covers  Joomla\Router\Router::fetchController
315	 * @since   1.0
316	 */
317	public function testFetchControllerWithoutPrefixSetThoughNecessary()
318	{
319		$this->setExpectedException('RuntimeException');
320		$controller = TestHelper::invoke($this->instance, 'fetchController', 'foo');
321	}
322
323	/**
324	 * Tests the Joomla\Router\Router::fetchController method without a prefix set.
325	 *
326	 * @return  void
327	 *
328	 * @covers  Joomla\Router\Router::fetchController
329	 * @since   1.0
330	 */
331	public function testFetchControllerWithoutPrefixSet()
332	{
333		$controller = TestHelper::invoke($this->instance, 'fetchController', 'TControllerBar');
334	}
335
336	/**
337	 * Prepares the environment before running a test.
338	 *
339	 * @return  void
340	 *
341	 * @since   1.0
342	 */
343	protected function setUp()
344	{
345		parent::setUp();
346
347		$this->instance = new Router;
348	}
349}