You are here

class ComplexDataNormalizerTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
  2. 9 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest

@coversDefaultClass \Drupal\serialization\Normalizer\ComplexDataNormalizer @group serialization

Hierarchy

Expanded class hierarchy of ComplexDataNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php, line 19
Contains \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest.

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class ComplexDataNormalizerTest extends UnitTestCase {
  use InternalTypedDataTestTrait;

  /**
   * Test format string.
   *
   * @var string
   */
  const TEST_FORMAT = 'test_format';

  /**
   * The Complex data normalizer under test.
   *
   * @var \Drupal\serialization\Normalizer\ComplexDataNormalizer
   */
  protected $normalizer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->normalizer = new ComplexDataNormalizer();
  }

  /**
   * @covers ::supportsNormalization
   */
  public function testSupportsNormalization() {
    $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()));
  }

  /**
   * Tests normalizing complex data.
   *
   * @covers ::normalize
   */
  public function testNormalizeComplexData() {
    $serializer_prophecy = $this
      ->prophesize(Serializer::class);
    $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 = $this
      ->prophesize(ComplexDataInterface::class);
    $complex_data
      ->getProperties(TRUE)
      ->willReturn([
      'prop:a' => $non_internal_property,
      'prop:internal' => $this
        ->getTypedDataProperty(TRUE),
    ])
      ->shouldBeCalled();
    $normalized = $this->normalizer
      ->normalize($complex_data
      ->reveal(), static::TEST_FORMAT);
    $this
      ->assertEquals([
      'prop:a' => 'A-normalized',
    ], $normalized);
  }

  /**
   * Tests 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',
    ];
    $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();
    $normalizer
      ->setSerializer($serializer_prophecy
      ->reveal());
    $stdClass = new \stdClass();
    $stdClass->a = 'A';
    $stdClass->b = 'B';
    $normalized = $normalizer
      ->normalize($stdClass, static::TEST_FORMAT, $serialization_context);
    $this
      ->assertEquals([
      'a' => 'A-normalized',
      'b' => 'B-normalized',
    ], $normalized);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComplexDataNormalizerTest::$normalizer protected property The Complex data normalizer under test.
ComplexDataNormalizerTest::setUp protected function Overrides UnitTestCase::setUp
ComplexDataNormalizerTest::testNormalizeComplexData public function Tests normalizing complex data.
ComplexDataNormalizerTest::testNormalizeNonComplex public function Tests normalize() where $object does not implement ComplexDataInterface.
ComplexDataNormalizerTest::testSupportsNormalization public function @covers ::supportsNormalization
ComplexDataNormalizerTest::TEST_FORMAT constant Test format string.
InternalTypedDataTestTrait::getTypedDataProperty protected function Gets a typed data property.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function