class ControllerResolverTest in Zircon Profile 8
Same name in this branch
- 8 vendor/symfony/http-kernel/Tests/Controller/ControllerResolverTest.php \Symfony\Component\HttpKernel\Tests\Controller\ControllerResolverTest
- 8 core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php \Drupal\Tests\Core\Controller\ControllerResolverTest
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php \Drupal\Tests\Core\Controller\ControllerResolverTest
@coversDefaultClass \Drupal\Core\Controller\ControllerResolver @group Controller
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Controller\ControllerResolverTest
Expanded class hierarchy of ControllerResolverTest
File
- core/
tests/ Drupal/ Tests/ Core/ Controller/ ControllerResolverTest.php, line 29 - Contains \Drupal\Tests\Core\Controller\ControllerResolverTest.
Namespace
Drupal\Tests\Core\ControllerView source
class ControllerResolverTest extends UnitTestCase {
/**
* The tested controller resolver.
*
* @var \Drupal\Core\Controller\ControllerResolver
*/
public $controllerResolver;
/**
* The container.
*
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* The PSR-7 converter.
*
* @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface
*/
protected $httpMessageFactory;
/**
* {@inheritdoc}
*/
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);
}
/**
* Tests getArguments().
*
* Ensure that doGetArguments uses converted arguments if available.
*
* @see \Drupal\Core\Controller\ControllerResolver::getArguments()
* @see \Drupal\Core\Controller\ControllerResolver::doGetArguments()
*/
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');
}
/**
* 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));
}
/**
* Tests getArguments with a route match and a request.
*
* @covers ::getArguments
* @covers ::doGetArguments
*/
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);
}
/**
* Tests getArguments with a route match and a PSR-7 request.
*
* @covers ::getArguments
* @covers ::doGetArguments
*/
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');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerResolverTest:: |
protected | property | The container. | |
ControllerResolverTest:: |
public | property | The tested controller resolver. | |
ControllerResolverTest:: |
protected | property | The PSR-7 converter. | |
ControllerResolverTest:: |
protected | function | Asserts that the controller is callable and produces the correct output. | |
ControllerResolverTest:: |
public | function | Provides test data for testCreateController(). | |
ControllerResolverTest:: |
public | function | Provides test data for testGetController(). | |
ControllerResolverTest:: |
public | function | Provides test data for testGetControllerFromDefinition(). | |
ControllerResolverTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ControllerResolverTest:: |
public | function | Tests createController(). | |
ControllerResolverTest:: |
public | function | Tests createController() with an invalid name. | |
ControllerResolverTest:: |
public | function | Tests createController() with a non-existent class. | |
ControllerResolverTest:: |
public | function | Tests getArguments(). | |
ControllerResolverTest:: |
public | function | Tests getArguments with a route match and a PSR-7 request. | |
ControllerResolverTest:: |
public | function | Tests getArguments with a route match and a request. | |
ControllerResolverTest:: |
public | function | Tests getController(). | |
ControllerResolverTest:: |
public | function | Tests getControllerFromDefinition(). | |
ControllerResolverTest:: |
public | function | Tests getControllerFromDefinition() without a callable. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |