You are here

public function JsonApiDocumentTopLevelNormalizerTest::setUp in JSON:API 8.2

Same name in this branch
  1. 8.2 tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::setUp()
  2. 8.2 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::setUp()
Same name and namespace in other branches
  1. 8 tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::setUp()

Overrides UnitTestCase::setUp

File

tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 38

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer

Code

public function setUp() {
  $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());
}