diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php index f59e15f279..de936d7acf 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php @@ -7,9 +7,10 @@ namespace Drupal\Tests\serialization\Unit\Normalizer; +use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\Core\TypedData\ComplexDataInterface; -use Drupal\Core\TypedData\ComplexDataWithExposedPropertiesTrait; -use Drupal\Core\TypedData\TraversableTypedDataInterface; +use Drupal\Core\TypedData\TypedDataInterface; use Drupal\serialization\Normalizer\ComplexDataNormalizer; use Drupal\Tests\UnitTestCase; use Symfony\Component\Serializer\Serializer; @@ -45,105 +46,97 @@ protected function setUp() { * @covers ::supportsNormalization */ public function testSupportsNormalization() { - $this->assertTrue($this->normalizer->supportsNormalization(new TestComplexData())); + $complexData = $this->prophesize(ComplexDataInterface::class)->reveal(); + $this->assertTrue($this->normalizer->supportsNormalization($complexData)); // Also test that an object not implementing ComplexDataInterface fails. $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); } /** + * Test normalizing complex data. + * * @covers ::normalize */ - public function testNormalize() { - $context = ['test' => 'test']; - + public function testNormalizeComplexData() { $serializer_prophecy = $this->prophesize(Serializer::class); - $serializer_prophecy->normalize('A', static::TEST_FORMAT, $context) + $cacheable = $this->prophesize(TestCacheableDependencyInterface::class); + $cacheable->__toString() + ->willReturn('prop-as-string') ->shouldBeCalled(); - $serializer_prophecy->normalize('B', static::TEST_FORMAT, $context) - ->shouldBeCalled(); - - $this->normalizer->setSerializer($serializer_prophecy->reveal()); - - $complex_data = new TestComplexData(['a' => 'A', 'b' => 'B']); - $this->normalizer->normalize($complex_data, static::TEST_FORMAT, $context); - - } - -} - -/** - * Test class implementing ComplexDataInterface and IteratorAggregate. - */ -class TestComplexData implements \IteratorAggregate, ComplexDataInterface { - - use ComplexDataWithExposedPropertiesTrait; - - private $values; + $cacheable = $cacheable->reveal(); - public function __construct(array $values = []) { - $this->values = $values; - } - - public function getIterator() { - return new \ArrayIterator($this->values); - } - - public function applyDefaultValue($notify = TRUE) { - } - - public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) { - } - - public function get($property_name) { - } - - public function getConstraints() { - } - - public function getDataDefinition() { - } - - public function getName() { - } + $property = $this->prophesize(TypedDataInterface::class); + $property = $property->reveal(); - public function getParent() { - } + $cacheableMetaData = $this->prophesize(RefinableCacheableDependencyInterface::class); + $cacheableMetaData->addCacheableDependency($cacheable) + ->shouldBeCalled(); + $serialization_context = [ + 'cacheability' => $cacheableMetaData->reveal(), + ]; - public function getProperties($include_computed = FALSE) { - } + $serializer_prophecy->normalize('A', static::TEST_FORMAT, $serialization_context) + ->willReturn('A-normalized') + ->shouldBeCalled(); + $serializer_prophecy->normalize($property, static::TEST_FORMAT, $serialization_context) + ->willReturn($cacheable) + ->shouldBeCalled(); - public function getPropertyPath() { - } + $this->normalizer->setSerializer($serializer_prophecy->reveal()); - public function getRoot() { - } + $complex_data = $this->prophesize(ComplexDataInterface::class); + $complex_data->getExposedProperties() + ->willReturn(['prop:a' => 'A', 'prop:cacheable' => $property]) + ->shouldBeCalled(); - public function getString() { + $normalized = $this->normalizer->normalize($complex_data->reveal(), static::TEST_FORMAT, $serialization_context); + $this->assertEquals(['prop:a' => 'A-normalized', 'prop:cacheable' => 'prop-as-string'], $normalized); } - public function getValue() { - } + /** + * Test normalize() where $object does not implement ComplexDataInterface. + * + * Normalizers extending ComplexDataNormalizer may have a different supported + * class. + * + * @covers ::normalize + */ + public function testNormalizeNonComplex() { + $normalizer = new TestExtendedNormalizer(); + $serialization_context = ['test' => 'test']; - public function isEmpty() { - } + $serializer_prophecy = $this->prophesize(Serializer::class); + $serializer_prophecy->normalize('A', static::TEST_FORMAT, $serialization_context) + ->willReturn('A-normalized') + ->shouldBeCalled(); + $serializer_prophecy->normalize('B', static::TEST_FORMAT, $serialization_context) + ->willReturn('B-normalized') + ->shouldBeCalled(); - public function onChange($name) { - } + $normalizer->setSerializer($serializer_prophecy->reveal()); - public function set($property_name, $value, $notify = TRUE) { - } + $stdClass = new \stdClass(); + $stdClass->a = 'A'; + $stdClass->b = 'B'; - public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) { - } + $normalized = $normalizer->normalize($stdClass, static::TEST_FORMAT, $serialization_context); + $this->assertEquals(['a' => 'A-normalized', 'b' => 'B-normalized'], $normalized); - public function setValue($value, $notify = TRUE) { } - public function toArray() { - } +} - public function validate() { - } +/** + * Test interface used for mocking. + */ +interface TestCacheableDependencyInterface extends CacheableDependencyInterface { + public function __toString(); +} +/** + * Test normalizer with a different supported class. + */ +class TestExtendedNormalizer extends ComplexDataNormalizer { + protected $supportedInterfaceOrClass = \stdClass::class; }