function EntityNormalizerTest::testDenormalizeWithNoBundle

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

Tests the denormalize method with no bundle defined.

@covers ::denormalize

File

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

Class

EntityNormalizerTest
@coversDefaultClass \Drupal\serialization\Normalizer\EntityNormalizer @group serialization

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testDenormalizeWithNoBundle() {
    $test_data = [
        'key_1' => 'value_1',
        'key_2' => 'value_2',
    ];
    $entity_type = $this->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $entity_type->expects($this->once())
        ->method('entityClassImplements')
        ->with(FieldableEntityInterface::class)
        ->willReturn(TRUE);
    $entity_type->expects($this->once())
        ->method('hasKey')
        ->with('bundle')
        ->willReturn(FALSE);
    $entity_type->expects($this->never())
        ->method('getKey');
    $this->entityTypeManager
        ->expects($this->once())
        ->method('getDefinition')
        ->with('test')
        ->willReturn($entity_type);
    $key_1 = $this->createMock(FieldItemListInterface::class);
    $key_2 = $this->createMock(FieldItemListInterface::class);
    $entity = $this->createMock(FieldableEntityInterface::class);
    $entity->expects($this->exactly(2))
        ->method('get')
        ->willReturnMap([
        [
            'key_1',
            $key_1,
        ],
        [
            'key_2',
            $key_2,
        ],
    ]);
    $storage = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $storage->expects($this->once())
        ->method('create')
        ->with([])
        ->willReturn($entity);
    $this->entityTypeManager
        ->expects($this->once())
        ->method('getStorage')
        ->with('test')
        ->willReturn($storage);
    $this->entityFieldManager
        ->expects($this->never())
        ->method('getBaseFieldDefinitions');
    // Setup expectations for the serializer. This will be called for each field
    // item.
    $serializer = $this->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
        ->disableOriginalConstructor()
        ->onlyMethods([
        'denormalize',
    ])
        ->getMock();
    $serializer->expects($this->exactly(2))
        ->method('denormalize')
        ->withConsecutive([
        'value_1',
        get_class($key_1),
        NULL,
        [
            'target_instance' => $key_1,
            'entity_type' => 'test',
        ],
    ], [
        'value_2',
        get_class($key_2),
        NULL,
        [
            'target_instance' => $key_2,
            'entity_type' => 'test',
        ],
    ]);
    $this->entityNormalizer
        ->setSerializer($serializer);
    $this->assertNotNull($this->entityNormalizer
        ->denormalize($test_data, 'Drupal\\Core\\Entity\\ContentEntityBase', NULL, [
        'entity_type' => 'test',
    ]));
}

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