| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Routing;
- use Cake\Routing\Route\Route;
- use Cake\Routing\Router;
- use Cake\Routing\RouteBuilder;
- use Cake\Routing\RouteCollection;
- use Cake\TestSuite\TestCase;
- class RouteCollectionTest extends TestCase {
- /**
- * Setup method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->collection = new RouteCollection();
- }
- /**
- * Test parse() throws an error on unknown routes.
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @expectedExceptionMessage A route matching "/" could not be found
- */
- public function testParseMissingRoute() {
- $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
- $routes->connect('/', ['controller' => 'Articles']);
- $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
- $result = $this->collection->parse('/');
- $this->assertEquals([], $result, 'Should not match, missing /b');
- }
- /**
- * Test parsing routes.
- *
- * @return void
- */
- public function testParse() {
- $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
- $routes->connect('/', ['controller' => 'Articles']);
- $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
- $result = $this->collection->parse('/b/');
- $expected = [
- 'controller' => 'Articles',
- 'action' => 'index',
- 'pass' => [],
- 'plugin' => null,
- 'key' => 'value',
- ];
- $this->assertEquals($expected, $result);
- $result = $this->collection->parse('/b/the-thing?one=two');
- $expected = [
- 'controller' => 'Articles',
- 'action' => 'view',
- 'id' => 'the-thing',
- 'pass' => [],
- 'plugin' => null,
- 'key' => 'value',
- '?' => ['one' => 'two'],
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test match() throws an error on unknown routes.
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @expectedExceptionMessage A route matching "array (
- */
- public function testMatchError() {
- $context = [
- '_base' => '/',
- '_scheme' => 'http',
- '_host' => 'example.org',
- ];
- $routes = new RouteBuilder($this->collection, '/b');
- $routes->connect('/', ['controller' => 'Articles']);
- $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
- $this->assertFalse($result, 'No matches');
- }
- /**
- * Test matching routes.
- *
- * @return void
- */
- public function testMatch() {
- $context = [
- '_base' => '/',
- '_scheme' => 'http',
- '_host' => 'example.org',
- ];
- $routes = new RouteBuilder($this->collection, '/b');
- $routes->connect('/', ['controller' => 'Articles']);
- $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
- $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
- $this->assertEquals('b', $result);
- $result = $this->collection->match(
- ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
- $context);
- $this->assertEquals('b/thing', $result);
- }
- /**
- * Test matching routes with names
- *
- * @return void
- */
- public function testMatchNamed() {
- $context = [
- '_base' => '/',
- '_scheme' => 'http',
- '_host' => 'example.org',
- ];
- $routes = new RouteBuilder($this->collection, '/b');
- $routes->connect('/', ['controller' => 'Articles']);
- $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
- $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
- $this->assertEquals('/b/2', $result);
- }
- /**
- * Test matching routes with names and failing
- *
- * @expectedException Cake\Routing\Error\MissingRouteException
- * @return void
- */
- public function testMatchNamedError() {
- $context = [
- '_base' => '/',
- '_scheme' => 'http',
- '_host' => 'example.org',
- ];
- $routes = new RouteBuilder($this->collection, '/b');
- $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
- $this->collection->match(['_name' => 'derp'], $context);
- }
- /**
- * Test matching plugin routes.
- *
- * @return void
- */
- public function testMatchPlugin() {
- $context = [
- '_base' => '/',
- '_scheme' => 'http',
- '_host' => 'example.org',
- ];
- $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
- $routes->connect('/', ['controller' => 'Contacts']);
- $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
- $this->assertEquals('contacts', $result);
- }
- /**
- * Test getting named routes.
- *
- * @return void
- */
- public function testNamed() {
- $routes = new RouteBuilder($this->collection, '/l');
- $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
- $routes->connect('/:controller/:action/*');
- $all = $this->collection->named();
- $this->assertCount(1, $all);
- $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
- $this->assertEquals('/l/:controller', $all['cntrl']->template);
- }
- /**
- * Test adding with an error
- */
- public function testAdd() {
- }
- /**
- * Test the routes() method.
- *
- * @return void
- */
- public function testRoutes() {
- }
- }
|