function EntityNormalizerTest::testNormalize

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

Tests the normalize() method.

@covers ::normalize

File

core/modules/serialization/tests/src/Unit/Normalizer/EntityNormalizerTest.php, line 75

Class

EntityNormalizerTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21serialization%21src%21Normalizer%21EntityNormalizer.php/class/EntityNormalizer/9" title="Normalizes/denormalizes Drupal entity objects into an array structure." class="local">\Drupal\serialization\Normalizer\EntityNormalizer</a> @group serialization

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testNormalize() {
    $list_item_1 = $this->createMock('Drupal\\Core\\TypedData\\TypedDataInterface');
    $list_item_2 = $this->createMock('Drupal\\Core\\TypedData\\TypedDataInterface');
    $definitions = [
        'field_1' => $list_item_1,
        'field_2' => $list_item_2,
    ];
    $content_entity = $this->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
        ->disableOriginalConstructor()
        ->onlyMethods([
        'getFields',
    ])
        ->getMockForAbstractClass();
    $content_entity->expects($this->once())
        ->method('getFields')
        ->willReturn($definitions);
    $serializer = $this->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
        ->disableOriginalConstructor()
        ->onlyMethods([
        'normalize',
    ])
        ->getMock();
    $serializer->expects($this->exactly(2))
        ->method('normalize')
        ->withConsecutive([
        $list_item_1,
        'test_format',
    ], [
        $list_item_2,
        'test_format',
    ]);
    $this->entityNormalizer
        ->setSerializer($serializer);
    $this->entityNormalizer
        ->normalize($content_entity, 'test_format');
}

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