5 * Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.
8 namespace Drupal\Tests\serialization\Unit\Normalizer;
10 use Drupal\Core\TypedData\ComplexDataInterface;
11 use Drupal\serialization\Normalizer\ComplexDataNormalizer;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\Serializer\Serializer;
16 * @coversDefaultClass \Drupal\serialization\Normalizer\ComplexDataNormalizer
17 * @group serialization
19 class ComplexDataNormalizerTest extends UnitTestCase {
21 use InternalTypedDataTestTrait;
28 const TEST_FORMAT = 'test_format';
31 * The Complex data normalizer under test.
33 * @var \Drupal\serialization\Normalizer\ComplexDataNormalizer
35 protected $normalizer;
40 protected function setUp() {
41 $this->normalizer = new ComplexDataNormalizer();
45 * @covers ::supportsNormalization
47 public function testSupportsNormalization() {
48 $complex_data = $this->prophesize(ComplexDataInterface::class)->reveal();
49 $this->assertTrue($this->normalizer->supportsNormalization($complex_data));
50 // Also test that an object not implementing ComplexDataInterface fails.
51 $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
55 * Test normalizing complex data.
59 public function testNormalizeComplexData() {
60 $serializer_prophecy = $this->prophesize(Serializer::class);
62 $non_internal_property = $this->getTypedDataProperty(FALSE);
64 $serializer_prophecy->normalize($non_internal_property, static::TEST_FORMAT, [])
65 ->willReturn('A-normalized')
68 $this->normalizer->setSerializer($serializer_prophecy->reveal());
70 $complex_data = $this->prophesize(ComplexDataInterface::class);
71 $complex_data->getProperties(TRUE)
73 'prop:a' => $non_internal_property,
74 'prop:internal' => $this->getTypedDataProperty(TRUE),
78 $normalized = $this->normalizer->normalize($complex_data->reveal(), static::TEST_FORMAT);
79 $this->assertEquals(['prop:a' => 'A-normalized'], $normalized);
83 * Test normalize() where $object does not implement ComplexDataInterface.
85 * Normalizers extending ComplexDataNormalizer may have a different supported
90 public function testNormalizeNonComplex() {
91 $normalizer = new TestExtendedNormalizer();
92 $serialization_context = ['test' => 'test'];
94 $serializer_prophecy = $this->prophesize(Serializer::class);
95 $serializer_prophecy->normalize('A', static::TEST_FORMAT, $serialization_context)
96 ->willReturn('A-normalized')
98 $serializer_prophecy->normalize('B', static::TEST_FORMAT, $serialization_context)
99 ->willReturn('B-normalized')
102 $normalizer->setSerializer($serializer_prophecy->reveal());
104 $stdClass = new \stdClass();
108 $normalized = $normalizer->normalize($stdClass, static::TEST_FORMAT, $serialization_context);
109 $this->assertEquals(['a' => 'A-normalized', 'b' => 'B-normalized'], $normalized);
116 * Test normalizer with a different supported class.
118 class TestExtendedNormalizer extends ComplexDataNormalizer {
119 protected $supportedInterfaceOrClass = \stdClass::class;