You are here

class EntityReferenceFieldItemNormalizerTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest

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

Hierarchy

Expanded class hierarchy of EntityReferenceFieldItemNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php, line 22
Contains \Drupal\Tests\serialization\Unit\Normalizer\EntityReferenceFieldItemNormalizerTest.

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {

  /**
   * The mock serializer.
   *
   * @var \Symfony\Component\Serializer\SerializerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $serializer;

  /**
   * The normalizer under test.
   *
   * @var \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer
   */
  protected $normalizer;

  /**
   * The mock field item.
   *
   * @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $fieldItem;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->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_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',
      ],
      '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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceFieldItemNormalizerTest::$fieldItem protected property The mock field item.
EntityReferenceFieldItemNormalizerTest::$normalizer protected property The normalizer under test.
EntityReferenceFieldItemNormalizerTest::$serializer protected property The mock serializer.
EntityReferenceFieldItemNormalizerTest::setUp public function Overrides UnitTestCase::setUp
EntityReferenceFieldItemNormalizerTest::testNormalize public function @covers ::normalize
EntityReferenceFieldItemNormalizerTest::testNormalizeWithNoEntity public function @covers ::normalize
EntityReferenceFieldItemNormalizerTest::testSupportsNormalization public function @covers ::supportsNormalization
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.