function CallableResolverTest::testCallbackResolver

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

@covers ::getCallableFromDefinition

File

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

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' => [
            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);
    }
}

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