diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/ControllerResolverTest.php b/core/modules/system/lib/Drupal/system/Tests/Routing/ControllerResolverTest.php deleted file mode 100644 index e9c6299..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/ControllerResolverTest.php +++ /dev/null @@ -1,43 +0,0 @@ - 'Controller Resolver tests', - 'description' => 'Tests that the Drupal-extended ControllerResolver is functioning properly.', - 'group' => 'Routing', - ); - } - - /** - * Confirms that a container aware controller gets returned. - */ - function testContainerAware() { - $container = new Container(); - $resolver = new ControllerResolver($container); - - $request = Request::create('/some/path'); - $request->attributes->set('_controller', '\Drupal\system\Tests\Routing\MockController::run'); - - $controller = $resolver->getController($request); - - $this->assertTrue($controller[0] instanceof MockController, 'The correct controller object was returned.'); - } -} diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/MockController.php b/core/modules/system/lib/Drupal/system/Tests/Routing/MockController.php deleted file mode 100644 index bc9d093..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/MockController.php +++ /dev/null @@ -1,22 +0,0 @@ -disableOriginalConstructor() ->getMock(); $mock_account = $this->getMock('Drupal\Core\Session\AccountInterface'); - $request = new \Symfony\Component\HttpFoundation\Request(array(), array(), array( + $request = new Request(array(), array(), array( 'entity' => $mock_entity, 'user' => $mock_account, '_raw_variables' => new ParameterBag(array('entity' => 1, 'user' => 1)), @@ -73,4 +76,130 @@ public function testGetArguments() { $this->assertEquals(1, $arguments[1], 'Not type hinted variables should use not upcasted values.'); } + /** + * Confirms that a container aware controller gets returned. + */ + public function testContainerAware() { + $container = new Container(); + $resolver = new ControllerResolver($container); + + $request = Request::create('/some/path'); + $request->attributes->set('_controller', '\Drupal\Tests\Core\Controller\MockController::run'); + + $controller = $resolver->getController($request); + + $this->assertTrue($controller[0] instanceof MockController, 'The wrong controller object was returned.'); + } + + /** + * Tests that the proper exception is thrown with a bad class. + * + * @expectedException \InvalidArgumentException + */ + public function testInvalidArgumentException() { + $container = new Container(); + $resolver = new ControllerResolver($container); + + $request = Request::create('/some/path'); + $request->attributes->set('_controller', '\Drupal\Tests\Core\Controller\MockControllerBadClass::run'); + + // This should throw an exception. + $controller = $resolver->getController($request); + } + + /** + * Tests that the proper exception is thrown with a bad name. + * + * @expectedException \LogicException + */ + public function testLogicException() { + $container = new Container(); + $resolver = new ControllerResolver($container); + + $request = Request::create('/some/path'); + $request->attributes->set('_controller', '\Drupal\Tests\Core\Controller\MockControllerBadFormatting'); + + // This should throw an exception. + $controller = $resolver->getController($request); + } + + /** + * Test the service:method notation. + */ + public function testServiceMethodNotation() { + $container = new Container(); + $resolver = new ControllerResolver($container); + + $request = Request::create('/some/path'); + + $container->set('mock.controller', new MockController); + $request->attributes->set('_controller', 'mock.controller:run'); + + $controller = $resolver->getController($request); + + $this->assertInstanceOf('\Drupal\Tests\Core\Controller\MockController', $controller[0], 'The wrong controller object was returned.'); + } + + /** + * Test using the ControllerInterface for the controller. + */ + public function testControllerInterface() { + $container = new Container(); + $resolver = new ControllerResolver($container); + + $request = Request::create('/some/path'); + $request->attributes->set('_controller', '\Drupal\Tests\Core\Controller\MockControllerAsInterface::run'); + + $controller = $resolver->getController($request); + + $this->assertInstanceOf('\Drupal\Tests\Core\Controller\MockControllerAsInterface', $controller[0], 'The wrong controller object was returned.'); + + // Verify that the controller was instantiated using + // MockControllerAsInterface::create(). + $this->assertTrue($controller[0]->called, 'The controller was not properly instantiated.'); + } + +} + +/** + * Dummy class, just for testing. + */ +class MockController extends ContainerAware { + + /** + * Does nothing; this is just a fake controller method. + */ + public function run() {} + +} + +/** + * Dummy class, just for testing. + */ +class MockControllerAsInterface extends ContainerAware implements ContainerInjectionInterface { + + /** + * Variable to determine if self::create() is called during testing. + * + * @var bool + */ + public $called = FALSE; + + /** + * Does nothing; this is just a fake controller method. + */ + public function run() {} + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + $controller = new self(); + + // Set this variable so the test can confirm this method was called. + $controller->called = TRUE; + + return $controller; + } + }