如何在phpunit symfony中加载datafixtures hautelook alice?

时间:2017-11-12 04:39:01

标签: php symfony nelmio-alice

我尝试在DefaultControllerTest中添加此代码

$load = new Loader();
$load->load('src/AppBundle/DataFixtures/ORM/product.yml');

这是我的控制器的完整代码

<?php

namespace Tests\AppBundle\Controller;

use Nelmio\Alice\Fixtures\Loader;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DefaultControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $load = new Loader();
        $load->load('src/AppBundle/DataFixtures/ORM/product.yml');
        $client = static::createClient();

        $crawler = $client->request('GET', '/');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertContains('Welcome to Symfony', $crawler->filter('#container h1')->text());
    }
}

如果我运行phpunit。它工作正常,没有发现错误。它成功测试但问题是product.yml没有在我的数据库中插入任何数据。但如果我运行此命令 bin / console hautelook_alice:doctrine:fixtures:load --append 。这将有效。它插入数据。在测试控制器之前,如何加载数据固定?我试着更多地研究它。但我现在不知道如何添加它。

1 个答案:

答案 0 :(得分:2)

您可以简单地使用bash脚本。这样的事情(适应你的需要):

#!/bin/bash
echo "# Refresh data model and load fixtures"
php bin/console doctrine:database:create --if-not-exists --env=dev
php bin/console doctrine:schema:drop --force --env=dev
php bin/console doctrine:schema:create --env=dev
php bin/console doctrine:schema:validate --env=dev
php bin/console hautelook_alice:doctrine:fixtures:load --append --env=dev
echo -e " --> DONE\n"

然后你可以启动将使用这个新数据库的phpunit。或者您可以在此脚本中添加phpunit调用:

./bin/simple-phpunit --debug --verbose
相关问题