class ResourceIdentifierNormalizerTest in Drupal 9
Same name and namespace in other branches
- 8 core/modules/jsonapi/tests/src/Unit/Normalizer/ResourceIdentifierNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\ResourceIdentifierNormalizerTest
 
@coversDefaultClass \Drupal\jsonapi\Normalizer\ResourceIdentifierNormalizer @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\ResourceIdentifierNormalizerTest
 
 
Expanded class hierarchy of ResourceIdentifierNormalizerTest
File
- core/
modules/ jsonapi/ tests/ src/ Unit/ Normalizer/ ResourceIdentifierNormalizerTest.php, line 28  
Namespace
Drupal\Tests\jsonapi\Unit\NormalizerView source
class ResourceIdentifierNormalizerTest extends UnitTestCase {
  /**
   * The normalizer under test.
   *
   * @var \Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer
   */
  protected $normalizer;
  /**
   * The base resource type for testing.
   *
   * @var \Drupal\jsonapi\ResourceType\ResourceType
   */
  protected $resourceType;
  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $target_resource_type = new ResourceType('lorem', 'dummy_bundle', NULL);
    $relationship_fields = [
      'field_dummy' => new ResourceTypeRelationship('field_dummy'),
      'field_dummy_single' => new ResourceTypeRelationship('field_dummy_single'),
    ];
    $this->resourceType = new ResourceType('fake_entity_type', 'dummy_bundle', NULL, FALSE, TRUE, TRUE, FALSE, $relationship_fields);
    $this->resourceType
      ->setRelatableResourceTypes([
      'field_dummy' => [
        $target_resource_type,
      ],
      'field_dummy_single' => [
        $target_resource_type,
      ],
    ]);
    $field_manager = $this
      ->prophesize(EntityFieldManagerInterface::class);
    $field_definition = $this
      ->prophesize(FieldConfig::class);
    $item_definition = $this
      ->prophesize(FieldItemDataDefinition::class);
    $item_definition
      ->getMainPropertyName()
      ->willReturn('bunny');
    $item_definition
      ->getSetting('target_type')
      ->willReturn('fake_entity_type');
    $item_definition
      ->getSetting('handler_settings')
      ->willReturn([
      'target_bundles' => [
        'dummy_bundle',
      ],
    ]);
    $field_definition
      ->getItemDefinition()
      ->willReturn($item_definition
      ->reveal());
    $storage_definition = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $storage_definition
      ->isMultiple()
      ->willReturn(TRUE);
    $field_definition
      ->getFieldStorageDefinition()
      ->willReturn($storage_definition
      ->reveal());
    $field_definition2 = $this
      ->prophesize(FieldConfig::class);
    $field_definition2
      ->getItemDefinition()
      ->willReturn($item_definition
      ->reveal());
    $storage_definition2 = $this
      ->prophesize(FieldStorageDefinitionInterface::class);
    $storage_definition2
      ->isMultiple()
      ->willReturn(FALSE);
    $field_definition2
      ->getFieldStorageDefinition()
      ->willReturn($storage_definition2
      ->reveal());
    $field_manager
      ->getFieldDefinitions('fake_entity_type', 'dummy_bundle')
      ->willReturn([
      'field_dummy' => $field_definition
        ->reveal(),
      'field_dummy_single' => $field_definition2
        ->reveal(),
    ]);
    $plugin_manager = $this
      ->prophesize(FieldTypePluginManagerInterface::class);
    $plugin_manager
      ->createFieldItemList(Argument::type(FieldableEntityInterface::class), Argument::type('string'), Argument::type('array'))
      ->willReturnArgument(2);
    $resource_type_repository = $this
      ->prophesize(ResourceTypeRepository::class);
    $resource_type_repository
      ->get('fake_entity_type', 'dummy_bundle')
      ->willReturn($this->resourceType);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->uuid()
      ->willReturn('4e6cb61d-4f04-437f-99fe-42c002393658');
    $entity
      ->id()
      ->willReturn(42);
    $entity_repository = $this
      ->prophesize(EntityRepositoryInterface::class);
    $entity_repository
      ->loadEntityByUuid('lorem', '4e6cb61d-4f04-437f-99fe-42c002393658')
      ->willReturn($entity
      ->reveal());
    $this->normalizer = new ResourceIdentifierNormalizer($field_manager
      ->reveal());
  }
  /**
   * @covers ::denormalize
   * @dataProvider denormalizeProvider
   */
  public function testDenormalize($input, $field_name, $expected) {
    $entity = $this
      ->prophesize(FieldableEntityInterface::class);
    $context = [
      'resource_type' => $this->resourceType,
      'related' => $field_name,
      'target_entity' => $entity
        ->reveal(),
    ];
    $denormalized = $this->normalizer
      ->denormalize($input, NULL, 'api_json', $context);
    $this
      ->assertEquals($expected, $denormalized);
  }
  /**
   * Data provider for the denormalize test.
   *
   * @return array
   *   The data for the test method.
   */
  public function denormalizeProvider() {
    return [
      [
        [
          'data' => [
            [
              'type' => 'lorem--dummy_bundle',
              'id' => '4e6cb61d-4f04-437f-99fe-42c002393658',
            ],
          ],
        ],
        'field_dummy',
        [
          new ResourceIdentifier('lorem--dummy_bundle', '4e6cb61d-4f04-437f-99fe-42c002393658'),
        ],
      ],
      [
        [
          'data' => [],
        ],
        'field_dummy',
        [],
      ],
      [
        [
          'data' => NULL,
        ],
        'field_dummy_single',
        [],
      ],
    ];
  }
  /**
   * @covers ::denormalize
   * @dataProvider denormalizeInvalidResourceProvider
   */
  public function testDenormalizeInvalidResource($data, $field_name) {
    $context = [
      'resource_type' => $this->resourceType,
      'related' => $field_name,
      'target_entity' => $this
        ->prophesize(FieldableEntityInterface::class)
        ->reveal(),
    ];
    $this
      ->expectException(BadRequestHttpException::class);
    $this->normalizer
      ->denormalize($data, NULL, 'api_json', $context);
  }
  /**
   * Data provider for the denormalize test.
   *
   * @return array
   *   The input data for the test method.
   */
  public function denormalizeInvalidResourceProvider() {
    return [
      [
        [
          'data' => [
            [
              'type' => 'invalid',
              'id' => '4e6cb61d-4f04-437f-99fe-42c002393658',
            ],
          ],
        ],
        'field_dummy',
      ],
      [
        [
          'data' => [
            'type' => 'lorem',
            'id' => '4e6cb61d-4f04-437f-99fe-42c002393658',
          ],
        ],
        'field_dummy',
      ],
      [
        [
          'data' => [
            [
              'type' => 'lorem',
              'id' => '4e6cb61d-4f04-437f-99fe-42c002393658',
            ],
          ],
        ],
        'field_dummy_single',
      ],
    ];
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            PhpUnitWarnings:: | 
                  private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
| 
            PhpUnitWarnings:: | 
                  public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
| 
            ResourceIdentifierNormalizerTest:: | 
                  protected | property | The normalizer under test. | |
| 
            ResourceIdentifierNormalizerTest:: | 
                  protected | property | The base resource type for testing. | |
| 
            ResourceIdentifierNormalizerTest:: | 
                  public | function | Data provider for the denormalize test. | |
| 
            ResourceIdentifierNormalizerTest:: | 
                  public | function | Data provider for the denormalize test. | |
| 
            ResourceIdentifierNormalizerTest:: | 
                  public | function | 
            Overrides UnitTestCase:: | 
                  |
| 
            ResourceIdentifierNormalizerTest:: | 
                  public | function | @covers ::denormalize @dataProvider denormalizeProvider | |
| 
            ResourceIdentifierNormalizerTest:: | 
                  public | function | @covers ::denormalize @dataProvider denormalizeInvalidResourceProvider | |
| 
            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 |