Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解PHPUnit中的进口/使用_Php_Symfony_Phpunit - Fatal编程技术网

了解PHPUnit中的进口/使用

了解PHPUnit中的进口/使用,php,symfony,phpunit,Php,Symfony,Phpunit,在PHPUnit测试中,我试图获得实现某个接口的symfony应用程序的所有类。我的应用程序代码位于命名空间App,我的测试位于tests 如果我实例化(或“使用”)一个类,那么这个测试用例代码只列出一个类(顶部的use语句无效): phpunit.xml的内容如下: <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema

在PHPUnit测试中,我试图获得实现某个接口的symfony应用程序的所有类。我的应用程序代码位于命名空间
App
,我的测试位于
tests

如果我实例化(或“使用”)一个类,那么这个测试用例代码只列出一个类(顶部的use语句无效):

phpunit.xml的内容如下:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
     backupGlobals="false"
     colors="true"
     bootstrap="config/bootstrap.php"
     verbose="true"
     debug="false"
     stopOnFailure="true">

由于您使用的是
KernelTestCase
,因此您可以访问容器,通过标记实现接口的所有服务并通过以下方式(Symfony 3.4或更高版本)将其传递给注册表服务,您可以轻松地实现容器:

您的注册表类:

class MyCustomTagRegistry
{
  /** @var Traversable */
  private $services;

  public function __construct(iterable $services = [])
  {
    $this->services = $services;
  }

  public function getServices(): array
  {
    return (array) $this->services;
  }
}
然后在测试中,您需要从容器中获取给定的服务:

$services = self::$container->get(MyCustomTagRegistry::class)->getServices();
您可以在此处找到有关如何使用服务标签的更多详细信息:


我的解决方案与@peetya所设想的类似:我必须在
provider
中启动一个额外的内核来获取服务列表(然后立即再次关闭它):


这个解决方案是完美的,除了在构建内核和容器之前调用数据提供程序之外。但是我可以尝试在数据提供程序中启动一个虚拟内核,只是为了得到一个公开标记服务的服务……为什么不在
设置中启动内核呢,但是数据提供程序在所有测试函数之前调用一次,并且在每次
设置
调用之前调用一次,如果在
设置
之前调用了数据提供程序,那么您可以在启动后在
设置
中获取服务,并将它们存储在实例级别的数组中(
$this->services=self:$container->get)(MyCustomTagRegistry::class)->getServices();
),您不必使用dataprovider,只需在测试中在此数组上循环即可。是的,我想这样的解决方法是可以的。我应该接受从数据提供程序中的容器中获取数据不是一个好的测试设计。多亏了大家!结束这里
# services.yml
services:
  _instanceof:
    YourInterface:
      tags: ['my_custom_tag']

  App\Registry\MyCustomTagRegistry:
    arguments: [!tagged my_custom_tag]
class MyCustomTagRegistry
{
  /** @var Traversable */
  private $services;

  public function __construct(iterable $services = [])
  {
    $this->services = $services;
  }

  public function getServices(): array
  {
    return (array) $this->services;
  }
}
$services = self::$container->get(MyCustomTagRegistry::class)->getServices();
public function provider()
{
    $ret = [];
    self::bootKernel();
    foreach (static::$container->get('report_helper')->placeholders as $placeholder){
        /** @var $placeholder ReportPlaceholderInterface */
        $ret[] = [get_class($placeholder)];
    }
    static::ensureKernelShutdown();
    return $ret;
}