You are here

class EntityResolverManagerTest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/EntityResolverManagerTest.php \Drupal\Tests\Core\Entity\EntityResolverManagerTest

@coversDefaultClass \Drupal\Core\Entity\EntityResolverManager @group Entity

Hierarchy

Expanded class hierarchy of EntityResolverManagerTest

File

core/tests/Drupal/Tests/Core/Entity/EntityResolverManagerTest.php, line 23
Contains \Drupal\Tests\Core\Entity\EntityResolverManagerTest.

Namespace

Drupal\Tests\Core\Entity
View source
class EntityResolverManagerTest extends UnitTestCase {

  /**
   * The tested entity resolver manager.
   *
   * @var \Drupal\Core\Entity\EntityResolverManager
   */
  protected $entityResolverManager;

  /**
   * The mocked entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The mocked class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $classResolver;

  /**
   * The mocked dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * {@inheritdoc}
   *
   * @covers ::__construct
   */
  protected function setUp() {
    $this->entityManager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
    $this->container = $this
      ->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
    $this->classResolver = $this
      ->getClassResolverStub();
    $this->entityResolverManager = new EntityResolverManager($this->entityManager, $this->classResolver);
  }

  /**
   * Tests setRouteOptions() with no parameter.
   *
   * We don't have any entity type involved, so we don't need any upcasting.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   *
   * @dataProvider providerTestSetRouteOptionsWithStandardRoute
   */
  public function testSetRouteOptionsWithStandardRoute($controller) {
    $route = new Route('/example', array(
      '_controller' => $controller,
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithStandardRoute.
   */
  public function providerTestSetRouteOptionsWithStandardRoute() {
    return array(
      array(
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethod',
      ),
      array(
        'test_function_controller',
      ),
    );
  }

  /**
   * Tests setRouteOptions() with a controller with a non entity argument.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   *
   * @dataProvider providerTestSetRouteOptionsWithStandardRouteWithArgument
   */
  public function testSetRouteOptionsWithStandardRouteWithArgument($controller) {
    $route = new Route('/example/{argument}', array(
      '_controller' => $controller,
      'argument' => 'test',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithStandardRouteWithArgument.
   */
  public function providerTestSetRouteOptionsWithStandardRouteWithArgument() {
    return array(
      array(
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
      ),
      array(
        'test_function_controller_with_argument',
      ),
    );
  }

  /**
   * Tests setRouteOptions() with a _content default.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   *
   * @dataProvider providerTestSetRouteOptionsWithContentController
   */
  public function testSetRouteOptionsWithContentController($controller) {
    $route = new Route('/example/{argument}', array(
      '_controller' => $controller,
      'argument' => 'test',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithContentController.
   */
  public function providerTestSetRouteOptionsWithContentController() {
    return array(
      array(
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerMethodWithArgument',
      ),
      array(
        'test_function_controller_with_argument',
      ),
    );
  }

  /**
   * Tests setRouteOptions() with an entity type parameter.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   *
   * @dataProvider providerTestSetRouteOptionsWithEntityTypeNoUpcasting
   */
  public function testSetRouteOptionsWithEntityTypeNoUpcasting($controller) {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_controller' => $controller,
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Data provider for testSetRouteOptionsWithEntityTypeNoUpcasting.
   */
  public function providerTestSetRouteOptionsWithEntityTypeNoUpcasting() {
    return array(
      array(
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityNoUpcasting',
      ),
      array(
        'test_function_controller_no_upcasting',
      ),
    );
  }

  /**
   * Tests setRouteOptions() with an entity type parameter, upcasting.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   *
   * @dataProvider providerTestSetRouteOptionsWithEntityTypeUpcasting
   */
  public function testSetRouteOptionsWithEntityTypeUpcasting($controller) {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_controller' => $controller,
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals(array(
      'entity_test' => array(
        'type' => 'entity:entity_test',
      ),
    ), $parameters);
  }

  /**
   * Data provider for testSetRouteOptionsWithEntityTypeUpcasting.
   */
  public function providerTestSetRouteOptionsWithEntityTypeUpcasting() {
    return array(
      array(
        'Drupal\\Tests\\Core\\Entity\\BasicControllerClass::exampleControllerWithEntityUpcasting',
      ),
      array(
        'test_function_controller_entity_upcasting',
      ),
    );
  }

  /**
   * Tests setRouteOptions() with an entity type parameter form.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   */
  public function testSetRouteOptionsWithEntityFormUpcasting() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_form' => 'Drupal\\Tests\\Core\\Entity\\BasicForm',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals(array(
      'entity_test' => array(
        'type' => 'entity:entity_test',
      ),
    ), $parameters);
  }

  /**
   * Tests setRouteOptions() with entity form upcasting, no create method.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   */
  public function testSetRouteOptionsWithEntityUpcastingNoCreate() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoContainerInjectionInterface',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals(array(
      'entity_test' => array(
        'type' => 'entity:entity_test',
      ),
    ), $parameters);
  }

  /**
   * Tests setRouteOptions() with an form parameter without interface.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   */
  public function testSetRouteOptionsWithEntityFormNoUpcasting() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_form' => 'Drupal\\Tests\\Core\\Entity\\BasicFormNoUpcasting',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $this
      ->assertEmpty($route
      ->getOption('parameters'));
  }

  /**
   * Tests setRouteOptions() with an _entity_view route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityViewRouteAndManualParameters() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{foo}', array(
      '_entity_view' => 'entity_test.view',
    ), array(), array(
      'parameters' => array(
        'foo' => array(
          'type' => 'entity:entity_test',
        ),
      ),
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals(array(
      'foo' => array(
        'type' => 'entity:entity_test',
      ),
    ), $parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_view route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityViewRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_entity_view' => 'entity_test.view',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals(array(
      'entity_test' => array(
        'type' => 'entity:entity_test',
      ),
    ), $parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_list route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityListRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_entity_list' => 'entity_test',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertNull($parameters);
  }

  /**
   * Tests setRouteOptions() with an _entity_form route.
   *
   * @covers ::setRouteOptions
   * @covers ::getControllerClass
   * @covers ::getEntityTypes
   * @covers ::setParametersFromReflection
   * @covers ::setParametersFromEntityInformation
   */
  public function testSetRouteOptionsWithEntityFormRoute() {
    $this
      ->setupEntityTypes();
    $route = new Route('/example/{entity_test}', array(
      '_entity_form' => 'entity_test.edit',
    ));
    $defaults = $route
      ->getDefaults();
    $this->entityResolverManager
      ->setRouteOptions($route);
    $this
      ->assertEquals($defaults, $route
      ->getDefaults());
    $parameters = $route
      ->getOption('parameters');
    $this
      ->assertEquals(array(
      'entity_test' => array(
        'type' => 'entity:entity_test',
      ),
    ), $parameters);
  }

  /**
   * Creates the entity manager mock returning entity type objects.
   */
  protected function setupEntityTypes() {
    $definition = $this
      ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $definition
      ->expects($this
      ->any())
      ->method('getClass')
      ->will($this
      ->returnValue('Drupal\\Tests\\Core\\Entity\\SimpleTestEntity'));
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->will($this
      ->returnValue(array(
      'entity_test' => $definition,
    )));
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->will($this
      ->returnCallback(function ($entity_type) use ($definition) {
      if ($entity_type == 'entity_test') {
        return $definition;
      }
      else {
        return NULL;
      }
    }));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityResolverManagerTest::$classResolver protected property The mocked class resolver.
EntityResolverManagerTest::$container protected property The mocked dependency injection container.
EntityResolverManagerTest::$entityManager protected property The mocked entity manager.
EntityResolverManagerTest::$entityResolverManager protected property The tested entity resolver manager.
EntityResolverManagerTest::providerTestSetRouteOptionsWithContentController public function Data provider for testSetRouteOptionsWithContentController.
EntityResolverManagerTest::providerTestSetRouteOptionsWithEntityTypeNoUpcasting public function Data provider for testSetRouteOptionsWithEntityTypeNoUpcasting.
EntityResolverManagerTest::providerTestSetRouteOptionsWithEntityTypeUpcasting public function Data provider for testSetRouteOptionsWithEntityTypeUpcasting.
EntityResolverManagerTest::providerTestSetRouteOptionsWithStandardRoute public function Data provider for testSetRouteOptionsWithStandardRoute.
EntityResolverManagerTest::providerTestSetRouteOptionsWithStandardRouteWithArgument public function Data provider for testSetRouteOptionsWithStandardRouteWithArgument.
EntityResolverManagerTest::setUp protected function @covers ::__construct Overrides UnitTestCase::setUp
EntityResolverManagerTest::setupEntityTypes protected function Creates the entity manager mock returning entity type objects.
EntityResolverManagerTest::testSetRouteOptionsWithContentController public function Tests setRouteOptions() with a _content default.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormNoUpcasting public function Tests setRouteOptions() with an form parameter without interface.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormRoute public function Tests setRouteOptions() with an _entity_form route.
EntityResolverManagerTest::testSetRouteOptionsWithEntityFormUpcasting public function Tests setRouteOptions() with an entity type parameter form.
EntityResolverManagerTest::testSetRouteOptionsWithEntityListRoute public function Tests setRouteOptions() with an _entity_list route.
EntityResolverManagerTest::testSetRouteOptionsWithEntityTypeNoUpcasting public function Tests setRouteOptions() with an entity type parameter.
EntityResolverManagerTest::testSetRouteOptionsWithEntityTypeUpcasting public function Tests setRouteOptions() with an entity type parameter, upcasting.
EntityResolverManagerTest::testSetRouteOptionsWithEntityUpcastingNoCreate public function Tests setRouteOptions() with entity form upcasting, no create method.
EntityResolverManagerTest::testSetRouteOptionsWithEntityViewRoute public function Tests setRouteOptions() with an _entity_view route.
EntityResolverManagerTest::testSetRouteOptionsWithEntityViewRouteAndManualParameters public function Tests setRouteOptions() with an _entity_view route.
EntityResolverManagerTest::testSetRouteOptionsWithStandardRoute public function Tests setRouteOptions() with no parameter.
EntityResolverManagerTest::testSetRouteOptionsWithStandardRouteWithArgument public function Tests setRouteOptions() with a controller with a non entity argument.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.