function CallableResolverTest::testCallbackResolver

Same name in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Utility/CallableResolverTest.php \Drupal\Tests\Core\Utility\CallableResolverTest::testCallbackResolver()

@covers ::getCallableFromDefinition @group legacy

File

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

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

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);
    }
}

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