Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / serialization / tests / src / Unit / Normalizer / ComplexDataNormalizerTest.php
index dc14d6f428cdc1be885080576b9f73832528507c..1bf6cf4a8125e3977adf3285f51dacec011f6e65 100644 (file)
@@ -8,7 +8,6 @@
 namespace Drupal\Tests\serialization\Unit\Normalizer;
 
 use Drupal\Core\TypedData\ComplexDataInterface;
-use Drupal\Core\TypedData\TraversableTypedDataInterface;
 use Drupal\serialization\Normalizer\ComplexDataNormalizer;
 use Drupal\Tests\UnitTestCase;
 use Symfony\Component\Serializer\Serializer;
@@ -19,6 +18,8 @@ use Symfony\Component\Serializer\Serializer;
  */
 class ComplexDataNormalizerTest extends UnitTestCase {
 
+  use InternalTypedDataTestTrait;
+
   /**
    * Test format string.
    *
@@ -44,103 +45,77 @@ class ComplexDataNormalizerTest extends UnitTestCase {
    * @covers ::supportsNormalization
    */
   public function testSupportsNormalization() {
-    $this->assertTrue($this->normalizer->supportsNormalization(new TestComplexData()));
+    $complex_data = $this->prophesize(ComplexDataInterface::class)->reveal();
+    $this->assertTrue($this->normalizer->supportsNormalization($complex_data));
     // 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)
-      ->shouldBeCalled();
-    $serializer_prophecy->normalize('B', static::TEST_FORMAT, $context)
+    $non_internal_property = $this->getTypedDataProperty(FALSE);
+
+    $serializer_prophecy->normalize($non_internal_property, static::TEST_FORMAT, [])
+      ->willReturn('A-normalized')
       ->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 {
-
-  private $values;
-
-  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() {
-  }
-
-  public function getParent() {
-  }
-
-  public function getProperties($include_computed = FALSE) {
-  }
-
-  public function getPropertyPath() {
-  }
-
-  public function getRoot() {
-  }
+    $complex_data = $this->prophesize(ComplexDataInterface::class);
+    $complex_data->getProperties(TRUE)
+      ->willReturn([
+        'prop:a' => $non_internal_property,
+        'prop:internal' => $this->getTypedDataProperty(TRUE),
+      ])
+      ->shouldBeCalled();
 
-  public function getString() {
+    $normalized = $this->normalizer->normalize($complex_data->reveal(), static::TEST_FORMAT);
+    $this->assertEquals(['prop:a' => 'A-normalized'], $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 normalizer with a different supported class.
+ */
+class TestExtendedNormalizer extends ComplexDataNormalizer {
+  protected $supportedInterfaceOrClass = \stdClass::class;
 
 }