class CallableResolverTest
Same name in other branches
- 11.x core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php \Drupal\Tests\Core\Utility\CallableResolverTest
@coversDefaultClass \Drupal\Core\Utility\CallableResolver @group Utility
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Utility\CallableResolverTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CallableResolverTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Utility/ CallableResolverTest.php, line 19
Namespace
Drupal\Tests\Core\UtilityView source
class CallableResolverTest extends UnitTestCase {
/**
* The callable resolver.
*
* @var \Drupal\Core\Utility\CallableResolver
*/
protected CallableResolver $resolver;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$container = new ContainerBuilder();
$container->set('test_service', $this);
$class_resolver = new ClassResolver($container);
$this->resolver = new CallableResolver($class_resolver);
}
/**
* @covers ::getCallableFromDefinition
* @group legacy
*/
public function testCallbackResolver() : void {
$cases = [
'Inline function' => [
function ($suffix) {
return __METHOD__ . '+' . $suffix;
},
'Drupal\\Tests\\Core\\Utility\\{closure}',
],
'First-class callable function' => [
$this->method(...),
__CLASS__ . '::method',
],
'First-class callable static' => [
static::staticMethod(...),
__CLASS__ . '::staticMethod',
],
'Arrow function' => [
fn($suffix) => __METHOD__ . '+' . $suffix,
'Drupal\\Tests\\Core\\Utility\\{closure}',
],
'Static function' => [
'\\Drupal\\Tests\\Core\\Utility\\NoInstantiationMockStaticCallable::staticMethod',
'Drupal\\Tests\\Core\\Utility\\NoInstantiationMockStaticCallable::staticMethod',
],
'Static function, array notation' => [
[
NoInstantiationMockStaticCallable::class,
'staticMethod',
],
'Drupal\\Tests\\Core\\Utility\\NoInstantiationMockStaticCallable::staticMethod',
],
'Static function, array notation, with object' => [
[
$this,
'staticMethod',
],
__CLASS__ . '::staticMethod',
],
'Non-static function, array notation, with object' => [
[
$this,
'method',
],
__CLASS__ . '::method',
],
'Non-static function, instantiated by class resolver' => [
static::class . '::method',
__CLASS__ . '::method',
],
'Non-static function, instantiated by class resolver, container injection' => [
'\\Drupal\\Tests\\Core\\Utility\\MockContainerInjection::getResult',
'Drupal\\Tests\\Core\\Utility\\MockContainerInjection::getResult-foo',
],
'Non-static function, instantiated by class resolver, container aware' => [
'\\Drupal\\Tests\\Core\\Utility\\MockContainerAware::getResult',
'Drupal\\Tests\\Core\\Utility\\MockContainerAware::getResult',
'Implementing \\Symfony\\Component\\DependencyInjection\\ContainerAwareInterface is deprecated in drupal:10.3.0 and it will be removed in drupal:11.0.0. Implement \\Drupal\\Core\\DependencyInjection\\ContainerInjectionInterface and use dependency injection instead. See https://www.drupal.org/node/3428661',
],
'Service notation' => [
'test_service:method',
__CLASS__ . '::method',
],
'Service notation, static method' => [
'test_service:staticMethod',
__CLASS__ . '::staticMethod',
],
'Class with invoke method' => [
static::class,
__CLASS__ . '::__invoke',
],
];
$argument = 'bar';
foreach ($cases as $label => [
$definition,
$result,
]) {
if (isset($cases[$label][2])) {
$this->expectDeprecation($cases[$label][2]);
}
$this->assertEquals($result . '+' . $argument, $this->resolver
->getCallableFromDefinition($definition)($argument), $label);
}
}
/**
* @dataProvider callableResolverExceptionHandlingTestCases
* @covers ::getCallableFromDefinition
*/
public function testCallbackResolverExceptionHandling($definition, $exception_class, $exception_message) : void {
$this->expectException($exception_class);
$this->expectExceptionMessage($exception_message);
$this->resolver
->getCallableFromDefinition($definition);
}
/**
* Test cases for ::testCallbackResolverExceptionHandling.
*/
public static function callableResolverExceptionHandlingTestCases() {
return [
'String function' => [
'not_a_callable',
\InvalidArgumentException::class,
'Class "not_a_callable" does not exist.',
],
'Array notation' => [
[
'not_a_callable',
'not_a_callable',
],
\InvalidArgumentException::class,
'The callable definition provided "[not_a_callable,not_a_callable]" is not a valid callable.',
],
'Missing method on class, array notation' => [
[
static::class,
'method_not_exists',
],
\InvalidArgumentException::class,
'The callable definition provided "[Drupal\\Tests\\Core\\Utility\\CallableResolverTest,method_not_exists]" is not a valid callable.',
],
'Missing method on class, static notation' => [
static::class . '::method_not_exists',
\InvalidArgumentException::class,
'The callable definition provided was invalid. Either class "Drupal\\Tests\\Core\\Utility\\CallableResolverTest" does not have a method "method_not_exists", or it is not callable.',
],
'Missing class, static notation' => [
'\\NotARealClass::method',
\InvalidArgumentException::class,
'Class "\\NotARealClass" does not exist.',
],
'No method, static notation' => [
NoMethodCallable::class . "::",
\InvalidArgumentException::class,
'The callable definition provided was invalid. Could not get class and method from definition "Drupal\\Tests\\Core\\Utility\\NoMethodCallable::".',
],
'Service not in container' => [
'bad_service:method',
\InvalidArgumentException::class,
'Class "bad_service" does not exist.',
],
'Invalid method on valid service' => [
'test_service:not_a_callable',
\InvalidArgumentException::class,
'The callable definition provided was invalid. Either class "Drupal\\Tests\\Core\\Utility\\CallableResolverTest" does not have a method "not_a_callable", or it is not callable.',
],
];
}
/**
* A test static method that returns "foo".
*
* @param string $suffix
* A suffix to append.
*
* @return string
* A test string.
*/
public static function staticMethod($suffix) {
return __METHOD__ . '+' . $suffix;
}
/**
* A test method that returns "foo".
*
* @param string $suffix
* A suffix to append.
*
* @return string
* A test string.
*
* @throws \Exception
* Throws an exception when called statically.
*/
public function method($suffix) {
return __METHOD__ . '+' . $suffix;
}
/**
* A test __invoke method.
*
* @param string $suffix
* A suffix to append.
*
* @return string
* A test string.
*/
public function __invoke($suffix) {
return __METHOD__ . '+' . $suffix;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
CallableResolverTest::$resolver | protected | property | The callable resolver. | |||
CallableResolverTest::callableResolverExceptionHandlingTestCases | public static | function | Test cases for ::testCallbackResolverExceptionHandling. | |||
CallableResolverTest::method | public | function | A test method that returns "foo". | |||
CallableResolverTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
CallableResolverTest::staticMethod | public static | function | A test static method that returns "foo". | |||
CallableResolverTest::testCallbackResolver | public | function | @covers ::getCallableFromDefinition @group legacy |
|||
CallableResolverTest::testCallbackResolverExceptionHandling | public | function | @dataProvider callableResolverExceptionHandlingTestCases @covers ::getCallableFromDefinition |
|||
CallableResolverTest::__invoke | public | function | A test __invoke method. | |||
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.