function EntityNormalizerTest::testDenormalizeWithValidBundle

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

Tests the denormalize method with a bundle property.

@covers ::denormalize

File

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

Class

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

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testDenormalizeWithValidBundle() {
    $test_data = [
        'key_1' => 'value_1',
        'key_2' => 'value_2',
        'test_type' => [
            [
                'name' => 'test_bundle',
            ],
        ],
    ];
    $entity_type = $this->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $entity_type->expects($this->once())
        ->method('id')
        ->willReturn('test');
    $entity_type->expects($this->once())
        ->method('hasKey')
        ->with('bundle')
        ->will($this->returnValue(TRUE));
    $entity_type->expects($this->once())
        ->method('getKey')
        ->with('bundle')
        ->will($this->returnValue('test_type'));
    $entity_type->expects($this->once())
        ->method('entityClassImplements')
        ->with(FieldableEntityInterface::class)
        ->willReturn(TRUE);
    $entity_type->expects($this->once())
        ->method('getBundleEntityType')
        ->will($this->returnValue('test_bundle'));
    $entity_type_storage_definition = $this->createMock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface');
    $entity_type_storage_definition->expects($this->once())
        ->method('getMainPropertyName')
        ->will($this->returnValue('name'));
    $entity_type_definition = $this->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
    $entity_type_definition->expects($this->once())
        ->method('getFieldStorageDefinition')
        ->will($this->returnValue($entity_type_storage_definition));
    $base_definitions = [
        'test_type' => $entity_type_definition,
    ];
    $this->entityTypeManager
        ->expects($this->at(0))
        ->method('getDefinition')
        ->with('test')
        ->will($this->returnValue($entity_type));
    $this->entityFieldManager
        ->expects($this->at(0))
        ->method('getBaseFieldDefinitions')
        ->with('test')
        ->will($this->returnValue($base_definitions));
    $entity_query_mock = $this->createMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
    $entity_query_mock->expects($this->once())
        ->method('execute')
        ->will($this->returnValue([
        'test_bundle' => 'test_bundle',
    ]));
    $entity_type_storage = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $entity_type_storage->expects($this->once())
        ->method('getQuery')
        ->will($this->returnValue($entity_query_mock));
    $this->entityTypeManager
        ->expects($this->at(1))
        ->method('getStorage')
        ->with('test_bundle')
        ->will($this->returnValue($entity_type_storage));
    $key_1 = $this->createMock(FieldItemListInterface::class);
    $key_2 = $this->createMock(FieldItemListInterface::class);
    $entity = $this->createMock(FieldableEntityInterface::class);
    $entity->expects($this->at(0))
        ->method('get')
        ->with('key_1')
        ->willReturn($key_1);
    $entity->expects($this->at(1))
        ->method('get')
        ->with('key_2')
        ->willReturn($key_2);
    $storage = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    // Create should only be called with the bundle property at first.
    $expected_test_data = [
        'test_type' => 'test_bundle',
    ];
    $storage->expects($this->once())
        ->method('create')
        ->with($expected_test_data)
        ->will($this->returnValue($entity));
    $this->entityTypeManager
        ->expects($this->at(2))
        ->method('getStorage')
        ->with('test')
        ->will($this->returnValue($storage));
    // Setup expectations for the serializer. This will be called for each field
    // item.
    $serializer = $this->getMockBuilder('Symfony\\Component\\Serializer\\Serializer')
        ->disableOriginalConstructor()
        ->setMethods([
        'denormalize',
    ])
        ->getMock();
    $serializer->expects($this->at(0))
        ->method('denormalize')
        ->with('value_1', get_class($key_1), NULL, [
        'target_instance' => $key_1,
        'entity_type' => 'test',
    ]);
    $serializer->expects($this->at(1))
        ->method('denormalize')
        ->with('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.