在phpunit测试中通过标签查找服务

时间:2019-06-13 15:09:41

标签: symfony phpunit symfony4

我正在测试我的symfony应用程序,并希望使用自定义标签访问所有服务,请参阅我的services.yml:

_instanceof:
   App\ReportPlaceholderInterface:
      tags: ['app.reportplaceholder']

只要我将它们作为参数注入到其他服务中,这些标签就会起作用:

App\Service\ReportHelper:
    arguments: [!tagged app.reportplaceholder]

在我的测试中,我想定义一个dataProvider:

class MyTest extends KernelTestCase{

    public function provider(){
        static::bootKernel();
        return static::$container->findTaggedServiceIds('app.reportplaceholder');

        /* this doesn't work as well, since the classes are in namespace App\ReportPlaceholder, and my tests live in namespace Tests */
        return array_map(function($p) { return [$p]; }, 
                array_filter(get_declared_classes(), function($className){
                     return in_array(ReportPlaceholderInterface::class, class_implements($className));}
        ));
    }

    /** @dataProvider provider */
    public function testPlaceholder($placeholderName){
        $placeholder = static::$container->get($placeholderName);
        ...
    }
}

我遇到此错误:

调用未定义的方法Symfony \ Bundle \ FrameworkBundle \ Test \ TestContainer :: findTaggedServiceIds()

0 个答案:

没有答案