Question

I'm working in some functional testing for a bundle and I'm having some issues. This is the content of 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();
    }
}

And this is what I do in my test:

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());
}

But any time I try the command:

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

I get this error:

1) Company\ApprovalBundle\Tests\Controller\CommissionCompanyControllerTest::testmodifyCommissionAction Doctrine\DBAL\DBALException: An exception occurred while executing 'DELETE FROM ext_translations':

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'kraken.ext_translations' doesn't exist

Where is the error?

Was it helpful?

Solution

Thanks to @tomas-tibensky suggestions I made some changes to my code and now fixtures are loaded as I want (I need to learn now how to delete it after test runs) but anyway here is the solution, since DB isn't updated then your need to run the command doctrine:schema:update --force to update DB schema and then load the fixtures:

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top