You are here

class ControllerResolverTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php \Drupal\Tests\Core\Controller\ControllerResolverTest
  2. 9 core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php \Drupal\Tests\Core\Controller\ControllerResolverTest

@coversDefaultClass \Drupal\Core\Controller\ControllerResolver @group Controller

Hierarchy

Expanded class hierarchy of ControllerResolverTest

File

core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php, line 28
Contains \Drupal\Tests\Core\Controller\ControllerResolverTest.

Namespace

Drupal\Tests\Core\Controller
View 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() : void {
    parent::setUp();
    $this->container = new ContainerBuilder();
    $class_resolver = new ClassResolver();
    $class_resolver
      ->setContainer($this->container);
    $this->httpMessageFactory = new PsrHttpFactory(new HttpFactory(), new HttpFactory(), new HttpFactory(), new HttpFactory());
    $this->controllerResolver = new ControllerResolver($this->httpMessageFactory, $class_resolver);
  }

  /**
   * 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 [
      // Tests class::method.
      [
        'Drupal\\Tests\\Core\\Controller\\MockController::getResult',
        'Drupal\\Tests\\Core\\Controller\\MockController',
        'This is a regular controller.',
      ],
      // Tests service:method.
      [
        'some_service:getResult',
        'Drupal\\Tests\\Core\\Controller\\MockController',
        'This is a regular controller.',
      ],
      // Tests a class with injection.
      [
        'Drupal\\Tests\\Core\\Controller\\MockContainerInjection::getResult',
        'Drupal\\Tests\\Core\\Controller\\MockContainerInjection',
        'This used injection.',
      ],
      // Tests a ContainerAware class.
      [
        'Drupal\\Tests\\Core\\Controller\\MockContainerAware::getResult',
        'Drupal\\Tests\\Core\\Controller\\MockContainerAware',
        'This is container aware.',
      ],
    ];
  }

  /**
   * Tests createController() with a non-existent class.
   */
  public function testCreateControllerNonExistentClass() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $this->controllerResolver
      ->getControllerFromDefinition('Class::method');
  }

  /**
   * Tests createController() with an invalid name.
   */
  public function testCreateControllerInvalidName() {
    $this
      ->expectException(\LogicException::class);
    $this->controllerResolver
      ->getControllerFromDefinition('ClassWithoutMethod');
  }

  /**
   * Tests getController().
   *
   * @dataProvider providerTestGetController
   */
  public function testGetController($attributes, $class, $output = NULL) {
    $request = new Request([], [], $attributes);
    $result = $this->controllerResolver
      ->getController($request);
    if ($class) {
      $this
        ->assertCallableController($result, $class, $output);
    }
    else {
      $this
        ->assertFalse($result);
    }
  }

  /**
   * Provides test data for testGetController().
   */
  public function providerTestGetController() {
    return [
      // Tests passing a controller via the request.
      [
        [
          '_controller' => 'Drupal\\Tests\\Core\\Controller\\MockContainerAware::getResult',
        ],
        'Drupal\\Tests\\Core\\Controller\\MockContainerAware',
        'This is container aware.',
      ],
      // Tests a request with no controller specified.
      [
        [],
        FALSE,
      ],
    ];
  }

  /**
   * Tests getControllerFromDefinition().
   *
   * @dataProvider providerTestGetControllerFromDefinition
   */
  public function testGetControllerFromDefinition($definition, $output) {
    $this->container
      ->set('invoke_service', new MockInvokeController());
    $controller = $this->controllerResolver
      ->getControllerFromDefinition($definition);
    $this
      ->assertCallableController($controller, NULL, $output);
  }

  /**
   * Provides test data for testGetControllerFromDefinition().
   */
  public function providerTestGetControllerFromDefinition() {
    return [
      // Tests a method on an object.
      [
        [
          new MockController(),
          'getResult',
        ],
        'This is a regular controller.',
      ],
      // Tests a function.
      [
        'phpversion',
        phpversion(),
      ],
      // Tests an object using __invoke().
      [
        new MockInvokeController(),
        'This used __invoke().',
      ],
      // Tests a class using __invoke().
      [
        'Drupal\\Tests\\Core\\Controller\\MockInvokeController',
        'This used __invoke().',
      ],
      // Tests a service from the container using __invoke().
      [
        'invoke_service',
        'This used __invoke().',
      ],
    ];
  }

  /**
   * Tests getControllerFromDefinition() without a callable.
   */
  public function testGetControllerFromDefinitionNotCallable() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $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 string|null $output
   *   The output expected for this controller.
   *
   * @internal
   */
  protected function assertCallableController(callable $controller, ?string $class, ?string $output) : void {
    if ($class) {
      $this
        ->assertIsObject($controller[0]);
      $this
        ->assertInstanceOf($class, $controller[0]);
    }
    $this
      ->assertIsCallable($controller);
    $this
      ->assertSame($output, call_user_func($controller));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerResolverTest::$container protected property The container.
ControllerResolverTest::$controllerResolver public property The tested controller resolver.
ControllerResolverTest::$httpMessageFactory protected property The PSR-7 converter.
ControllerResolverTest::assertCallableController protected function Asserts that the controller is callable and produces the correct output.
ControllerResolverTest::providerTestCreateController public function Provides test data for testCreateController().
ControllerResolverTest::providerTestGetController public function Provides test data for testGetController().
ControllerResolverTest::providerTestGetControllerFromDefinition public function Provides test data for testGetControllerFromDefinition().
ControllerResolverTest::setUp protected function Overrides UnitTestCase::setUp
ControllerResolverTest::testCreateController public function Tests createController().
ControllerResolverTest::testCreateControllerInvalidName public function Tests createController() with an invalid name.
ControllerResolverTest::testCreateControllerNonExistentClass public function Tests createController() with a non-existent class.
ControllerResolverTest::testGetController public function Tests getController().
ControllerResolverTest::testGetControllerFromDefinition public function Tests getControllerFromDefinition().
ControllerResolverTest::testGetControllerFromDefinitionNotCallable public function Tests getControllerFromDefinition() without a callable.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function