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()
75 * @expectedDeprecation Drupal\Core\Controller\ControllerResolver::doGetArguments is deprecated as of 8.6.0 and will be removed in 9.0. Inject the "http_kernel.controller.argument_resolver" service instead.
77 public function testGetArguments() {
78 $controller = function (EntityInterface $entity, $user, RouteMatchInterface $route_match, ServerRequestInterface $psr_7) {
80 $mock_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity')
81 ->disableOriginalConstructor()
83 $mock_account = $this->getMock('Drupal\Core\Session\AccountInterface');
84 $request = new Request([], [], [
85 'entity' => $mock_entity,
86 'user' => $mock_account,
87 '_raw_variables' => new ParameterBag(['entity' => 1, 'user' => 1]),
88 ], [], [], ['HTTP_HOST' => 'drupal.org']);
89 $arguments = $this->controllerResolver->getArguments($request, $controller);
91 $this->assertEquals($mock_entity, $arguments[0]);
92 $this->assertEquals($mock_account, $arguments[1]);
93 $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[2], 'Ensure that the route match object is passed along as well');
94 $this->assertInstanceOf(ServerRequestInterface::class, $arguments[3], 'Ensure that the PSR-7 object is passed along as well');
98 * Tests createController().
100 * @dataProvider providerTestCreateController
102 public function testCreateController($controller, $class, $output) {
103 $this->container->set('some_service', new MockController());
104 $result = $this->controllerResolver->getControllerFromDefinition($controller);
105 $this->assertCallableController($result, $class, $output);
109 * Provides test data for testCreateController().
111 public function providerTestCreateController() {
113 // Tests class::method.
114 ['Drupal\Tests\Core\Controller\MockController::getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
115 // Tests service:method.
116 ['some_service:getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
117 // Tests a class with injection.
118 ['Drupal\Tests\Core\Controller\MockContainerInjection::getResult', 'Drupal\Tests\Core\Controller\MockContainerInjection', 'This used injection.'],
119 // Tests a ContainerAware class.
120 ['Drupal\Tests\Core\Controller\MockContainerAware::getResult', 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
125 * Tests createController() with a non-existent class.
127 public function testCreateControllerNonExistentClass() {
128 $this->setExpectedException(\InvalidArgumentException::class);
129 $this->controllerResolver->getControllerFromDefinition('Class::method');
133 * Tests createController() with an invalid name.
135 public function testCreateControllerInvalidName() {
136 $this->setExpectedException(\LogicException::class);
137 $this->controllerResolver->getControllerFromDefinition('ClassWithoutMethod');
141 * Tests getController().
143 * @dataProvider providerTestGetController
145 public function testGetController($attributes, $class, $output = NULL) {
146 $request = new Request([], [], $attributes);
147 $result = $this->controllerResolver->getController($request);
149 $this->assertCallableController($result, $class, $output);
152 $this->assertSame(FALSE, $result);
157 * Provides test data for testGetController().
159 public function providerTestGetController() {
161 // Tests passing a controller via the request.
162 [['_controller' => 'Drupal\Tests\Core\Controller\MockContainerAware::getResult'], 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
163 // Tests a request with no controller specified.
169 * Tests getControllerFromDefinition().
171 * @dataProvider providerTestGetControllerFromDefinition
173 public function testGetControllerFromDefinition($definition, $output) {
174 $controller = $this->controllerResolver->getControllerFromDefinition($definition);
175 $this->assertCallableController($controller, NULL, $output);
179 * Provides test data for testGetControllerFromDefinition().
181 public function providerTestGetControllerFromDefinition() {
183 // Tests a method on an object.
184 [[new MockController(), 'getResult'], 'This is a regular controller.'],
186 ['phpversion', phpversion()],
187 // Tests an object using __invoke().
188 [new MockInvokeController(), 'This used __invoke().'],
189 // Tests a class using __invoke().
190 ['Drupal\Tests\Core\Controller\MockInvokeController', 'This used __invoke().'],
195 * Tests getControllerFromDefinition() without a callable.
197 public function testGetControllerFromDefinitionNotCallable() {
198 $this->setExpectedException(\InvalidArgumentException::class);
199 $this->controllerResolver->getControllerFromDefinition('Drupal\Tests\Core\Controller\MockController::bananas');
203 * Asserts that the controller is callable and produces the correct output.
205 * @param callable $controller
206 * A callable controller.
207 * @param string|null $class
208 * Either the name of the class the controller represents, or NULL if it is
210 * @param mixed $output
211 * The output expected for this controller.
213 protected function assertCallableController($controller, $class, $output) {
215 $this->assertTrue(is_object($controller[0]));
216 $this->assertInstanceOf($class, $controller[0]);
218 $this->assertTrue(is_callable($controller));
219 $this->assertSame($output, call_user_func($controller));
223 * Tests getArguments with a route match and a request.
225 * @covers ::getArguments
226 * @covers ::doGetArguments
230 public function testGetArgumentsWithRouteMatchAndRequest() {
231 $request = Request::create('/test');
232 $mock_controller = new MockController();
233 $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
234 $this->assertEquals([RouteMatch::createFromRequest($request), $request], $arguments);
238 * Tests getArguments with a route match and a PSR-7 request.
240 * @covers ::getArguments
241 * @covers ::doGetArguments
245 public function testGetArgumentsWithRouteMatchAndPsr7Request() {
246 $request = Request::create('/test');
247 $mock_controller = new MockControllerPsr7();
248 $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
249 $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[0], 'Ensure that the route match object is passed along as well');
250 $this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $arguments[1], 'Ensure that the PSR-7 object is passed along as well');
255 class MockController {
257 public function getResult() {
258 return 'This is a regular controller.';
261 public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, Request $request) {
262 return 'this is another example controller';
266 class MockControllerPsr7 {
268 public function getResult() {
269 return ['#markup' => 'This is a regular controller'];
272 public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, ServerRequestInterface $request) {
273 return ['#markup' => 'this is another example controller'];
278 class MockContainerInjection implements ContainerInjectionInterface {
281 public function __construct($result) {
282 $this->result = $result;
285 public static function create(ContainerInterface $container) {
286 return new static('This used injection.');
289 public function getResult() {
290 return $this->result;
294 class MockContainerAware implements ContainerAwareInterface {
295 use ContainerAwareTrait;
297 public function getResult() {
298 return 'This is container aware.';
302 class MockInvokeController {
304 public function __invoke() {
305 return 'This used __invoke().';