29

我的 DoctrineFixturesBundle 已安装,我可以通过命令行加载夹具,但是,如何从我的功能测试中加载夹具?

4

4 回答 4

25

如果您使用 symfony 的WebTestCase,实际上有一个非常简单的方法来加载您的固定装置。您的夹具必须实现FixtureInterface; load()因此,您可以直接在测试方法中调用它的setUp()方法。您只需将 an 传递EntityManager给该load()方法,该方法可以从 symfony 容器中获取:

public function setUp() {
    $client = static::createClient();
    $container = $client->getContainer();
    $doctrine = $container->get('doctrine');
    $entityManager = $doctrine->getManager();

    $fixture = new YourFixture();
    $fixture->load($entityManager);
}
于 2013-12-05T12:55:38.903 回答
18

如您在此问题setUp()中所见,您可以在测试方法中加载固定装置。

您可以使用问题中的代码,但需要附加--appenddoctrine:fixtures:load命令中以避免固定装置确认。

更好的解决方案是查看LiipFunctionalTestBundle,它可以更轻松地使用数据夹具。

于 2013-06-13T16:05:17.380 回答
13

如果您想首先清除以前的测试数据表,例如,如果您在 phpunit 中运行测试,我只是想提供一种稍微简洁的方法。

use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Loader;
use Namespace\FakeBundle\DataFixtures\ORM\YourFixtures;

public function setUp()
{
    static::$kernel = static::createKernel();
    static::$kernel->boot();
    $this->em = static::$kernel->getContainer()
        ->get('doctrine')
        ->getManager()
    ;

    $loader = new Loader();
    $loader->addFixture(new YourFixtures);

    $purger = new ORMPurger($this->em);
    $executor = new ORMExecutor($this->em, $purger);
    $executor->execute($loader->getFixtures());

    parent::setUp();
}

这允许加载夹具(您可以将更多内容推入添加夹具方法),并在加载表之前清除它们。另请注意,MongoDB 使用 MongoDBPurger 和 MongoDBExecutor 具有相同的选项。希望它可以帮助某人

于 2014-02-17T14:59:00.580 回答
2

正如已经提到的,建议使用LiipFunctionalTestBundle。然后你想WebTestCaseLiip\FunctionalTestBundle\Test\WebTestCase. 这将允许调用$this->loadFixtures()以固定装置数组作为参数的调用。

$fixtures = array('Acme\MemeberBundle\DataFixtures\ORM\LoadMemberData');
$this->loadFixtures($fixtures);

有关更多详细信息,我写了一篇简短的博文

于 2014-04-07T21:18:25.070 回答