| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?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.3.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Http;
- use Cake\Http\ControllerFactory;
- use Cake\Http\ServerRequest;
- use Cake\TestSuite\TestCase;
- /**
- * Test case for ControllerFactory.
- */
- class ControllerFactoryTest extends TestCase
- {
- /**
- * Setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- static::setAppNamespace();
- $this->factory = new ControllerFactory();
- $this->response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- }
- /**
- * Test building an application controller
- *
- * @return void
- */
- public function testApplicationController()
- {
- $request = new ServerRequest([
- 'url' => 'cakes/index',
- 'params' => [
- 'controller' => 'Cakes',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->create($request, $this->response);
- $this->assertInstanceOf('TestApp\Controller\CakesController', $result);
- $this->assertSame($request, $result->request);
- $this->assertSame($this->response, $result->response);
- }
- /**
- * Test building a prefixed app controller.
- *
- * @return void
- */
- public function testPrefixedAppController()
- {
- $request = new ServerRequest([
- 'url' => 'admin/posts/index',
- 'params' => [
- 'prefix' => 'admin',
- 'controller' => 'Posts',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->create($request, $this->response);
- $this->assertInstanceOf(
- 'TestApp\Controller\Admin\PostsController',
- $result
- );
- $this->assertSame($request, $result->request);
- $this->assertSame($this->response, $result->response);
- }
- /**
- * Test building a nested prefix app controller
- *
- * @return void
- */
- public function testNestedPrefixedAppController()
- {
- $request = new ServerRequest([
- 'url' => 'admin/sub/posts/index',
- 'params' => [
- 'prefix' => 'admin/sub',
- 'controller' => 'Posts',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->create($request, $this->response);
- $this->assertInstanceOf(
- 'TestApp\Controller\Admin\Sub\PostsController',
- $result
- );
- $this->assertSame($request, $result->request);
- $this->assertSame($this->response, $result->response);
- }
- /**
- * Test building a plugin controller
- *
- * @return void
- */
- public function testPluginController()
- {
- $request = new ServerRequest([
- 'url' => 'test_plugin/test_plugin/index',
- 'params' => [
- 'plugin' => 'TestPlugin',
- 'controller' => 'TestPlugin',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->create($request, $this->response);
- $this->assertInstanceOf(
- 'TestPlugin\Controller\TestPluginController',
- $result
- );
- $this->assertSame($request, $result->request);
- $this->assertSame($this->response, $result->response);
- }
- /**
- * Test building a vendored plugin controller.
- *
- * @return void
- */
- public function testVendorPluginController()
- {
- $request = new ServerRequest([
- 'url' => 'test_plugin_three/ovens/index',
- 'params' => [
- 'plugin' => 'Company/TestPluginThree',
- 'controller' => 'Ovens',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->create($request, $this->response);
- $this->assertInstanceOf(
- 'Company\TestPluginThree\Controller\OvensController',
- $result
- );
- $this->assertSame($request, $result->request);
- $this->assertSame($this->response, $result->response);
- }
- /**
- * Test building a prefixed plugin controller
- *
- * @return void
- */
- public function testPrefixedPluginController()
- {
- $request = new ServerRequest([
- 'url' => 'test_plugin/admin/comments',
- 'params' => [
- 'prefix' => 'admin',
- 'plugin' => 'TestPlugin',
- 'controller' => 'Comments',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->create($request, $this->response);
- $this->assertInstanceOf(
- 'TestPlugin\Controller\Admin\CommentsController',
- $result
- );
- $this->assertSame($request, $result->request);
- $this->assertSame($this->response, $result->response);
- }
- /**
- * @return void
- */
- public function testAbstractClassFailure()
- {
- $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
- $this->expectExceptionMessage('Controller class Abstract could not be found.');
- $request = new ServerRequest([
- 'url' => 'abstract/index',
- 'params' => [
- 'controller' => 'Abstract',
- 'action' => 'index',
- ]
- ]);
- $this->factory->create($request, $this->response);
- }
- /**
- * @return void
- */
- public function testInterfaceFailure()
- {
- $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
- $this->expectExceptionMessage('Controller class Interface could not be found.');
- $request = new ServerRequest([
- 'url' => 'interface/index',
- 'params' => [
- 'controller' => 'Interface',
- 'action' => 'index',
- ]
- ]);
- $this->factory->create($request, $this->response);
- }
- /**
- * @return void
- */
- public function testMissingClassFailure()
- {
- $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
- $this->expectExceptionMessage('Controller class Invisible could not be found.');
- $request = new ServerRequest([
- 'url' => 'interface/index',
- 'params' => [
- 'controller' => 'Invisible',
- 'action' => 'index',
- ]
- ]);
- $this->factory->create($request, $this->response);
- }
- /**
- * @return void
- */
- public function testSlashedControllerFailure()
- {
- $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
- $this->expectExceptionMessage('Controller class Admin/Posts could not be found.');
- $request = new ServerRequest([
- 'url' => 'admin/posts/index',
- 'params' => [
- 'controller' => 'Admin/Posts',
- 'action' => 'index',
- ]
- ]);
- $this->factory->create($request, $this->response);
- }
- /**
- * @return void
- */
- public function testAbsoluteReferenceFailure()
- {
- $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
- $this->expectExceptionMessage('Controller class TestApp\Controller\CakesController could not be found.');
- $request = new ServerRequest([
- 'url' => 'interface/index',
- 'params' => [
- 'controller' => 'TestApp\Controller\CakesController',
- 'action' => 'index',
- ]
- ]);
- $this->factory->create($request, $this->response);
- }
- /**
- * Test building controller name when passing no controller name
- *
- * @return void
- */
- public function testGetControllerClassNoControllerName()
- {
- $request = new ServerRequest([
- 'url' => 'test_plugin_three/ovens/index',
- 'params' => [
- 'plugin' => 'Company/TestPluginThree',
- 'controller' => 'Ovens',
- 'action' => 'index',
- ]
- ]);
- $result = $this->factory->getControllerClass($request);
- $this->assertSame('Company\TestPluginThree\Controller\OvensController', $result);
- }
- }
|