normalizer = new EntityReferenceFieldItemNormalizer(); $this->serializer = $this->prophesize(Serializer::class); // Set up the serializer to return an entity property. $this->serializer->normalize(Argument::cetera()) ->willReturn(['value' => 'test']); $this->normalizer->setSerializer($this->serializer->reveal()); $this->fieldItem = $this->prophesize(EntityReferenceItem::class); $this->fieldItem->getIterator() ->willReturn(new \ArrayIterator(['target_id' => []])); } /** * @covers ::supportsNormalization */ public function testSupportsNormalization() { $this->assertTrue($this->normalizer->supportsNormalization($this->fieldItem->reveal())); $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); } /** * @covers ::normalize */ public function testNormalize() { $test_url = '/test/100'; $entity = $this->prophesize(EntityInterface::class); $entity->url('canonical') ->willReturn($test_url) ->shouldBeCalled(); $entity->uuid() ->willReturn('080e3add-f9d5-41ac-9821-eea55b7b42fb') ->shouldBeCalled(); $entity->getEntityTypeId() ->willReturn('test_type') ->shouldBeCalled(); $entity_reference = $this->prophesize(TypedDataInterface::class); $entity_reference->getValue() ->willReturn($entity->reveal()) ->shouldBeCalled(); $this->fieldItem->get('entity') ->willReturn($entity_reference) ->shouldBeCalled(); $normalized = $this->normalizer->normalize($this->fieldItem->reveal()); $expected = [ 'target_id' => ['value' => 'test'], 'target_type' => 'test_type', 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb', 'url' => $test_url, ]; $this->assertSame($expected, $normalized); } /** * @covers ::normalize */ public function testNormalizeWithNoEntity() { $entity_reference = $this->prophesize(TypedDataInterface::class); $entity_reference->getValue() ->willReturn(NULL) ->shouldBeCalled(); $this->fieldItem->get('entity') ->willReturn($entity_reference->reveal()) ->shouldBeCalled(); $normalized = $this->normalizer->normalize($this->fieldItem->reveal()); $expected = [ 'target_id' => ['value' => 'test'], ]; $this->assertSame($expected, $normalized); } }