class EntityConverterTest

Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php \Drupal\KernelTests\Core\ParamConverter\EntityConverterTest
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php \Drupal\KernelTests\Core\ParamConverter\EntityConverterTest
  2. 9 core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityConverterTest
  3. 8.9.x core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityConverterTest
  4. 11.x core/tests/Drupal/KernelTests/Core/ParamConverter/EntityConverterTest.php \Drupal\KernelTests\Core\ParamConverter\EntityConverterTest
  5. 11.x core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityConverterTest

@coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter @group ParamConverter @group Entity

Hierarchy

Expanded class hierarchy of EntityConverterTest

File

core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php, line 27

Namespace

Drupal\Tests\Core\ParamConverter
View source
class EntityConverterTest extends UnitTestCase {
    
    /**
     * The mocked entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityTypeManager;
    
    /**
     * The mocked entities repository.
     *
     * @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $entityRepository;
    
    /**
     * The tested entity converter.
     *
     * @var \Drupal\Core\ParamConverter\EntityConverter
     */
    protected $entityConverter;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
        $this->entityRepository = $this->createMock(EntityRepositoryInterface::class);
        $this->entityConverter = new EntityConverter($this->entityTypeManager, $this->entityRepository);
    }
    
    /**
     * Sets up mock services and class instances.
     *
     * @param object[] $service_map
     *   An associative array of service instances keyed by service name.
     */
    protected function setUpMocks($service_map = []) {
        $entity = $this->createMock(ContentEntityInterface::class);
        $entity->expects($this->any())
            ->method('getEntityTypeId')
            ->willReturn('entity_test');
        $entity->expects($this->any())
            ->method('id')
            ->willReturn('id');
        $entity->expects($this->any())
            ->method('isTranslatable')
            ->willReturn(FALSE);
        $entity->expects($this->any())
            ->method('getLoadedRevisionId')
            ->willReturn('revision_id');
        $storage = $this->createMock(ContentEntityStorageInterface::class);
        $storage->expects($this->any())
            ->method('load')
            ->with('id')
            ->willReturn($entity);
        $storage->expects($this->any())
            ->method('getLatestRevisionId')
            ->with('id')
            ->willReturn('revision_id');
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getStorage')
            ->with('entity_test')
            ->willReturn($storage);
        $entity_type = $this->createMock(ContentEntityTypeInterface::class);
        $entity_type->expects($this->any())
            ->method('isRevisionable')
            ->willReturn(TRUE);
        $this->entityTypeManager
            ->expects($this->any())
            ->method('getDefinition')
            ->with('entity_test')
            ->willReturn($entity_type);
        $context_definition = $this->createMock(DataDefinition::class);
        foreach ([
            'setLabel',
            'setDescription',
            'setRequired',
            'setConstraints',
        ] as $method) {
            $context_definition->expects($this->any())
                ->method($method)
                ->willReturn($context_definition);
        }
        $context_definition->expects($this->any())
            ->method('getConstraints')
            ->willReturn([]);
        $typed_data_manager = $this->createMock(TypedDataManagerInterface::class);
        $typed_data_manager->expects($this->any())
            ->method('create')
            ->willReturn($this->createMock(TypedDataInterface::class));
        $typed_data_manager->expects($this->any())
            ->method('createDataDefinition')
            ->willReturn($context_definition);
        $service_map += [
            'typed_data_manager' => $typed_data_manager,
        ];
        
        /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject $container */
        $container = $this->createMock(ContainerInterface::class);
        $return_map = [];
        foreach ($service_map as $name => $service) {
            $return_map[] = [
                $name,
                1,
                $service,
            ];
        }
        $container->expects($this->any())
            ->method('get')
            ->willReturnMap($return_map);
        \Drupal::setContainer($container);
    }
    
    /**
     * Tests the applies() method.
     *
     * @dataProvider providerTestApplies
     *
     * @covers ::applies
     */
    public function testApplies(array $definition, $name, Route $route, $applies) : void {
        $this->entityTypeManager
            ->expects($this->any())
            ->method('hasDefinition')
            ->willReturnCallback(function ($entity_type) {
            return 'entity_test' == $entity_type;
        });
        $this->assertEquals($applies, $this->entityConverter
            ->applies($definition, $name, $route));
    }
    
    /**
     * Provides test data for testApplies()
     */
    public static function providerTestApplies() {
        $data = [];
        $data[] = [
            [
                'type' => 'entity:foo',
            ],
            'foo',
            new Route('/test/{foo}/bar'),
            FALSE,
        ];
        $data[] = [
            [
                'type' => 'entity:entity_test',
            ],
            'foo',
            new Route('/test/{foo}/bar'),
            TRUE,
        ];
        $data[] = [
            [
                'type' => 'entity:entity_test',
            ],
            'entity_test',
            new Route('/test/{entity_test}/bar'),
            TRUE,
        ];
        $data[] = [
            [
                'type' => 'entity:{entity_test}',
            ],
            'entity_test',
            new Route('/test/{entity_test}/bar'),
            FALSE,
        ];
        $data[] = [
            [
                'type' => 'entity:{entity_type}',
            ],
            'entity_test',
            new Route('/test/{entity_type}/{entity_test}/bar'),
            TRUE,
        ];
        $data[] = [
            [
                'type' => 'foo',
            ],
            'entity_test',
            new Route('/test/{entity_type}/{entity_test}/bar'),
            FALSE,
        ];
        return $data;
    }
    
    /**
     * Tests the convert() method.
     *
     * @dataProvider providerTestConvert
     *
     * @covers ::convert
     */
    public function testConvert($value, array $definition, array $defaults, $expected_result) : void {
        $this->setUpMocks();
        $this->entityRepository
            ->expects($this->any())
            ->method('getCanonical')
            ->willReturnCallback(function ($entity_type_id, $entity_id) {
            return $entity_type_id === 'entity_test' && $entity_id === 'valid_id' ? (object) [
                'id' => 'valid_id',
            ] : NULL;
        });
        $this->assertEquals($expected_result, $this->entityConverter
            ->convert($value, $definition, 'foo', $defaults));
    }
    
    /**
     * Provides test data for testConvert.
     */
    public static function providerTestConvert() {
        $data = [];
        // Existing entity type.
        $data[] = [
            'valid_id',
            [
                'type' => 'entity:entity_test',
            ],
            [
                'foo' => 'valid_id',
            ],
            (object) [
                'id' => 'valid_id',
            ],
        ];
        // Invalid ID.
        $data[] = [
            'invalid_id',
            [
                'type' => 'entity:entity_test',
            ],
            [
                'foo' => 'invalid_id',
            ],
            NULL,
        ];
        // Entity type placeholder.
        $data[] = [
            'valid_id',
            [
                'type' => 'entity:{entity_type}',
            ],
            [
                'foo' => 'valid_id',
                'entity_type' => 'entity_test',
            ],
            (object) [
                'id' => 'valid_id',
            ],
        ];
        return $data;
    }
    
    /**
     * Tests the convert() method with an invalid entity type.
     */
    public function testConvertWithInvalidEntityType() : void {
        $this->setUpMocks();
        $plugin_id = 'invalid_id';
        $contexts = [
            'operation' => 'entity_upcast',
        ];
        $this->entityRepository
            ->expects($this->once())
            ->method('getCanonical')
            ->with($plugin_id, 'id', $contexts)
            ->willThrowException(new PluginNotFoundException($plugin_id));
        $this->expectException(PluginNotFoundException::class);
        $this->entityConverter
            ->convert('id', [
            'type' => 'entity:' . $plugin_id,
        ], 'foo', [
            'foo' => 'id',
        ]);
    }
    
    /**
     * Tests the convert() method with an invalid dynamic entity type.
     */
    public function testConvertWithInvalidDynamicEntityType() : void {
        $this->expectException(ParamNotConvertedException::class);
        $this->expectExceptionMessage('The "foo" parameter was not converted because the "invalid_id" parameter is missing.');
        $this->entityConverter
            ->convert('id', [
            'type' => 'entity:{invalid_id}',
        ], 'foo', [
            'foo' => 'id',
        ]);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityConverterTest::$entityConverter protected property The tested entity converter.
EntityConverterTest::$entityRepository protected property The mocked entities repository.
EntityConverterTest::$entityTypeManager protected property The mocked entity type manager.
EntityConverterTest::providerTestApplies public static function Provides test data for testApplies()
EntityConverterTest::providerTestConvert public static function Provides test data for testConvert.
EntityConverterTest::setUp protected function Overrides UnitTestCase::setUp
EntityConverterTest::setUpMocks protected function Sets up mock services and class instances.
EntityConverterTest::testApplies public function Tests the applies() method.
EntityConverterTest::testConvert public function Tests the convert() method.
EntityConverterTest::testConvertWithInvalidDynamicEntityType public function Tests the convert() method with an invalid dynamic entity type.
EntityConverterTest::testConvertWithInvalidEntityType public function Tests the convert() method with an invalid entity type.
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.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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