You are here

class ComplexDataNormalizerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
  2. 10 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() {
    $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()));
  }

  /**
   * Test 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);
  }

  /**
   * 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',
    ];
    $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 Test normalizing complex data.
ComplexDataNormalizerTest::testNormalizeNonComplex public function Test 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.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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.