You are here

class EntityConverterTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityConverterTest

@coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter @group ParamConverter @group Entity

Hierarchy

Expanded class hierarchy of EntityConverterTest

File

core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php, line 19
Contains \Drupal\Tests\Core\ParamConverter\EntityConverterTest.

Namespace

Drupal\Tests\Core\ParamConverter
View source
class EntityConverterTest extends UnitTestCase {

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

  /**
   * The tested entity converter.
   *
   * @var \Drupal\Core\ParamConverter\EntityConverter
   */
  protected $entityConverter;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->entityManager = $this
      ->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
    $this->entityConverter = new EntityConverter($this->entityManager);
  }

  /**
   * Tests the applies() method.
   *
   * @dataProvider providerTestApplies
   *
   * @covers ::applies
   */
  public function testApplies(array $definition, $name, Route $route, $applies) {
    $this->entityManager
      ->expects($this
      ->any())
      ->method('hasDefinition')
      ->willReturnCallback(function ($entity_type) {
      return 'entity_test' == $entity_type;
    });
    $this
      ->assertEquals($applies, $this->entityConverter
      ->applies($definition, $name, $route));
  }

  /**
   * Provides test data for testApplies()
   */
  public function providerTestApplies() {
    $data = [];
    $data[] = [
      [
        'type' => 'entity:foo',
      ],
      'foo',
      new Route('/test/{foo}/bar'),
      FALSE,
    ];
    $data[] = [
      [
        'type' => 'entity:entity_test',
      ],
      'foo',
      new Route('/test/{foo}/bar'),
      TRUE,
    ];
    $data[] = [
      [
        'type' => 'entity:entity_test',
      ],
      'entity_test',
      new Route('/test/{entity_test}/bar'),
      TRUE,
    ];
    $data[] = [
      [
        'type' => 'entity:{entity_test}',
      ],
      'entity_test',
      new Route('/test/{entity_test}/bar'),
      FALSE,
    ];
    $data[] = [
      [
        'type' => 'entity:{entity_type}',
      ],
      'entity_test',
      new Route('/test/{entity_type}/{entity_test}/bar'),
      TRUE,
    ];
    $data[] = [
      [
        'type' => 'foo',
      ],
      'entity_test',
      new Route('/test/{entity_type}/{entity_test}/bar'),
      FALSE,
    ];
    return $data;
  }

  /**
   * Tests the convert() method.
   *
   * @dataProvider providerTestConvert
   *
   * @covers ::convert
   */
  public function testConvert($value, array $definition, array $defaults, $expected_result) {
    $entity_storage = $this
      ->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $this->entityManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('entity_test')
      ->willReturn($entity_storage);
    $entity_storage
      ->expects($this
      ->any())
      ->method('load')
      ->willReturnMap([
      [
        'valid_id',
        (object) [
          'id' => 'valid_id',
        ],
      ],
      [
        'invalid_id',
        NULL,
      ],
    ]);
    $this
      ->assertEquals($expected_result, $this->entityConverter
      ->convert($value, $definition, 'foo', $defaults));
  }

  /**
   * Provides test data for testConvert
   */
  public function providerTestConvert() {
    $data = [];

    // Existing entity type.
    $data[] = [
      'valid_id',
      [
        'type' => 'entity:entity_test',
      ],
      [
        'foo' => 'valid_id',
      ],
      (object) [
        'id' => 'valid_id',
      ],
    ];

    // Invalid ID.
    $data[] = [
      'invalid_id',
      [
        'type' => 'entity:entity_test',
      ],
      [
        'foo' => 'invalid_id',
      ],
      NULL,
    ];

    // Entity type placeholder.
    $data[] = [
      'valid_id',
      [
        'type' => 'entity:{entity_type}',
      ],
      [
        'foo' => 'valid_id',
        'entity_type' => 'entity_test',
      ],
      (object) [
        'id' => 'valid_id',
      ],
    ];
    return $data;
  }

  /**
   * Tests the convert() method with an invalid entity type.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function testConvertWithInvalidEntityType() {
    $this->entityManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('invalid_id')
      ->willThrowException(new \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException('invalid_id'));
    $this->entityConverter
      ->convert('id', [
      'type' => 'entity:invalid_id',
    ], 'foo', [
      'foo' => 'id',
    ]);
  }

  /**
   * Tests the convert() method with an invalid dynamic entity type.
   *
   * @expectedException \Drupal\Core\ParamConverter\ParamNotConvertedException
   * @expectedExceptionMessage The "foo" parameter was not converted because the "invalid_id" parameter is missing
   */
  public function testConvertWithInvalidDynamicEntityType() {
    $this->entityConverter
      ->convert('id', [
      'type' => 'entity:{invalid_id}',
    ], 'foo', [
      'foo' => 'id',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityConverterTest::$entityConverter protected property The tested entity converter.
EntityConverterTest::$entityManager protected property The mocked entity manager.
EntityConverterTest::providerTestApplies public function Provides test data for testApplies()
EntityConverterTest::providerTestConvert public function Provides test data for testConvert
EntityConverterTest::setUp protected function Overrides UnitTestCase::setUp
EntityConverterTest::testApplies public function Tests the applies() method.
EntityConverterTest::testConvert public function Tests the convert() method.
EntityConverterTest::testConvertWithInvalidDynamicEntityType public function Tests the convert() method with an invalid dynamic entity type.
EntityConverterTest::testConvertWithInvalidEntityType public function Tests the convert() method with an invalid entity type.
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.