5 * Contains \Drupal\Tests\Core\Controller\ControllerResolverTest.
8 namespace Drupal\Tests\Core\Controller;
10 use Drupal\Core\Controller\ControllerResolver;
11 use Drupal\Core\DependencyInjection\ClassResolver;
12 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
13 use Drupal\Core\Entity\EntityInterface;
14 use Drupal\Core\Routing\RouteMatch;
15 use Drupal\Core\Routing\RouteMatchInterface;
16 use Drupal\Tests\UnitTestCase;
17 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
22 use Symfony\Component\HttpFoundation\Request;
23 use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
24 use Psr\Http\Message\ServerRequestInterface;
27 * @coversDefaultClass \Drupal\Core\Controller\ControllerResolver
30 class ControllerResolverTest extends UnitTestCase {
33 * The tested controller resolver.
35 * @var \Drupal\Core\Controller\ControllerResolver
37 public $controllerResolver;
42 * @var \Symfony\Component\DependencyInjection\ContainerBuilder
47 * The PSR-7 converter.
49 * @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface
51 protected $httpMessageFactory;
56 protected function setUp() {
59 $this->container = new ContainerBuilder();
60 $class_resolver = new ClassResolver();
61 $class_resolver->setContainer($this->container);
62 $this->httpMessageFactory = new DiactorosFactory();
63 $this->controllerResolver = new ControllerResolver($this->httpMessageFactory, $class_resolver);
67 * Tests getArguments().
69 * Ensure that doGetArguments uses converted arguments if available.
71 * @see \Drupal\Core\Controller\ControllerResolver::getArguments()
72 * @see \Drupal\Core\Controller\ControllerResolver::doGetArguments()
74 public function testGetArguments() {
75 $controller = function(EntityInterface $entity, $user, RouteMatchInterface $route_match, ServerRequestInterface $psr_7) {
77 $mock_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity')
78 ->disableOriginalConstructor()
80 $mock_account = $this->getMock('Drupal\Core\Session\AccountInterface');
81 $request = new Request([], [], [
82 'entity' => $mock_entity,
83 'user' => $mock_account,
84 '_raw_variables' => new ParameterBag(['entity' => 1, 'user' => 1]),
85 ], [], [], ['HTTP_HOST' => 'drupal.org']);
86 $arguments = $this->controllerResolver->getArguments($request, $controller);
88 $this->assertEquals($mock_entity, $arguments[0]);
89 $this->assertEquals($mock_account, $arguments[1]);
90 $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[2], 'Ensure that the route match object is passed along as well');
91 $this->assertInstanceOf(ServerRequestInterface::class, $arguments[3], 'Ensure that the PSR-7 object is passed along as well');
95 * Tests createController().
97 * @dataProvider providerTestCreateController
99 public function testCreateController($controller, $class, $output) {
100 $this->container->set('some_service', new MockController());
101 $result = $this->controllerResolver->getControllerFromDefinition($controller);
102 $this->assertCallableController($result, $class, $output);
106 * Provides test data for testCreateController().
108 public function providerTestCreateController() {
110 // Tests class::method.
111 ['Drupal\Tests\Core\Controller\MockController::getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
112 // Tests service:method.
113 ['some_service:getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
114 // Tests a class with injection.
115 ['Drupal\Tests\Core\Controller\MockContainerInjection::getResult', 'Drupal\Tests\Core\Controller\MockContainerInjection', 'This used injection.'],
116 // Tests a ContainerAware class.
117 ['Drupal\Tests\Core\Controller\MockContainerAware::getResult', 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
122 * Tests createController() with a non-existent class.
124 public function testCreateControllerNonExistentClass() {
125 $this->setExpectedException(\InvalidArgumentException::class);
126 $this->controllerResolver->getControllerFromDefinition('Class::method');
130 * Tests createController() with an invalid name.
132 public function testCreateControllerInvalidName() {
133 $this->setExpectedException(\LogicException::class);
134 $this->controllerResolver->getControllerFromDefinition('ClassWithoutMethod');
138 * Tests getController().
140 * @dataProvider providerTestGetController
142 public function testGetController($attributes, $class, $output = NULL) {
143 $request = new Request([], [], $attributes);
144 $result = $this->controllerResolver->getController($request);
146 $this->assertCallableController($result, $class, $output);
149 $this->assertSame(FALSE, $result);
154 * Provides test data for testGetController().
156 public function providerTestGetController() {
158 // Tests passing a controller via the request.
159 [['_controller' => 'Drupal\Tests\Core\Controller\MockContainerAware::getResult'], 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
160 // Tests a request with no controller specified.
166 * Tests getControllerFromDefinition().
168 * @dataProvider providerTestGetControllerFromDefinition
170 public function testGetControllerFromDefinition($definition, $output) {
171 $controller = $this->controllerResolver->getControllerFromDefinition($definition);
172 $this->assertCallableController($controller, NULL, $output);
176 * Provides test data for testGetControllerFromDefinition().
178 public function providerTestGetControllerFromDefinition() {
180 // Tests a method on an object.
181 [[new MockController(), 'getResult'], 'This is a regular controller.'],
183 ['phpversion', phpversion()],
184 // Tests an object using __invoke().
185 [new MockInvokeController(), 'This used __invoke().'],
186 // Tests a class using __invoke().
187 ['Drupal\Tests\Core\Controller\MockInvokeController', 'This used __invoke().'],
191 * Tests getControllerFromDefinition() without a callable.
193 public function testGetControllerFromDefinitionNotCallable() {
194 $this->setExpectedException(\InvalidArgumentException::class);
195 $this->controllerResolver->getControllerFromDefinition('Drupal\Tests\Core\Controller\MockController::bananas');
199 * Asserts that the controller is callable and produces the correct output.
201 * @param callable $controller
202 * A callable controller.
203 * @param string|null $class
204 * Either the name of the class the controller represents, or NULL if it is
206 * @param mixed $output
207 * The output expected for this controller.
209 protected function assertCallableController($controller, $class, $output) {
211 $this->assertTrue(is_object($controller[0]));
212 $this->assertInstanceOf($class, $controller[0]);
214 $this->assertTrue(is_callable($controller));
215 $this->assertSame($output, call_user_func($controller));
219 * Tests getArguments with a route match and a request.
221 * @covers ::getArguments
222 * @covers ::doGetArguments
224 public function testGetArgumentsWithRouteMatchAndRequest() {
225 $request = Request::create('/test');
226 $mock_controller = new MockController();
227 $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
228 $this->assertEquals([RouteMatch::createFromRequest($request), $request], $arguments);
232 * Tests getArguments with a route match and a PSR-7 request.
234 * @covers ::getArguments
235 * @covers ::doGetArguments
237 public function testGetArgumentsWithRouteMatchAndPsr7Request() {
238 $request = Request::create('/test');
239 $mock_controller = new MockControllerPsr7();
240 $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
241 $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[0], 'Ensure that the route match object is passed along as well');
242 $this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $arguments[1], 'Ensure that the PSR-7 object is passed along as well');
247 class MockController {
248 public function getResult() {
249 return 'This is a regular controller.';
252 public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, Request $request) {
253 return 'this is another example controller';
257 class MockControllerPsr7 {
258 public function getResult() {
259 return ['#markup' => 'This is a regular controller'];
262 public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, ServerRequestInterface $request) {
263 return ['#markup' => 'this is another example controller'];
268 class MockContainerInjection implements ContainerInjectionInterface {
270 public function __construct($result) {
271 $this->result = $result;
273 public static function create(ContainerInterface $container) {
274 return new static('This used injection.');
276 public function getResult() {
277 return $this->result;
281 class MockContainerAware implements ContainerAwareInterface {
282 use ContainerAwareTrait;
283 public function getResult() {
284 return 'This is container aware.';
288 class MockInvokeController {
289 public function __invoke() {
290 return 'This used __invoke().';