You are here

public function EntityNormalizerTest::testDenormalizeWithInvalidBundle in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/serialization/tests/src/Unit/Normalizer/EntityNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\EntityNormalizerTest::testDenormalizeWithInvalidBundle()

Tests the denormalize method with a bundle property.

@expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException

@covers ::denormalize

File

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

Class

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

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testDenormalizeWithInvalidBundle() {
  $test_data = [
    'key_1' => 'value_1',
    'key_2' => 'value_2',
    'test_type' => [
      [
        'name' => 'test_bundle',
      ],
    ],
  ];
  $entity_type = $this
    ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
  $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('getBundleEntityType')
    ->will($this
    ->returnValue('test_bundle'));
  $entity_type_storage_definition = $this
    ->getmock('Drupal\\Core\\Field\\FieldStorageDefinitionInterface');
  $entity_type_storage_definition
    ->expects($this
    ->once())
    ->method('getMainPropertyName')
    ->will($this
    ->returnValue('name'));
  $entity_type_definition = $this
    ->getMock('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->entityManager
    ->expects($this
    ->at(0))
    ->method('getDefinition')
    ->with('test')
    ->will($this
    ->returnValue($entity_type));
  $this->entityManager
    ->expects($this
    ->at(1))
    ->method('getBaseFieldDefinitions')
    ->with('test')
    ->will($this
    ->returnValue($base_definitions));
  $entity_query_mock = $this
    ->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface');
  $entity_query_mock
    ->expects($this
    ->once())
    ->method('execute')
    ->will($this
    ->returnValue([
    'test_bundle_other' => 'test_bundle_other',
  ]));
  $entity_type_storage = $this
    ->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $entity_type_storage
    ->expects($this
    ->once())
    ->method('getQuery')
    ->will($this
    ->returnValue($entity_query_mock));
  $this->entityManager
    ->expects($this
    ->at(2))
    ->method('getStorage')
    ->with('test_bundle')
    ->will($this
    ->returnValue($entity_type_storage));
  $this->entityNormalizer
    ->denormalize($test_data, 'Drupal\\Core\\Entity\\ContentEntityBase', NULL, [
    'entity_type' => 'test',
  ]);
}