1

我尝试将此代码添加到我的 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。这将起作用。它插入数据。在测试控制器之前如何加载数据夹具?我尝试对此进行更多研究。但我现在不知道如何添加它。

4

1 回答 1

1

您可以简单地使用 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
于 2017-11-12T09:18:02.363 回答