【问题标题】:Symfony2: Functional testing is failing during Fixtures loadSymfony2:在 Fixtures 加载期间功能测试失败
【发布时间】:2014-04-30 02:11:27
【问题描述】:

我正在对捆绑包进行一些功能测试,但遇到了一些问题。这是LoadFeeData.php的内容:

public function load(ObjectManager $manager) {
    for ($i = 0; $i < 10; $i++) {
        $fee = new Fee();
        $fee->setName("Comision-" . uniqid());
        $fee->setDescription($this->generateRandomString());
        $fee->setHoldback(1);
        $manager->persist($fee);
        $manager->flush();
    }
}

这就是我在测试中所做的:

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

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

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

但是任何时候我尝试这个命令:

 phpunit -c app/ src/Company/ApprovalBundle/Tests/Controller/CommissionCompanyControllerTest.php

我收到此错误:

1) Company\ApprovalBundle\Tests\Controller\CommissionCompanyControllerTest::testmodifyCommissionAction Doctrine\DBAL\DBALException:执行时发生异常 '从 ext_translations 中删除':

SQLSTATE[42S02]:未找到基表或视图:1146 表 “kraken.ext_translations”不存在

哪里出错了?

【问题讨论】:

  • 您是否运行过“app/console dictionary:schema:update --force”命令?

标签: php symfony testing phpunit functional-testing


【解决方案1】:

感谢@tomas-tibensky 的建议,我对代码进行了一些更改,现在可以根据需要加载固定装置(我现在需要学习如何在测试运行后删除它)但无论如何这里是解决方案,因为 DB 不是t 更新然后您需要运行命令doctrine:schema:update --force 来更新数据库架构,然后加载固定装置:

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase,
    Doctrine\Common\DataFixtures\Loader,
    Doctrine\Common\DataFixtures\Executor\ORMExecutor,
    Doctrine\Common\DataFixtures\Purger\ORMPurger,
    Symfony\Bundle\FrameworkBundle\Console\Application,
    Symfony\Component\Console\Input\StringInput
    Company\ApprovalBundle\Entity\CompanyHasWtax,
    Company\RegisterCompanyBundle\Entity\Company,
    Company\ApprovalBundle\DataFixtures\ORM\LoadFeeData;

class CommissionCompanyControllerTest extends WebTestCase {

    private $em;
    protected static $application;

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

        self::runCommand('doctrine:schema:update --force');

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

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

    protected static function runCommand($command) {
        $command = sprintf('%s --quiet', $command);

        return self::getApplication()->run(new StringInput($command));
    }

    protected static function getApplication() {
        if (null === self::$application) {
            $client = static::createClient();

            self::$application = new Application($client->getKernel());
            self::$application->setAutoExit(false);
        }

        return self::$application;
    }
}

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 2011-09-29
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多