Issue #2042739 by jhedstrom, bdone, dawehner, tim.plunkett: Convert system module's ControllerResolverTest to phpunit.
parent
b50ffe9928
commit
d1275b0025
|
@ -33,7 +33,7 @@ class ControllerResolver extends BaseControllerResolver implements ControllerRes
|
||||||
/**
|
/**
|
||||||
* The injection container that should be injected into all controllers.
|
* The injection container that should be injected into all controllers.
|
||||||
*
|
*
|
||||||
* @var Symfony\Component\DependencyInjection\ContainerInterface
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface
|
||||||
*/
|
*/
|
||||||
protected $container;
|
protected $container;
|
||||||
|
|
||||||
|
@ -47,9 +47,9 @@ class ControllerResolver extends BaseControllerResolver implements ControllerRes
|
||||||
/**
|
/**
|
||||||
* Constructs a new ControllerResolver.
|
* Constructs a new ControllerResolver.
|
||||||
*
|
*
|
||||||
* @param Symfony\Component\DependencyInjection\ContainerInterface $container
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
|
||||||
* A ContainerInterface instance.
|
* A ContainerInterface instance.
|
||||||
* @param Symfony\Component\HttpKernel\Log\LoggerInterface $logger
|
* @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
|
||||||
* (optional) A LoggerInterface instance.
|
* (optional) A LoggerInterface instance.
|
||||||
*/
|
*/
|
||||||
public function __construct(ContainerInterface $container, LoggerInterface $logger = NULL) {
|
public function __construct(ContainerInterface $container, LoggerInterface $logger = NULL) {
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Definition of Drupal\system\Tests\Routing\ControllerResolverTest.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\system\Tests\Routing;
|
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\Container;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
|
|
||||||
use Drupal\Core\Controller\ControllerResolver;
|
|
||||||
use Drupal\simpletest\UnitTestBase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests that the Drupal-extended ControllerResolver is functioning properly.
|
|
||||||
*/
|
|
||||||
class ControllerResolverTest extends UnitTestBase {
|
|
||||||
|
|
||||||
public static function getInfo() {
|
|
||||||
return array(
|
|
||||||
'name' => '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.');
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Contains Drupal\system\Tests\Routing\MockController.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\system\Tests\Routing;
|
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerAware;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dummy class, just for testing.
|
|
||||||
*/
|
|
||||||
class MockController extends ContainerAware {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does nothing; this is just a fake controller method.
|
|
||||||
*/
|
|
||||||
public function run() {}
|
|
||||||
|
|
||||||
}
|
|
|
@ -8,17 +8,22 @@
|
||||||
namespace Drupal\Tests\Core\Controller;
|
namespace Drupal\Tests\Core\Controller;
|
||||||
|
|
||||||
use Drupal\Core\Controller\ControllerResolver;
|
use Drupal\Core\Controller\ControllerResolver;
|
||||||
|
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
use Drupal\Tests\UnitTestCase;
|
use Drupal\Tests\UnitTestCase;
|
||||||
use Drupal\user\UserInterface;
|
use Symfony\Component\DependencyInjection\ContainerAware;
|
||||||
use Guzzle\Http\Message\Request;
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that the Drupal-extended ControllerResolver is functioning properly.
|
* Tests that the Drupal-extended ControllerResolver is functioning properly.
|
||||||
*
|
*
|
||||||
* @see \Drupal\Core\Controller\ControllerResolver
|
* @see \Drupal\Core\Controller\ControllerResolver
|
||||||
|
*
|
||||||
|
* @group Drupal
|
||||||
|
* @group Routing
|
||||||
*/
|
*/
|
||||||
class ControllerResolverTest extends UnitTestCase {
|
class ControllerResolverTest extends UnitTestCase {
|
||||||
|
|
||||||
|
@ -29,6 +34,16 @@ class ControllerResolverTest extends UnitTestCase {
|
||||||
*/
|
*/
|
||||||
public $controllerResolver;
|
public $controllerResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The container.
|
||||||
|
*
|
||||||
|
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
|
||||||
|
*/
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public static function getInfo() {
|
public static function getInfo() {
|
||||||
return array(
|
return array(
|
||||||
'name' => 'Controller Resolver tests',
|
'name' => 'Controller Resolver tests',
|
||||||
|
@ -43,8 +58,8 @@ class ControllerResolverTest extends UnitTestCase {
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$container = new ContainerBuilder();
|
$this->container = new ContainerBuilder();
|
||||||
$this->controllerResolver = new ControllerResolver($container);
|
$this->controllerResolver = new ControllerResolver($this->container);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,7 +77,7 @@ class ControllerResolverTest extends UnitTestCase {
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
->getMock();
|
->getMock();
|
||||||
$mock_account = $this->getMock('Drupal\Core\Session\AccountInterface');
|
$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,
|
'entity' => $mock_entity,
|
||||||
'user' => $mock_account,
|
'user' => $mock_account,
|
||||||
'_raw_variables' => new ParameterBag(array('entity' => 1, 'user' => 1)),
|
'_raw_variables' => new ParameterBag(array('entity' => 1, 'user' => 1)),
|
||||||
|
@ -73,4 +88,159 @@ class ControllerResolverTest extends UnitTestCase {
|
||||||
$this->assertEquals(1, $arguments[1], 'Not type hinted variables should use not upcasted values.');
|
$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().';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue