class CallableResolverTest

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php \Drupal\Tests\Core\Utility\CallableResolverTest

@coversDefaultClass \Drupal\Core\Utility\CallableResolver @group Utility

Hierarchy

Expanded class hierarchy of CallableResolverTest

File

core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php, line 18

Namespace

Drupal\Tests\Core\Utility
View 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
     */
    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' => [
                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);
        }
    }
    
    /**
     * @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' => [
                [
                    \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) {
        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 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 @covers ::getCallableFromDefinition
CallableResolverTest::testCallbackResolverExceptionHandling public function @dataProvider callableResolverExceptionHandlingTestCases
@covers ::getCallableFromDefinition
CallableResolverTest::__invoke public function A test __invoke 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::setUpBeforeClass public static function

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