function EntitySerializationTest::testDenormalizeCustomSerializedItem

Same name in other branches
  1. 8.9.x core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testDenormalizeCustomSerializedItem()
  2. 10 core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testDenormalizeCustomSerializedItem()
  3. 11.x core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testDenormalizeCustomSerializedItem()

Tests normalizing/denormalizing custom serialized columns.

File

core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php, line 303

Class

EntitySerializationTest
Tests that entities can be serialized to supported core formats.

Namespace

Drupal\Tests\serialization\Kernel

Code

public function testDenormalizeCustomSerializedItem() {
    $entity = EntitySerializedField::create([
        'serialized_text' => serialize([
            'Hello world!',
        ]),
    ]);
    $normalized = $this->serializer
        ->normalize($entity);
    $this->assertEquals([
        'Hello world!',
    ], $normalized['serialized_text'][0]['value']);
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized_text" field (field item class: Drupal\\entity_test\\Plugin\\Field\\FieldType\\SerializedPropertyItem).');
    $this->serializer
        ->denormalize([
        'serialized_text' => [
            [
                'value' => 'boo',
            ],
        ],
        'type' => 'entity_test_serialized_field',
    ], EntitySerializedField::class);
}

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