diff --git a/core/lib/Drupal/Core/Controller/ControllerResolver.php b/core/lib/Drupal/Core/Controller/ControllerResolver.php index 7999f0e..7928c36 100644 --- a/core/lib/Drupal/Core/Controller/ControllerResolver.php +++ b/core/lib/Drupal/Core/Controller/ControllerResolver.php @@ -33,7 +33,7 @@ class ControllerResolver extends BaseControllerResolver implements ControllerRes /** * The injection container that should be injected into all controllers. * - * @var Symfony\Component\DependencyInjection\ContainerInterface + * @var \Symfony\Component\DependencyInjection\ContainerInterface */ protected $container; @@ -47,9 +47,9 @@ class ControllerResolver extends BaseControllerResolver implements ControllerRes /** * Constructs a new ControllerResolver. * - * @param Symfony\Component\DependencyInjection\ContainerInterface $container + * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * A ContainerInterface instance. - * @param Symfony\Component\HttpKernel\Log\LoggerInterface $logger + * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger * (optional) A LoggerInterface instance. */ public function __construct(ContainerInterface $container, LoggerInterface $logger = NULL) { 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 @@ - 'Controller Resolver tests', @@ -43,8 +58,8 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $container = new ContainerBuilder(); - $this->controllerResolver = new ControllerResolver($container); + $this->container = new ContainerBuilder(); + $this->controllerResolver = new ControllerResolver($this->container); } /** @@ -62,7 +77,7 @@ public function testGetArguments() { ->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 +88,159 @@ public function testGetArguments() { $this->assertEquals(1, $arguments[1], 'Not type hinted variables should use not upcasted values.'); } + /** + * Tests createController(). + * + * @dataProvider providerTestCreateController + */ + public function testCreateController($controller, $class, $output) { + $this->container->set('some_service', new MockController()); + $result = $this->controllerResolver->getControllerFromDefinition($controller); + $this->assertCallableController($result, $class, $output); + } + + /** + * Provides test data for testCreateController(). + */ + public function providerTestCreateController() { + return array( + // Tests class::method. + array('Drupal\Tests\Core\Controller\MockController::getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'), + // Tests service:method. + array('some_service:getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'), + // Tests a class with injection. + array('Drupal\Tests\Core\Controller\MockContainerInjection::getResult', 'Drupal\Tests\Core\Controller\MockContainerInjection', 'This used injection.'), + // Tests a ContainerAware class. + array('Drupal\Tests\Core\Controller\MockContainerAware::getResult', 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'), + ); + } + + /** + * Tests createController() with a non-existent class. + * + * @expectedException \InvalidArgumentException + */ + public function testCreateControllerNonExistentClass() { + $this->controllerResolver->getControllerFromDefinition('Class::method'); + } + + /** + * Tests createController() with an invalid name. + * + * @expectedException \LogicException + */ + public function testCreateControllerInvalidName() { + $this->controllerResolver->getControllerFromDefinition('ClassWithoutMethod'); + } + + /** + * Tests getController(). + * + * @dataProvider providerTestGetController + */ + public function testGetController($attributes, $class, $output = NULL) { + $request = new Request(array(), array(), $attributes); + $result = $this->controllerResolver->getController($request); + if ($class) { + $this->assertCallableController($result, $class, $output); + } + else { + $this->assertSame(FALSE, $result); + } + } + + /** + * Provides test data for testGetController(). + */ + public function providerTestGetController() { + return array( + // Tests passing a controller via the request. + array(array('_controller' => 'Drupal\Tests\Core\Controller\MockContainerAware::getResult'), 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'), + // Tests a request with no controller specified. + array(array(), FALSE) + ); + } + + /** + * Tests getControllerFromDefinition(). + * + * @dataProvider providerTestGetControllerFromDefinition + */ + public function testGetControllerFromDefinition($definition, $output) { + $controller = $this->controllerResolver->getControllerFromDefinition($definition); + $this->assertCallableController($controller, NULL, $output); + } + + /** + * Provides test data for testGetControllerFromDefinition(). + */ + public function providerTestGetControllerFromDefinition() { + return array( + // Tests a method on an object. + array(array(new MockController(), 'getResult'), 'This is a regular controller.'), + // Tests a function. + array('phpversion', phpversion()), + // Tests an object using __invoke(). + array(new MockInvokeController(), 'This used __invoke().'), + // Tests a class using __invoke(). + array('Drupal\Tests\Core\Controller\MockInvokeController', 'This used __invoke().'), + ); + } + /** + * Tests getControllerFromDefinition() without a callable. + * + * @expectedException \InvalidArgumentException + */ + public function testGetControllerFromDefinitionNotCallable() { + $this->controllerResolver->getControllerFromDefinition('Drupal\Tests\Core\Controller\MockController::bananas'); + } + + /** + * Asserts that the controller is callable and produces the correct output. + * + * @param callable $controller + * A callable controller. + * @param string|null $class + * Either the name of the class the controller represents, or NULL if it is + * not an object. + * @param mixed $output + * The output expected for this controller. + */ + protected function assertCallableController($controller, $class, $output) { + if ($class) { + $this->assertTrue(is_object($controller[0])); + $this->assertInstanceOf($class, $controller[0]); + } + $this->assertTrue(is_callable($controller)); + $this->assertSame($output, call_user_func($controller)); + } + +} + +class MockController { + public function getResult() { + return 'This is a regular controller.'; + } +} +class MockContainerInjection implements ContainerInjectionInterface { + protected $result; + public function __construct($result) { + $this->result = $result; + } + public static function create(ContainerInterface $container) { + return new static('This used injection.'); + } + public function getResult() { + return $this->result; + } +} +class MockContainerAware extends ContainerAware { + public function getResult() { + return 'This is container aware.'; + } +} +class MockInvokeController { + public function __invoke() { + return 'This used __invoke().'; + } }