Symfony2:在Fixtures加载期间功能测试失败

时间:2014-03-22 16:28:09

标签: php symfony testing phpunit functional-testing

我正在进行一些功能测试,我遇到了一些问题。这是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)   公司\ ApprovalBundle \测试\控制器\ CommissionCompanyControllerTest :: testmodifyCommissionAction   Doctrine \ DBAL \ DBALException:执行时发生异常   '从ext_translations删除':

     

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

错误在哪里?

1 个答案:

答案 0 :(得分:1)

感谢@ tomas-tibensky建议我对我的代码进行了一些更改,现在我想要加载灯具(我现在需要学习如何在测试运行后删除它)但是无论如何这里是解决方案,因为DB不是&# 39; 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;
    }
}