function EntityReferenceFieldItemNormalizerTest::testNormalize

Same name in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest::testNormalize()
  2. 10 core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest::testNormalize()
  3. 11.x core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest::testNormalize()

@covers ::normalize

File

core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php, line 112

Class

EntityReferenceFieldItemNormalizerTest
@coversDefaultClass \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer @group serialization

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testNormalize() {
    $test_url = '/test/100';
    $generated_url = (new GeneratedUrl())->setGeneratedUrl($test_url);
    $url = $this->prophesize(Url::class);
    $url->toString(TRUE)
        ->willReturn($generated_url);
    $entity = $this->prophesize(EntityInterface::class);
    $entity->hasLinkTemplate('canonical')
        ->willReturn(TRUE);
    $entity->isNew()
        ->willReturn(FALSE)
        ->shouldBeCalled();
    $entity->toUrl('canonical')
        ->willReturn($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();
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getSetting('target_type')
        ->willReturn('test_type');
    $this->fieldItem
        ->getFieldDefinition()
        ->willReturn($field_definition->reveal());
    $this->fieldItem
        ->get('entity')
        ->willReturn($entity_reference)
        ->shouldBeCalled();
    $this->fieldItem
        ->getProperties(TRUE)
        ->willReturn([
        'target_id' => $this->getTypedDataProperty(FALSE),
    ])
        ->shouldBeCalled();
    $normalized = $this->normalizer
        ->normalize($this->fieldItem
        ->reveal());
    $expected = [
        'target_id' => 'test',
        'target_type' => 'test_type',
        'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
        'url' => $test_url,
    ];
    $this->assertSame($expected, $normalized);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.