[英]Symfony2 + Functional Testing “DataFixtures load how set the proper order”
[英]Symfony 4: how to load DataFixtures in a KernelTestCase
我设置了DataFixtures,可以通过控制台将其加载到测试数据库中。
$ php bin/console doctrine:fixtures:load --env=test -n
> purging database
> loading App\DataFixtures\PropertyFixtures
> loading App\DataFixtures\UserFixtures
> loading App\DataFixtures\UserPropertyFixtures
像chram一样工作
但是我不知道如何在服务单元测试中自动加载这些灯具,而不必在测试之前手动运行命令。 必须有另一种方式!
到目前为止,我发现的是使用较旧版本的symfony进行测试或对Controller进行测试的描述。 如果可以避免,谁仍然想测试Controller?
Liip \\ FunctionalTestBundle似乎也仅适用于WebTestCases,至少我没有看到扩展或替换常规KernelTestCase的方法。
那么,有什么方法可以通过测试类的setUp()方法运行命令?
任何链接到symfony 4的教程? 有服务示例吗? 我无法想象,我是唯一遇到这个问题的人。
我得到了一个解决方案,尽管这里并不充满美感和风格。 人们赞赏更优雅的选择。
我使用名为AppKernel的类扩展了在Kernel.php中找到的Kernel类。 要激活它以进行测试,并且仅激活那些我更改了phpunit.xml文件的文件:
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\AppKernel" />
因此,现在仅对于测试加载此类。
在AppKernel类中,我将启动方法扩展如下:
public function boot()
{
parent::boot();
$this->importDataFixtures();
}
/**
* Loads the tests data for DataFixtures when we start phpUnit.
*/
protected function importDataFixtures()
{
system('php /var/www/octopus/bin/console doctrine:fixtures:load --env=test -n');
}
因此,通过系统进行导入的调用当然很丑陋,但是可以。 如果有人有更好的主意,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.