1bf6cf4a8125e3977adf3285f51dacec011f6e65
[yaffs-website] / web / core / modules / serialization / tests / src / Unit / Normalizer / ComplexDataNormalizerTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.
6  */
7
8 namespace Drupal\Tests\serialization\Unit\Normalizer;
9
10 use Drupal\Core\TypedData\ComplexDataInterface;
11 use Drupal\serialization\Normalizer\ComplexDataNormalizer;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\Serializer\Serializer;
14
15 /**
16  * @coversDefaultClass \Drupal\serialization\Normalizer\ComplexDataNormalizer
17  * @group serialization
18  */
19 class ComplexDataNormalizerTest extends UnitTestCase {
20
21   use InternalTypedDataTestTrait;
22
23   /**
24    * Test format string.
25    *
26    * @var string
27    */
28   const TEST_FORMAT = 'test_format';
29
30   /**
31    * The Complex data normalizer under test.
32    *
33    * @var \Drupal\serialization\Normalizer\ComplexDataNormalizer
34    */
35   protected $normalizer;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     $this->normalizer = new ComplexDataNormalizer();
42   }
43
44   /**
45    * @covers ::supportsNormalization
46    */
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()));
52   }
53
54   /**
55    * Test normalizing complex data.
56    *
57    * @covers ::normalize
58    */
59   public function testNormalizeComplexData() {
60     $serializer_prophecy = $this->prophesize(Serializer::class);
61
62     $non_internal_property = $this->getTypedDataProperty(FALSE);
63
64     $serializer_prophecy->normalize($non_internal_property, static::TEST_FORMAT, [])
65       ->willReturn('A-normalized')
66       ->shouldBeCalled();
67
68     $this->normalizer->setSerializer($serializer_prophecy->reveal());
69
70     $complex_data = $this->prophesize(ComplexDataInterface::class);
71     $complex_data->getProperties(TRUE)
72       ->willReturn([
73         'prop:a' => $non_internal_property,
74         'prop:internal' => $this->getTypedDataProperty(TRUE),
75       ])
76       ->shouldBeCalled();
77
78     $normalized = $this->normalizer->normalize($complex_data->reveal(), static::TEST_FORMAT);
79     $this->assertEquals(['prop:a' => 'A-normalized'], $normalized);
80   }
81
82   /**
83    * Test normalize() where $object does not implement ComplexDataInterface.
84    *
85    * Normalizers extending ComplexDataNormalizer may have a different supported
86    * class.
87    *
88    * @covers ::normalize
89    */
90   public function testNormalizeNonComplex() {
91     $normalizer = new TestExtendedNormalizer();
92     $serialization_context = ['test' => 'test'];
93
94     $serializer_prophecy = $this->prophesize(Serializer::class);
95     $serializer_prophecy->normalize('A', static::TEST_FORMAT, $serialization_context)
96       ->willReturn('A-normalized')
97       ->shouldBeCalled();
98     $serializer_prophecy->normalize('B', static::TEST_FORMAT, $serialization_context)
99       ->willReturn('B-normalized')
100       ->shouldBeCalled();
101
102     $normalizer->setSerializer($serializer_prophecy->reveal());
103
104     $stdClass = new \stdClass();
105     $stdClass->a = 'A';
106     $stdClass->b = 'B';
107
108     $normalized = $normalizer->normalize($stdClass, static::TEST_FORMAT, $serialization_context);
109     $this->assertEquals(['a' => 'A-normalized', 'b' => 'B-normalized'], $normalized);
110
111   }
112
113 }
114
115 /**
116  * Test normalizer with a different supported class.
117  */
118 class TestExtendedNormalizer extends ComplexDataNormalizer {
119   protected $supportedInterfaceOrClass = \stdClass::class;
120
121 }