问题描述
使用Smyfony2和Doctrin2,可以使用以下示例创建数据夹具:
With Smyfony2 and Doctrin2, data fixtures can be created using the following example: http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
我想要的是能够使用这个概念测试使得设置/拆卸可以为功能测试创建纯测试数据环境。如何在功能测试中运行一组特定的测试夹具,以及如何将这些夹具与标准夹具分开,以便控制台命令忽略它们?
What I would like is to be able to use this concept for testing so that setup/teardown can create a pure test data environment for functional testing. How might I go about having a specific set of test-only fixtures run during functional tests and how do I separate these fixtures from my standard fixtures so that the console command ignores them?
似乎这样做的方法是复制教条的功能:fixtures console命令并将测试夹具存储在别处。有没有人有更好的解决方案?
It seems that the way to do it would be to replicate the functionality of the doctrine:fixtures console command and store the test fixtures elsewhere. Does anyone have a better solution?
推荐答案
根据目录打开fixtures的另一种方法是使用自定义的fixture类。您的夹具类将会扩展此类,并指定实际加载的环境。
An alternative to breaking out fixtures by directory is to use a custom fixture class. Your fixture classes would then extend this class and specify the environments it will actually be loaded in.
<?php
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Provides support for environment specific fixtures.
*
* This container aware, abstract data fixture is used to only allow loading in
* specific environments. The environments the data fixture will be loaded in is
* determined by the list of environment names returned by `getEnvironments()`.
*
* > The fixture will still be shown as having been loaded by the Doctrine
* > command, `doctrine:fixtures:load`, despite not having been actually
* > loaded.
*
* @author Kevin Herrera <[email protected]>
*/
abstract class AbstractDataFixture implements ContainerAwareInterface, FixtureInterface
{
/**
* The dependency injection container.
*
* @var ContainerInterface
*/
protected $container;
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/** @var KernelInterface $kernel */
$kernel = $this->container->get('kernel');
if (in_array($kernel->getEnvironment(), $this->getEnvironments())) {
$this->doLoad($manager);
}
}
/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* Performs the actual fixtures loading.
*
* @see \Doctrine\Common\DataFixtures\FixtureInterface::load()
*
* @param ObjectManager $manager The object manager.
*/
abstract protected function doLoad(ObjectManager $manager);
/**
* Returns the environments the fixtures may be loaded in.
*
* @return array The name of the environments.
*/
abstract protected function getEnvironments();
}
您的灯具最终将如下所示:
Your fixtures would end up looking like this:
<?php
namespace Vendor\Bundle\ExampleBundle\DataFixtures\ORM;
use AbstractDataFixture;
use Doctrine\Common\Persistence\ObjectManager;
/**
* Loads data only on "prod".
*/
class ExampleData extends AbstractDataFixture
{
/**
* @override
*/
protected function doLoad(ObjectManager $manager)
{
// ... snip ...
}
/**
* @override
*/
protected function getEnvironments()
{
return array('prod');
}
}
我相信这应该与ORM一起使用ODM数据夹具。
I believe that this should work with both ORM an ODM data fixtures.
这篇关于具有Symfony + Doctrine的环境特定数据夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!