class ParamConverterManagerTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/ParamConverter/ParamConverterManagerTest.php \Drupal\Tests\Core\ParamConverter\ParamConverterManagerTest
- 10 core/tests/Drupal/Tests/Core/ParamConverter/ParamConverterManagerTest.php \Drupal\Tests\Core\ParamConverter\ParamConverterManagerTest
- 9 core/tests/Drupal/Tests/Core/ParamConverter/ParamConverterManagerTest.php \Drupal\Tests\Core\ParamConverter\ParamConverterManagerTest
- 8.9.x core/tests/Drupal/Tests/Core/ParamConverter/ParamConverterManagerTest.php \Drupal\Tests\Core\ParamConverter\ParamConverterManagerTest
Tests Drupal\Core\ParamConverter\ParamConverterManager.
Attributes
#[CoversClass(ParamConverterManager::class)]
#[Group('ParamConverter')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\ParamConverter\ParamConverterManagerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ParamConverterManagerTest
File
-
core/
tests/ Drupal/ Tests/ Core/ ParamConverter/ ParamConverterManagerTest.php, line 20
Namespace
Drupal\Tests\Core\ParamConverterView source
class ParamConverterManagerTest extends UnitTestCase {
/**
* @var \Drupal\Core\ParamConverter\ParamConverterManager
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->manager = new ParamConverterManager();
}
/**
* Tests \Drupal\Core\ParamConverter\ParamConverterManager::getConverter().
*/
public function testGetConverter($name, $class) : void {
$converter = $this->getMockBuilder('Drupal\\Core\\ParamConverter\\ParamConverterInterface')
->setMockClassName($class)
->getMock();
$this->manager
->addConverter($converter, $name);
$this->assertInstanceOf($class, $this->manager
->getConverter($name));
// Assert that a second call to getConverter() does not use the container.
$this->assertInstanceOf($class, $this->manager
->getConverter($name));
}
/**
* Tests \Drupal\Core\ParamConverter\ParamConverterManager::getConverter().
*/
public function testGetConverterException() : void {
$this->expectException(\InvalidArgumentException::class);
$this->manager
->getConverter('undefined.converter');
}
/**
* Provide data for parameter converter manager tests.
*
* @return array
* An array of arrays, each containing the input parameters for
* providerTestResolvers::testAddConverter().
*
* @see ParamConverterManagerTest::testAddConverter()
*/
public function providerTestAddConverter() : array {
$converters[0]['unsorted'] = [
[
'name' => 'strawberry',
],
[
'name' => 'raspberry',
],
[
'name' => 'pear',
],
[
'name' => 'peach',
],
[
'name' => 'pineapple',
],
[
'name' => 'banana',
],
[
'name' => 'apple',
],
];
$converters[0]['sorted'] = [
'strawberry',
'raspberry',
'pear',
'peach',
'pineapple',
'banana',
'apple',
];
$converters[1]['unsorted'] = [
[
'name' => 'giraffe',
],
[
'name' => 'zebra',
],
[
'name' => 'eagle',
],
[
'name' => 'ape',
],
[
'name' => 'cat',
],
[
'name' => 'puppy',
],
[
'name' => 'llama',
],
];
$converters[1]['sorted'] = [
'giraffe',
'zebra',
'eagle',
'ape',
'cat',
'puppy',
'llama',
];
return $converters;
}
/**
* Provide data for parameter converter manager tests.
*
* @return array
* An array of arrays, each containing the input parameters for
* providerTestResolvers::testGetConverter().
*
* @see ParamConverterManagerTest::testGetConverter()
*/
public static function providerTestGetConverter() : array {
return [
[
'ape',
'ApeConverterClass',
],
[
'cat',
'CatConverterClass',
],
[
'puppy',
'PuppyConverterClass',
],
[
'llama',
'LlamaConverterClass',
],
[
'giraffe',
'GiraffeConverterClass',
],
[
'zebra',
'ZebraConverterClass',
],
[
'eagle',
'EagleConverterClass',
],
];
}
/**
* Tests set route parameter converters.
*/
public function testSetRouteParameterConverters($path, $parameters = NULL, $expected = NULL) : void {
$converter = $this->createMock('Drupal\\Core\\ParamConverter\\ParamConverterInterface');
$converter->expects($this->any())
->method('applies')
->with($this->anything(), 'id', $this->anything())
->willReturn(TRUE);
$this->manager
->addConverter($converter, 'applied');
$route = new Route($path);
if ($parameters) {
$route->setOption('parameters', $parameters);
}
$collection = new RouteCollection();
$collection->add('test_route', $route);
$this->manager
->setRouteParameterConverters($collection);
foreach ($collection as $route) {
$result = $route->getOption('parameters');
if ($expected) {
$this->assertSame($expected, $result['id']['converter']);
}
else {
$this->assertNull($result);
}
}
}
/**
* Provides data for testSetRouteParameterConverters().
*/
public static function providerTestSetRouteParameterConverters() : array {
return [
[
'/test',
],
[
'/test/{id}',
[
'id' => [],
],
'applied',
],
[
'/test/{id}',
[
'id' => [
'converter' => 'predefined',
],
],
'predefined',
],
];
}
/**
* Tests convert.
*/
public function testConvert() : void {
$route = new Route('/test/{id}/{literal}/{null}');
$parameters = [
'id' => [
'converter' => 'test_convert',
],
'literal' => [],
'null' => [],
];
$route->setOption('parameters', $parameters);
$defaults = [
RouteObjectInterface::ROUTE_OBJECT => $route,
RouteObjectInterface::ROUTE_NAME => 'test_route',
'id' => 1,
'literal' => 'this is a literal',
'null' => NULL,
];
$expected = $defaults;
$expected['id'] = 'something_better!';
$converter = $this->createMock('Drupal\\Core\\ParamConverter\\ParamConverterInterface');
$converter->expects($this->any())
->method('convert')
->with(1, $this->isArray(), 'id', $this->isArray())
->willReturn('something_better!');
$this->manager
->addConverter($converter, 'test_convert');
$result = $this->manager
->convert($defaults);
$this->assertEquals($expected, $result);
}
/**
* Tests convert no converting.
*/
public function testConvertNoConverting() : void {
$route = new Route('/test');
$defaults = [
RouteObjectInterface::ROUTE_OBJECT => $route,
RouteObjectInterface::ROUTE_NAME => 'test_route',
];
$expected = $defaults;
$result = $this->manager
->convert($defaults);
$this->assertEquals($expected, $result);
}
/**
* Tests convert missing param.
*/
public function testConvertMissingParam() : void {
$route = new Route('/test/{id}');
$parameters = [
'id' => [
'converter' => 'test_convert',
],
];
$route->setOption('parameters', $parameters);
$defaults = [
RouteObjectInterface::ROUTE_OBJECT => $route,
RouteObjectInterface::ROUTE_NAME => 'test_route',
'id' => 1,
];
$converter = $this->createMock('Drupal\\Core\\ParamConverter\\ParamConverterInterface');
$converter->expects($this->any())
->method('convert')
->with(1, $this->isArray(), 'id', $this->isArray())
->willReturn(NULL);
$this->manager
->addConverter($converter, 'test_convert');
$this->expectException(ParamNotConvertedException::class);
$this->expectExceptionMessage('The "id" parameter was not converted for the path "/test/{id}" (route name: "test_route")');
$this->manager
->convert($defaults);
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | |||
| ParamConverterManagerTest::$manager | protected | property | |||
| ParamConverterManagerTest::providerTestAddConverter | public | function | Provide data for parameter converter manager tests. | ||
| ParamConverterManagerTest::providerTestGetConverter | public static | function | Provide data for parameter converter manager tests. | ||
| ParamConverterManagerTest::providerTestSetRouteParameterConverters | public static | function | Provides data for testSetRouteParameterConverters(). | ||
| ParamConverterManagerTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
| ParamConverterManagerTest::testConvert | public | function | Tests convert. | ||
| ParamConverterManagerTest::testConvertMissingParam | public | function | Tests convert missing param. | ||
| ParamConverterManagerTest::testConvertNoConverting | public | function | Tests convert no converting. | ||
| ParamConverterManagerTest::testGetConverter | public | function | Tests \Drupal\Core\ParamConverter\ParamConverterManager::getConverter(). | ||
| ParamConverterManagerTest::testGetConverterException | public | function | Tests \Drupal\Core\ParamConverter\ParamConverterManager::getConverter(). | ||
| ParamConverterManagerTest::testSetRouteParameterConverters | public | function | Tests set route parameter converters. | ||
| 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::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::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.