class JsonApiDocumentTopLevelNormalizerTest in Drupal 9
Same name and namespace in other branches
- 8 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi
@internal
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest
Expanded class hierarchy of JsonApiDocumentTopLevelNormalizerTest
File
- core/
modules/ jsonapi/ tests/ src/ Unit/ Normalizer/ JsonApiDocumentTopLevelNormalizerTest.php, line 26
Namespace
Drupal\Tests\jsonapi\Unit\NormalizerView source
class JsonApiDocumentTopLevelNormalizerTest extends UnitTestCase {
/**
* The normalizer under test.
*
* @var \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer
*/
protected $normalizer;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$resource_type_repository = $this
->prophesize(ResourceTypeRepository::class);
$field_resolver = $this
->prophesize(FieldResolver::class);
$resource_type_repository
->getByTypeName(Argument::any())
->willReturn(new ResourceType('node', 'article', NULL));
$entity_storage = $this
->prophesize(EntityStorageInterface::class);
$self = $this;
$uuid_to_id = [
'76dd5c18-ea1b-4150-9e75-b21958a2b836' => 1,
'fcce1b61-258e-4054-ae36-244d25a9e04c' => 2,
];
$entity_storage
->loadByProperties(Argument::type('array'))
->will(function ($args) use ($self, $uuid_to_id) {
$result = [];
foreach ($args[0]['uuid'] as $uuid) {
$entity = $self
->prophesize(EntityInterface::class);
$entity
->uuid()
->willReturn($uuid);
$entity
->id()
->willReturn($uuid_to_id[$uuid]);
$result[$uuid] = $entity
->reveal();
}
return $result;
});
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager
->getStorage('node')
->willReturn($entity_storage
->reveal());
$entity_type = $this
->prophesize(EntityTypeInterface::class);
$entity_type
->getKey('uuid')
->willReturn('uuid');
$entity_type_manager
->getDefinition('node')
->willReturn($entity_type
->reveal());
$this->normalizer = new JsonApiDocumentTopLevelNormalizer($entity_type_manager
->reveal(), $resource_type_repository
->reveal(), $field_resolver
->reveal());
$serializer = $this
->prophesize(DenormalizerInterface::class);
$serializer
->willImplement(SerializerInterface::class);
$serializer
->denormalize(Argument::type('array'), Argument::type('string'), Argument::type('string'), Argument::type('array'))
->willReturnArgument(0);
$this->normalizer
->setSerializer($serializer
->reveal());
}
/**
* @covers ::denormalize
* @dataProvider denormalizeProvider
*/
public function testDenormalize($input, $expected) {
$resource_type = new ResourceType('node', 'article', FieldableEntityInterface::class);
$resource_type
->setRelatableResourceTypes([]);
$context = [
'resource_type' => $resource_type,
];
$denormalized = $this->normalizer
->denormalize($input, NULL, 'api_json', $context);
$this
->assertSame($expected, $denormalized);
}
/**
* Data provider for the denormalize test.
*
* @return array
* The data for the test method.
*/
public function denormalizeProvider() {
return [
[
[
'data' => [
'type' => 'lorem',
'id' => 'e1a613f6-f2b9-4e17-9d33-727eb6509d8b',
'attributes' => [
'title' => 'dummy_title',
],
],
],
[
'title' => 'dummy_title',
'uuid' => 'e1a613f6-f2b9-4e17-9d33-727eb6509d8b',
],
],
[
[
'data' => [
'type' => 'lorem',
'id' => '0676d1bf-55b3-4bbc-9fbc-3df10f4599d5',
'relationships' => [
'field_dummy' => [
'data' => [
'type' => 'node',
'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
],
],
],
],
],
[
'uuid' => '0676d1bf-55b3-4bbc-9fbc-3df10f4599d5',
'field_dummy' => [
[
'target_id' => 1,
],
],
],
],
[
[
'data' => [
'type' => 'lorem',
'id' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'relationships' => [
'field_dummy' => [
'data' => [
[
'type' => 'node',
'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
],
[
'type' => 'node',
'id' => 'fcce1b61-258e-4054-ae36-244d25a9e04c',
],
],
],
],
],
],
[
'uuid' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'field_dummy' => [
[
'target_id' => 1,
],
[
'target_id' => 2,
],
],
],
],
[
[
'data' => [
'type' => 'lorem',
'id' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'relationships' => [
'field_dummy' => [
'data' => [
[
'type' => 'node',
'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
'meta' => [
'foo' => 'bar',
],
],
[
'type' => 'node',
'id' => 'fcce1b61-258e-4054-ae36-244d25a9e04c',
],
],
],
],
],
],
[
'uuid' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
'field_dummy' => [
[
'target_id' => 1,
'foo' => 'bar',
],
[
'target_id' => 2,
],
],
],
],
];
}
/**
* Ensures only valid UUIDs can be specified.
*
* @param string $id
* The input UUID. May be invalid.
* @param bool $expect_exception
* Whether to expect an exception.
*
* @covers ::denormalize
* @dataProvider denormalizeUuidProvider
*/
public function testDenormalizeUuid($id, $expect_exception) {
$data['data'] = isset($id) ? [
'type' => 'node--article',
'id' => $id,
] : [
'type' => 'node--article',
];
if ($expect_exception) {
$this
->expectException(UnprocessableEntityHttpException::class);
$this
->expectExceptionMessage('IDs should be properly generated and formatted UUIDs as described in RFC 4122.');
}
$denormalized = $this->normalizer
->denormalize($data, NULL, 'api_json', [
'resource_type' => new ResourceType('node', 'article', FieldableEntityInterface::class),
]);
if (isset($id)) {
$this
->assertSame($id, $denormalized['uuid']);
}
else {
$this
->assertArrayNotHasKey('uuid', $denormalized);
}
}
/**
* Provides test cases for testDenormalizeUuid.
*/
public function denormalizeUuidProvider() {
return [
'valid' => [
'76dd5c18-ea1b-4150-9e75-b21958a2b836',
FALSE,
],
'missing' => [
NULL,
FALSE,
],
'invalid_empty' => [
'',
TRUE,
],
'invalid_alpha' => [
'invalid',
TRUE,
],
'invalid_numeric' => [
1234,
TRUE,
],
'invalid_alphanumeric' => [
'abc123',
TRUE,
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
JsonApiDocumentTopLevelNormalizerTest:: |
protected | property | The normalizer under test. | |
JsonApiDocumentTopLevelNormalizerTest:: |
public | function | Data provider for the denormalize test. | |
JsonApiDocumentTopLevelNormalizerTest:: |
public | function | Provides test cases for testDenormalizeUuid. | |
JsonApiDocumentTopLevelNormalizerTest:: |
public | function |
Overrides UnitTestCase:: |
|
JsonApiDocumentTopLevelNormalizerTest:: |
public | function | @covers ::denormalize @dataProvider denormalizeProvider | |
JsonApiDocumentTopLevelNormalizerTest:: |
public | function | Ensures only valid UUIDs can be specified. | |
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 |