| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Routing;
- use Cake\Http\ServerRequest;
- use Cake\Routing\DispatcherFactory;
- use Cake\TestSuite\TestCase;
- /**
- * Dispatcher factory test case.
- */
- class DispatcherFactoryTest extends TestCase
- {
- protected $errorLevel;
- /**
- * setup function
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- static::setAppNamespace();
- DispatcherFactory::clear();
- $this->errorLevel = error_reporting(E_ALL ^ E_USER_DEPRECATED);
- }
- /**
- * teardown function
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- error_reporting($this->errorLevel);
- }
- /**
- * Test add filter
- *
- * @return void
- */
- public function testAddFilter()
- {
- $mw = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
- ->setMethods(['beforeDispatch'])
- ->getMock();
- $result = DispatcherFactory::add($mw);
- $this->assertSame($mw, $result);
- }
- /**
- * Test add filter as a string
- *
- * @return void
- */
- public function testAddFilterString()
- {
- $result = DispatcherFactory::add('Routing');
- $this->assertInstanceOf('Cake\Routing\Filter\RoutingFilter', $result);
- }
- /**
- * Test add filter missing
- *
- * @return void
- */
- public function testAddFilterMissing()
- {
- $this->expectException(\Cake\Routing\Exception\MissingDispatcherFilterException::class);
- DispatcherFactory::add('NopeSauce');
- }
- /**
- * Test add filter
- *
- * @return void
- */
- public function testAddFilterWithOptions()
- {
- $config = ['config' => 'value', 'priority' => 999];
- $result = DispatcherFactory::add('Routing', $config);
- $this->assertInstanceOf('Cake\Routing\Filter\RoutingFilter', $result);
- $this->assertEquals($config['config'], $result->getConfig('config'));
- $this->assertEquals($config['priority'], $result->getConfig('priority'));
- }
- /**
- * Test creating a dispatcher with the factory
- *
- * @return void
- */
- public function testCreate()
- {
- $mw = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
- ->setMethods(['beforeDispatch'])
- ->getMock();
- DispatcherFactory::add($mw);
- $result = DispatcherFactory::create();
- $this->assertInstanceOf('Cake\Routing\Dispatcher', $result);
- $this->assertCount(1, $result->filters());
- }
- /**
- * test create() -> dispatch() -> response flow.
- *
- * @return void
- */
- public function testCreateDispatchWithFilters()
- {
- $url = new ServerRequest([
- 'url' => 'posts',
- 'params' => [
- 'controller' => 'Posts',
- 'action' => 'index',
- 'pass' => [],
- 'bare' => true,
- ],
- ]);
- $response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['send'])
- ->getMock();
- $response->expects($this->once())
- ->method('send')
- ->will($this->returnSelf());
- DispatcherFactory::add('ControllerFactory');
- DispatcherFactory::add('Append');
- $dispatcher = DispatcherFactory::create();
- $result = $dispatcher->dispatch($url, $response);
- $this->assertEquals('posts index appended content', $result->body());
- }
- }
|