+ /**
+ * @covers ::denormalize
+ */
+ public function testDenormalizeWithTypeAndUuid() {
+ $data = [
+ 'target_id' => ['value' => 'test'],
+ 'target_type' => 'test_type',
+ 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
+ ];
+
+ $entity = $this->prophesize(FieldableEntityInterface::class);
+ $entity->id()
+ ->willReturn('test')
+ ->shouldBeCalled();
+ $this->entityRepository
+ ->loadEntityByUuid($data['target_type'], $data['target_uuid'])
+ ->willReturn($entity)
+ ->shouldBeCalled();
+
+ $this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
+
+ $this->assertDenormalize($data);
+ }
+
+ /**
+ * @covers ::denormalize
+ */
+ public function testDenormalizeWithUuidWithoutType() {
+ $data = [
+ 'target_id' => ['value' => 'test'],
+ 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
+ ];
+
+ $entity = $this->prophesize(FieldableEntityInterface::class);
+ $entity->id()
+ ->willReturn('test')
+ ->shouldBeCalled();
+ $this->entityRepository
+ ->loadEntityByUuid('test_type', $data['target_uuid'])
+ ->willReturn($entity)
+ ->shouldBeCalled();
+
+ $this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
+
+ $this->assertDenormalize($data);
+ }
+
+ /**
+ * @covers ::denormalize
+ */
+ public function testDenormalizeWithUuidWithIncorrectType() {
+ $this->setExpectedException(UnexpectedValueException::class, 'The field "field_reference" property "target_type" must be set to "test_type" or omitted.');
+
+ $data = [
+ 'target_id' => ['value' => 'test'],
+ 'target_type' => 'wrong_type',
+ 'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
+ ];
+
+ $this->fieldDefinition
+ ->getName()
+ ->willReturn('field_reference')
+ ->shouldBeCalled();
+
+ $this->assertDenormalize($data);
+ }
+
+ /**
+ * @covers ::denormalize
+ */
+ public function testDenormalizeWithTypeWithIncorrectUuid() {
+ $this->setExpectedException(InvalidArgumentException::class, 'No "test_type" entity found with UUID "unique-but-none-non-existent" for field "field_reference"');
+
+ $data = [
+ 'target_id' => ['value' => 'test'],
+ 'target_type' => 'test_type',
+ 'target_uuid' => 'unique-but-none-non-existent',
+ ];
+ $this->entityRepository
+ ->loadEntityByUuid($data['target_type'], $data['target_uuid'])
+ ->willReturn(NULL)
+ ->shouldBeCalled();
+ $this->fieldItem
+ ->getName()
+ ->willReturn('field_reference')
+ ->shouldBeCalled();
+
+ $this->assertDenormalize($data);
+ }
+
+ /**
+ * @covers ::denormalize
+ */
+ public function testDenormalizeWithEmtpyUuid() {
+ $this->setExpectedException(InvalidArgumentException::class, 'If provided "target_uuid" cannot be empty for field "test_type".');
+
+ $data = [
+ 'target_id' => ['value' => 'test'],
+ 'target_type' => 'test_type',
+ 'target_uuid' => '',
+ ];
+ $this->fieldItem
+ ->getName()
+ ->willReturn('field_reference')
+ ->shouldBeCalled();
+
+ $this->assertDenormalize($data);
+ }
+
+ /**
+ * @covers ::denormalize
+ */
+ public function testDenormalizeWithId() {
+ $data = [
+ 'target_id' => ['value' => 'test'],
+ ];
+ $this->fieldItem->setValue($data)->shouldBeCalled();
+
+ $this->assertDenormalize($data);
+ }
+
+ /**
+ * Asserts denormalization process is correct for give data.
+ *
+ * @param array $data
+ * The data to denormalize.
+ */
+ protected function assertDenormalize(array $data) {
+ $this->fieldItem->getParent()
+ ->willReturn($this->prophesize(FieldItemListInterface::class)->reveal());
+ $this->fieldItem->getFieldDefinition()->willReturn($this->fieldDefinition->reveal());
+ if (!empty($data['target_uuid'])) {
+ $this->fieldDefinition
+ ->getSetting('target_type')
+ ->willReturn('test_type')
+ ->shouldBeCalled();
+ }
+
+ $context = ['target_instance' => $this->fieldItem->reveal()];
+ $denormalized = $this->normalizer->denormalize($data, EntityReferenceItem::class, 'json', $context);
+ $this->assertSame($context['target_instance'], $denormalized);
+ }
+