class ConfigEntityNormalizerTest

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

@coversDefaultClass \Drupal\serialization\Normalizer\ConfigEntityNormalizer @group serialization

Hierarchy

Expanded class hierarchy of ConfigEntityNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/ConfigEntityNormalizerTest.php, line 19

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class ConfigEntityNormalizerTest extends UnitTestCase {
    
    /**
     * Tests the normalize() method.
     *
     * @covers ::normalize
     */
    public function testNormalize() : void {
        $test_export_properties = [
            'test' => 'test',
            '_core' => [
                'default_config_hash' => $this->randomMachineName(),
                $this->randomMachineName() => 'some random key',
            ],
        ];
        $entity_field_manager = $this->createMock(EntityFieldManagerInterface::class);
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class);
        $normalizer = new ConfigEntityNormalizer($entity_type_manager, $entity_type_repository, $entity_field_manager);
        $config_entity = $this->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
        $config_entity->expects($this->once())
            ->method('toArray')
            ->willReturn($test_export_properties);
        $this->assertSame([
            'test' => 'test',
        ], $normalizer->normalize($config_entity));
    }
    
    /**
     * @covers ::denormalize
     */
    public function testDenormalize() : void {
        $test_value = $this->randomMachineName();
        $data = [
            'test' => $test_value,
            '_core' => [
                'default_config_hash' => $this->randomMachineName(),
                $this->randomMachineName() => 'some random key',
            ],
        ];
        $expected_storage_data = [
            'test' => $test_value,
        ];
        // Mock of the entity storage, to test our expectation that the '_core' key
        // never makes it to that point, thanks to the denormalizer omitting it.
        $entity_storage = $this->prophesize(EntityStorageInterface::class);
        $entity_storage->create($expected_storage_data)
            ->shouldBeCalled()
            ->will(function ($args) {
            $entity = new \stdClass();
            $entity->received_data = $args[0];
            return $entity;
        });
        // Stubs for the denormalizer going from entity type manager to entity
        // storage.
        $entity_type_id = $this->randomMachineName();
        $entity_type_class = $this->randomMachineName();
        $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
        $entity_type_manager->getDefinition($entity_type_id, FALSE)
            ->willReturn($this->prophesize(ConfigEntityTypeInterface::class)
            ->reveal());
        $entity_type_manager->getStorage($entity_type_id)
            ->willReturn($entity_storage->reveal());
        $entity_type_repository = $this->prophesize(EntityTypeRepositoryInterface::class);
        $entity_type_repository->getEntityTypeFromClass($entity_type_class)
            ->willReturn($entity_type_id);
        $entity_field_manager = $this->prophesize(EntityFieldManagerInterface::class);
        $normalizer = new ConfigEntityNormalizer($entity_type_manager->reveal(), $entity_type_repository->reveal(), $entity_field_manager->reveal());
        // Verify the denormalizer still works correctly: the mock above creates an
        // artificial entity object containing exactly the data it received. It also
        // should still set _restSubmittedFields correctly.
        $expected_denormalization = (object) [
            '_restSubmittedFields' => [
                'test',
            ],
            'received_data' => [
                'test' => $test_value,
            ],
        ];
        $this->assertEquals($expected_denormalization, $normalizer->denormalize($data, $entity_type_class, 'json'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ConfigEntityNormalizerTest::testDenormalize public function @covers ::denormalize
ConfigEntityNormalizerTest::testNormalize public function Tests the normalize() method.
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.
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::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function

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