View source
<?php
namespace Drupal\Tests\Core\Controller;
use Drupal\Core\Controller\ControllerResolver;
use Drupal\Core\DependencyInjection\ClassResolver;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Psr\Http\Message\ServerRequestInterface;
class ControllerResolverTest extends UnitTestCase {
public $controllerResolver;
protected $container;
protected $httpMessageFactory;
protected function setUp() {
parent::setUp();
$this->container = new ContainerBuilder();
$class_resolver = new ClassResolver();
$class_resolver
->setContainer($this->container);
$this->httpMessageFactory = new DiactorosFactory();
$this->controllerResolver = new ControllerResolver($this->httpMessageFactory, $class_resolver);
}
public function testGetArguments() {
$controller = function (EntityInterface $entity, $user, RouteMatchInterface $route_match, ServerRequestInterface $psr_7) {
};
$mock_entity = $this
->getMockBuilder('Drupal\\Core\\Entity\\Entity')
->disableOriginalConstructor()
->getMock();
$mock_account = $this
->getMock('Drupal\\Core\\Session\\AccountInterface');
$request = new Request(array(), array(), array(
'entity' => $mock_entity,
'user' => $mock_account,
'_raw_variables' => new ParameterBag(array(
'entity' => 1,
'user' => 1,
)),
), array(), array(), array(
'HTTP_HOST' => 'drupal.org',
));
$arguments = $this->controllerResolver
->getArguments($request, $controller);
$this
->assertEquals($mock_entity, $arguments[0]);
$this
->assertEquals($mock_account, $arguments[1]);
$this
->assertEquals(RouteMatch::createFromRequest($request), $arguments[2], 'Ensure that the route match object is passed along as well');
$this
->assertInstanceOf(ServerRequestInterface::class, $arguments[3], 'Ensure that the PSR-7 object is passed along as well');
}
public function testCreateController($controller, $class, $output) {
$this->container
->set('some_service', new MockController());
$result = $this->controllerResolver
->getControllerFromDefinition($controller);
$this
->assertCallableController($result, $class, $output);
}
public function providerTestCreateController() {
return array(
array(
'Drupal\\Tests\\Core\\Controller\\MockController::getResult',
'Drupal\\Tests\\Core\\Controller\\MockController',
'This is a regular controller.',
),
array(
'some_service:getResult',
'Drupal\\Tests\\Core\\Controller\\MockController',
'This is a regular controller.',
),
array(
'Drupal\\Tests\\Core\\Controller\\MockContainerInjection::getResult',
'Drupal\\Tests\\Core\\Controller\\MockContainerInjection',
'This used injection.',
),
array(
'Drupal\\Tests\\Core\\Controller\\MockContainerAware::getResult',
'Drupal\\Tests\\Core\\Controller\\MockContainerAware',
'This is container aware.',
),
);
}
public function testCreateControllerNonExistentClass() {
$this->controllerResolver
->getControllerFromDefinition('Class::method');
}
public function testCreateControllerInvalidName() {
$this->controllerResolver
->getControllerFromDefinition('ClassWithoutMethod');
}
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);
}
}
public function providerTestGetController() {
return array(
array(
array(
'_controller' => 'Drupal\\Tests\\Core\\Controller\\MockContainerAware::getResult',
),
'Drupal\\Tests\\Core\\Controller\\MockContainerAware',
'This is container aware.',
),
array(
array(),
FALSE,
),
);
}
public function testGetControllerFromDefinition($definition, $output) {
$controller = $this->controllerResolver
->getControllerFromDefinition($definition);
$this
->assertCallableController($controller, NULL, $output);
}
public function providerTestGetControllerFromDefinition() {
return array(
array(
array(
new MockController(),
'getResult',
),
'This is a regular controller.',
),
array(
'phpversion',
phpversion(),
),
array(
new MockInvokeController(),
'This used __invoke().',
),
array(
'Drupal\\Tests\\Core\\Controller\\MockInvokeController',
'This used __invoke().',
),
);
}
public function testGetControllerFromDefinitionNotCallable() {
$this->controllerResolver
->getControllerFromDefinition('Drupal\\Tests\\Core\\Controller\\MockController::bananas');
}
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));
}
public function testGetArgumentsWithRouteMatchAndRequest() {
$request = Request::create('/test');
$mock_controller = new MockController();
$arguments = $this->controllerResolver
->getArguments($request, [
$mock_controller,
'getControllerWithRequestAndRouteMatch',
]);
$this
->assertEquals([
RouteMatch::createFromRequest($request),
$request,
], $arguments);
}
public function testGetArgumentsWithRouteMatchAndPsr7Request() {
$request = Request::create('/test');
$mock_controller = new MockControllerPsr7();
$arguments = $this->controllerResolver
->getArguments($request, [
$mock_controller,
'getControllerWithRequestAndRouteMatch',
]);
$this
->assertEquals(RouteMatch::createFromRequest($request), $arguments[0], 'Ensure that the route match object is passed along as well');
$this
->assertInstanceOf('Psr\\Http\\Message\\ServerRequestInterface', $arguments[1], 'Ensure that the PSR-7 object is passed along as well');
}
}
class MockController {
public function getResult() {
return 'This is a regular controller.';
}
public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, Request $request) {
return 'this is another example controller';
}
}
class MockControllerPsr7 {
public function getResult() {
return [
'#markup' => 'This is a regular controller',
];
}
public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, ServerRequestInterface $request) {
return [
'#markup' => 'this is another example 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().';
}
}