class EntityRevisionParamConverterTest in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityRevisionParamConverterTest
@coversDefaultClass \Drupal\Core\ParamConverter\EntityRevisionParamConverter @group entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\ParamConverter\EntityRevisionParamConverterTest
Expanded class hierarchy of EntityRevisionParamConverterTest
File
- core/
tests/ Drupal/ Tests/ Core/ ParamConverter/ EntityRevisionParamConverterTest.php, line 18
Namespace
Drupal\Tests\Core\ParamConverterView source
class EntityRevisionParamConverterTest extends UnitTestCase {
/**
* The tested entity revision param converter.
*
* @var \Drupal\Core\ParamConverter\EntityRevisionParamConverter
*/
protected $converter;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->converter = new EntityRevisionParamConverter($this
->prophesize(EntityTypeManagerInterface::class)
->reveal(), $this
->prophesize(EntityRepositoryInterface::class)
->reveal());
}
protected function getTestRoute() {
$route = new Route('/test/{test_revision}');
$route
->setOption('parameters', [
'test_revision' => [
'type' => 'entity_revision:test',
],
]);
return $route;
}
/**
* @covers ::applies
*/
public function testNonApplyingRoute() {
$route = new Route('/test');
$this
->assertFalse($this->converter
->applies([], 'test_revision', $route));
}
/**
* @covers ::applies
*/
public function testApplyingRoute() {
$route = $this
->getTestRoute();
$this
->assertTrue($this->converter
->applies($route
->getOption('parameters')['test_revision'], 'test_revision', $route));
}
/**
* Tests the convert() method.
*
* @dataProvider providerTestConvert
*
* @covers ::convert
*/
public function testConvert($value, array $definition, array $defaults, $expected_result) {
$storage = $this
->prophesize(EntityStorageInterface::class);
$storage
->loadRevision('valid_id')
->willReturn((object) [
'revision_id' => 'valid_id',
]);
$storage
->loadRevision('invalid_id')
->willReturn(NULL);
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getStorage('entity_test')
->willReturn($storage
->reveal());
$entity_repository = $this
->prophesize(EntityRepositoryInterface::class);
$converter = new EntityRevisionParamConverter($entity_type_manager
->reveal(), $entity_repository
->reveal());
$result = $converter
->convert($value, $definition, 'test_revision', $defaults);
$this
->assertEquals($expected_result, $result);
}
/**
* Provides test data for testConvert.
*/
public function providerTestConvert() {
$data = [];
// Existing entity type.
$data[] = [
'valid_id',
[
'type' => 'entity_revision:entity_test',
],
[
'test_revision' => 'valid_id',
],
(object) [
'revision_id' => 'valid_id',
],
];
// Invalid ID.
$data[] = [
'invalid_id',
[
'type' => 'entity_revision:entity_test',
],
[
'test_revision' => 'invalid_id',
],
NULL,
];
// Entity type placeholder.
$data[] = [
'valid_id',
[
'type' => 'entity_revision:{entity_type}',
],
[
'test_revision' => 'valid_id',
'entity_type' => 'entity_test',
],
(object) [
'revision_id' => 'valid_id',
],
];
return $data;
}
/**
* Tests the convert() method with an invalid entity type ID.
*
* @covers ::convert
*/
public function testConvertWithInvalidEntityType() {
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getStorage('invalid_entity_type_id')
->willThrow(new InvalidPluginDefinitionException('invalid_entity_type_id'));
$entity_repository = $this
->prophesize(EntityRepositoryInterface::class);
$converter = new EntityRevisionParamConverter($entity_type_manager
->reveal(), $entity_repository
->reveal());
$this
->expectException(InvalidPluginDefinitionException::class);
$converter
->convert('valid_id', [
'type' => 'entity_revision:invalid_entity_type_id',
], 'foo', [
'foo' => 'valid_id',
]);
}
/**
* Tests the convert() method with an invalid dynamic entity type ID.
*
* @covers ::convert
*/
public function testConvertWithInvalidType() {
$this
->expectException(ParamNotConvertedException::class);
$this
->expectExceptionMessage('The type definition "entity_revision_{entity_type_id}" is invalid. The expected format is "entity_revision:<entity_type_id>".');
$this->converter
->convert('valid_id', [
'type' => 'entity_revision_{entity_type_id}',
], 'foo', [
'foo' => 'valid_id',
]);
}
/**
* Tests the convert() method with an invalid dynamic entity type ID.
*
* @covers ::convert
*/
public function testConvertWithInvalidDynamicEntityType() {
$this
->expectException(ParamNotConvertedException::class);
$this
->expectExceptionMessage('The "foo" parameter was not converted because the "invalid_entity_type_id" parameter is missing.');
$this->converter
->convert('valid_id', [
'type' => 'entity_revision:{invalid_entity_type_id}',
], 'foo', [
'foo' => 'valid_id',
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityRevisionParamConverterTest:: |
protected | property | The tested entity revision param converter. | |
EntityRevisionParamConverterTest:: |
protected | function | ||
EntityRevisionParamConverterTest:: |
public | function | Provides test data for testConvert. | |
EntityRevisionParamConverterTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EntityRevisionParamConverterTest:: |
public | function | @covers ::applies | |
EntityRevisionParamConverterTest:: |
public | function | Tests the convert() method. | |
EntityRevisionParamConverterTest:: |
public | function | Tests the convert() method with an invalid dynamic entity type ID. | |
EntityRevisionParamConverterTest:: |
public | function | Tests the convert() method with an invalid entity type ID. | |
EntityRevisionParamConverterTest:: |
public | function | Tests the convert() method with an invalid dynamic entity type ID. | |
EntityRevisionParamConverterTest:: |
public | function | @covers ::applies | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed 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. | |
UnitTestCase:: |
public static | function |