class ComplexDataNormalizerTest

Same name in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
  2. 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest
  3. 10 core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest

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

Hierarchy

  • class \Drupal\Tests\serialization\Unit\Normalizer\ComplexDataNormalizerTest extends \Drupal\Tests\UnitTestCase uses \Drupal\Tests\serialization\Unit\Normalizer\InternalTypedDataTestTrait

Expanded class hierarchy of ComplexDataNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php, line 16

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 {
        parent::setUp();
        $this->normalizer = new ComplexDataNormalizer();
    }
    
    /**
     * @covers ::supportsNormalization
     */
    public function testSupportsNormalization() : void {
        $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() : void {
        $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() : void {
        $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

Title Sort descending Modifiers Object type Summary Overriden Title
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.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
InternalTypedDataTestTrait::getTypedDataProperty protected function Gets a typed data property.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.