class CallableResolverTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php \Drupal\Tests\Core\Utility\CallableResolverTest
- 10 core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php \Drupal\Tests\Core\Utility\CallableResolverTest
Tests Drupal\Core\Utility\CallableResolver.
Attributes
#[CoversClass(CallableResolver::class)]
#[Group('Utility')]
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\Utility\CallableResolverTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CallableResolverTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Utility/ CallableResolverTest.php, line 20
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);
}
/**
* Tests callback resolver.
*
* @legacy-covers ::getCallableFromDefinition
*/
public function testCallbackResolver() : void {
$cases = [
'Inline function' => [
function ($suffix) {
return __METHOD__ . '+' . $suffix;
},
'{closure:Drupal\\Tests\\Core\\Utility\\CallableResolverTest::testCallbackResolver():53}',
],
'First-class callable function' => [
$this->method(...),
__CLASS__ . '::method',
],
'First-class callable static' => [
static::staticMethod(...),
__CLASS__ . '::staticMethod',
],
'Arrow function' => [
fn($suffix): string => __METHOD__ . '+' . $suffix,
'{closure:Drupal\\Tests\\Core\\Utility\\CallableResolverTest::testCallbackResolver():67}',
],
'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' => [
MethodCallable::class . '::method',
MethodCallable::class . '::method',
],
'Non-static function, instantiated by class resolver, container injection' => [
'\\Drupal\\Tests\\Core\\Utility\\MockContainerInjection::getResult',
'Drupal\\Tests\\Core\\Utility\\MockContainerInjection::getResult-foo',
],
'Service notation' => [
'test_service:method',
__CLASS__ . '::method',
],
'Service notation, static method' => [
'test_service:staticMethod',
__CLASS__ . '::staticMethod',
],
'Class with invoke method' => [
MethodCallable::class,
MethodCallable::class . '::__invoke',
],
];
$argument = 'bar';
foreach ($cases as $label => [$definition, $result]) {
$this->assertEquals($result . '+' . $argument, $this->resolver
->getCallableFromDefinition($definition)($argument), $label);
}
}
/**
* Tests callback resolver exception handling.
*
* @legacy-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() : array {
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' => [
[
\stdClass::class,
'method_not_exists',
],
\InvalidArgumentException::class,
'The callable definition provided "[stdClass,method_not_exists]" is not a valid callable.',
],
'Missing method on class, static notation' => [
\stdClass::class . '::method_not_exists',
\InvalidArgumentException::class,
'The callable definition provided was invalid. Either class "stdClass" 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) : string {
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) : string {
return __METHOD__ . '+' . $suffix;
}
/**
* A test __invoke method.
*
* @param string $suffix
* A suffix to append.
*
* @return string
* A test string.
*/
public function __invoke($suffix) : string {
return __METHOD__ . '+' . $suffix;
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| 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 | Tests callback resolver. | ||
| CallableResolverTest::testCallbackResolverExceptionHandling | public | function | Tests callback resolver exception handling. | ||
| CallableResolverTest::__invoke | public | function | A test __invoke method. | ||
| 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 | |||
| 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.