正如标题所述,我想知道如何在方法setUpBeforeClass中加载数据夹具。测试类扩展了Liip\FunctionalTestBundle\Test\WebTestCase。
现在我有这样的想法:
public function setUp()
{
$this->client = $this->createClient();
$this->fixtures = $this->loadFixtures([
'App\DataFixtures\MyFixtures',
// more fixtures
])->getReferenceRepository();
}然而,测试似乎花费了太长的时间,而且实际上没有必要在每次测试之前加载这些夹具。
当我试图在setUpBeforeClass中加载夹具时,我得到了一个错误:
错误:在/home/cezar/phpprojects/livegene/vendor/liip/functional-test-bundle/src/Test/WebTestCase.php:252中未在对象上下文中使用$this
查看LiipFunctionalTestBundle的源代码就会发现以下代码片段:
protected function loadFixtures(array $classNames = [], bool $append = false, ?string $omName = null, string $registryName = 'doctrine', ?int $purgeMode = null): ?AbstractExecutor
{
$container = $this->getContainer();
$dbToolCollection = $container->get('liip_functional_test.services.database_tool_collection');
$dbTool = $dbToolCollection->get($omName, $registryName, $purgeMode, $this);
$dbTool->setExcludedDoctrineTables($this->excludedDoctrineTables);
return $dbTool->loadFixtures($classNames, $append);
}我是否可以做到这一点,如果是的话,如何才能实现呢?
发布于 2019-02-26 16:10:35
如果您所需要的只是一个具有(部分)有效数据库模式以进行查询的工作EntityManager,则可以使用Symfony DoctrineBridge提供的DoctrineTestHelper:
public static function setUpBeforeClass()
{
$config = DoctrineTestHelper::createTestConfiguration();
$config->setNamingStrategy(new UnderscoreNamingStrategy());
$entityManager = DoctrineTestHelper::createTestEntityManager($config);
$schemaTool = new SchemaTool($entityManager);
$schemaTool->createSchema([
// List of entities to create schema for
$entityManager->getClassMetadata(User::class),
$entityManager->getClassMetadata(Task::class),
]);
static::$entityManager = $entityManager;
}默认情况下,这将在内存中为连接使用SQLite3,但也可以使用配置和适当的驱动程序将其指向任何其他数据库。还要注意注册任何定制的DBAL类型和LifecycleEvent-侦听器,因为这将改变数据的处理方式以及到实体的映射是否有效。
现在,在您的测试中,您可以像往常一样对表使用static::$entityManager,或者根据需要插入测试夹具。
https://stackoverflow.com/questions/54885564
复制相似问题