| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- /**
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Routing;
- use Cake\Controller\Controller;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Event\Event;
- use Cake\Network\Request;
- use Cake\Network\Response;
- use Cake\Network\Session;
- use Cake\Routing\Dispatcher;
- use Cake\Routing\Filter\ControllerFactoryFilter;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Inflector;
- /**
- * A testing stub that doesn't send headers.
- */
- class DispatcherMockResponse extends Response
- {
- protected function _sendHeader($name, $value = null)
- {
- return $name . ' ' . $value;
- }
- }
- /**
- * TestDispatcher class
- */
- class TestDispatcher extends Dispatcher
- {
- /**
- * Controller instance, made publicly available for testing
- *
- * @var Controller
- */
- public $controller;
- /**
- * invoke method
- *
- * @param \Cake\Controller\Controller $controller
- * @return \Cake\Network\Response $response
- */
- protected function _invoke(Controller $controller)
- {
- $this->controller = $controller;
- return parent::_invoke($controller);
- }
- }
- /**
- * MyPluginAppController class
- *
- */
- class MyPluginAppController extends Controller
- {
- }
- interface DispatcherTestInterfaceController
- {
- public function index();
- }
- /**
- * MyPluginController class
- *
- */
- class MyPluginController extends MyPluginAppController
- {
- /**
- * name property
- *
- * @var string
- */
- public $name = 'MyPlugin';
- /**
- * index method
- *
- * @return void
- */
- public function index()
- {
- return true;
- }
- /**
- * add method
- *
- * @return void
- */
- public function add()
- {
- return true;
- }
- /**
- * admin_add method
- *
- * @param mixed $id
- * @return void
- */
- public function admin_add($id = null)
- {
- return $id;
- }
- }
- /**
- * OtherPagesController class
- *
- */
- class OtherPagesController extends MyPluginAppController
- {
- /**
- * name property
- *
- * @var string
- */
- public $name = 'OtherPages';
- /**
- * display method
- *
- * @param string $page
- * @return void
- */
- public function display($page = null)
- {
- return $page;
- }
- /**
- * index method
- *
- * @return void
- */
- public function index()
- {
- return true;
- }
- }
- /**
- * ArticlesTestAppController class
- *
- */
- class ArticlesTestAppController extends Controller
- {
- }
- /**
- * ArticlesTestController class
- *
- */
- class ArticlesTestController extends ArticlesTestAppController
- {
- /**
- * name property
- *
- * @var string
- */
- public $name = 'ArticlesTest';
- /**
- * admin_index method
- *
- * @return void
- */
- public function admin_index()
- {
- return true;
- }
- /**
- * fake index method.
- *
- * @return void
- */
- public function index()
- {
- return true;
- }
- }
- /**
- * DispatcherTest class
- *
- */
- class DispatcherTest extends TestCase
- {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $_GET = [];
- Configure::write('App.base', false);
- Configure::write('App.baseUrl', false);
- Configure::write('App.dir', 'app');
- Configure::write('App.webroot', 'webroot');
- Configure::write('App.namespace', 'TestApp');
- $this->dispatcher = new TestDispatcher();
- $this->dispatcher->addFilter(new ControllerFactoryFilter());
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- Plugin::unload();
- }
- /**
- * testMissingController method
- *
- * @expectedException \Cake\Routing\Exception\MissingControllerException
- * @expectedExceptionMessage Controller class SomeController could not be found.
- * @return void
- */
- public function testMissingController()
- {
- $request = new Request([
- 'url' => 'some_controller/home',
- 'params' => [
- 'controller' => 'SomeController',
- 'action' => 'home',
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response');
- $this->dispatcher->dispatch($request, $response, ['return' => 1]);
- }
- /**
- * testMissingControllerInterface method
- *
- * @expectedException \Cake\Routing\Exception\MissingControllerException
- * @expectedExceptionMessage Controller class DispatcherTestInterface could not be found.
- * @return void
- */
- public function testMissingControllerInterface()
- {
- $request = new Request([
- 'url' => 'dispatcher_test_interface/index',
- 'params' => [
- 'controller' => 'DispatcherTestInterface',
- 'action' => 'index',
- ]
- ]);
- $url = new Request('dispatcher_test_interface/index');
- $response = $this->getMock('Cake\Network\Response');
- $this->dispatcher->dispatch($request, $response, ['return' => 1]);
- }
- /**
- * testMissingControllerInterface method
- *
- * @expectedException \Cake\Routing\Exception\MissingControllerException
- * @expectedExceptionMessage Controller class Abstract could not be found.
- * @return void
- */
- public function testMissingControllerAbstract()
- {
- $request = new Request([
- 'url' => 'abstract/index',
- 'params' => [
- 'controller' => 'Abstract',
- 'action' => 'index',
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response');
- $this->dispatcher->dispatch($request, $response, ['return' => 1]);
- }
- /**
- * testDispatch method
- *
- * @return void
- */
- public function testDispatchBasic()
- {
- $url = new Request([
- 'url' => 'pages/home',
- 'params' => [
- 'controller' => 'Pages',
- 'action' => 'display',
- 'pass' => ['extract'],
- 'return' => 1
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response');
- $this->dispatcher->dispatch($url, $response);
- $expected = 'Pages';
- $this->assertEquals($expected, $this->dispatcher->controller->name);
- }
- /**
- * Test that Dispatcher handles actions that return response objects.
- *
- * @return void
- */
- public function testDispatchActionReturnsResponse()
- {
- $request = new Request([
- 'url' => 'some_pages/responseGenerator',
- 'params' => [
- 'controller' => 'SomePages',
- 'action' => 'responseGenerator',
- 'pass' => []
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
- ob_start();
- $this->dispatcher->dispatch($request, $response);
- $result = ob_get_clean();
- $this->assertEquals('new response', $result);
- }
- /**
- * testPrefixDispatch method
- *
- * @return void
- */
- public function testPrefixDispatch()
- {
- $request = new Request([
- 'url' => 'admin/posts/index',
- 'params' => [
- 'prefix' => 'Admin',
- 'controller' => 'Posts',
- 'action' => 'index',
- 'pass' => [],
- 'return' => 1
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response');
- $this->dispatcher->dispatch($request, $response);
- $this->assertInstanceOf(
- 'TestApp\Controller\Admin\PostsController',
- $this->dispatcher->controller
- );
- $expected = '/admin/posts/index';
- $this->assertSame($expected, $request->here);
- }
- /**
- * test prefix dispatching in a plugin.
- *
- * @return void
- */
- public function testPrefixDispatchPlugin()
- {
- Plugin::load('TestPlugin');
- $request = new Request([
- 'url' => 'admin/test_plugin/comments/index',
- 'params' => [
- 'plugin' => 'TestPlugin',
- 'prefix' => 'Admin',
- 'controller' => 'Comments',
- 'action' => 'index',
- 'pass' => [],
- 'return' => 1
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response');
- $this->dispatcher->dispatch($request, $response);
- $this->assertInstanceOf(
- 'TestPlugin\Controller\Admin\CommentsController',
- $this->dispatcher->controller
- );
- }
- /**
- * Test dispatcher filters being called.
- *
- * @return void
- */
- public function testDispatcherFilter()
- {
- $filter = $this->getMock(
- 'Cake\Routing\DispatcherFilter',
- ['beforeDispatch', 'afterDispatch']
- );
- $filter->expects($this->at(0))
- ->method('beforeDispatch');
- $filter->expects($this->at(1))
- ->method('afterDispatch');
- $this->dispatcher->addFilter($filter);
- $request = new Request([
- 'url' => '/',
- 'params' => [
- 'controller' => 'pages',
- 'action' => 'display',
- 'home',
- 'pass' => []
- ]
- ]);
- $response = $this->getMock('Cake\Network\Response', ['send']);
- $this->dispatcher->dispatch($request, $response);
- }
- /**
- * Test dispatcher filters being called and changing the response.
- *
- * @return void
- */
- public function testBeforeDispatchAbortDispatch()
- {
- $response = $this->getMock('Cake\Network\Response', ['send']);
- $response->expects($this->once())
- ->method('send');
- $filter = $this->getMock(
- 'Cake\Routing\DispatcherFilter',
- ['beforeDispatch', 'afterDispatch']
- );
- $filter->expects($this->once())
- ->method('beforeDispatch')
- ->will($this->returnValue($response));
- $filter->expects($this->never())
- ->method('afterDispatch');
- $request = new Request();
- $res = new Response();
- $this->dispatcher->addFilter($filter);
- $this->dispatcher->dispatch($request, $res);
- }
- /**
- * Test dispatcher filters being called and changing the response.
- *
- * @return void
- */
- public function testAfterDispatchReplaceResponse()
- {
- $response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'send']);
- $response->expects($this->once())
- ->method('send');
- $filter = $this->getMock(
- 'Cake\Routing\DispatcherFilter',
- ['beforeDispatch', 'afterDispatch']
- );
- $filter->expects($this->once())
- ->method('afterDispatch')
- ->will($this->returnValue($response));
- $request = new Request([
- 'url' => '/posts',
- 'params' => [
- 'plugin' => null,
- 'controller' => 'Posts',
- 'action' => 'index',
- 'pass' => [],
- ],
- 'session' => new Session
- ]);
- $this->dispatcher->addFilter($filter);
- $this->dispatcher->dispatch($request, $response);
- }
- }
|