Zeljko
Zeljko

Reputation: 5158

symfony2 behat in test enviroment: DB tables not created

I am trying to behat my application and I have a big problem; DB tables are not created so I can't put any fixtures.

My scenario is:

  Scenario: Check the stories page
    Given Database is set
      And I am logged as "admin" and password "123123123"
      And print last response
      ...

Part of FeatureContext:

/**
 * @Given /^Database is set$/
 */
public function databaseIsSet()
{
    $this->generateSchema() ;
    $admin = new User() ;
    $admin->setRoles(array(User::ROLE_SUPER_ADMIN)) ;
    $admin->setEnabled(true) ;
    $admin->setUsername("admin") ;
    $admin->setPlainPassword("123123123") ;
    $admin->setEmail("[email protected]") ;
    $em = $this->getEntityManager() ;
    $em->persist($admin) ;
    $em->flush() ;
            echo $admin->getId() . "==" ;
    echo "db set" ;
}
/**
 * @Given /^I am logged as "([^"]*)" and password "([^"]*)"$/
 */
public function iAmLoggedAsAndPassword($username, $password)
{
    return array(
        new Step\When('I am on "/login"'),
        new Step\When('I fill in "username" with "' . $username . '"'),
        new Step\When('I fill in "password" with "' . $password . '"'),
        new Step\When('I press "Login"'),
    );
}
protected function generateSchema()
{
    // Get the metadatas of the application to create the schema.
    $metadatas = $this->getMetadatas();

    if ( ! empty($metadatas)) {
        /**
        * @var \Doctrine\ORM\Tools\SchemaTool
        */
        $tool = new SchemaTool($this->getEntityManager());
//            $tool->dropDatabase() ;
        $tool->createSchema($metadatas);
    } else {
        throw new Doctrine\DBAL\Schema\SchemaException('No Metadata Classes to process.');
    }
}

/**
 * Overwrite this method to get specific metadatas.
 *
 * @return Array
 */
protected function getMetadatas()
{
    $result = $this->getEntityManager()->getMetadataFactory()->getAllMetadata() ;return $result;
}

protected function getEntityManager()
{
    return $this->kernel->getContainer()->get("doctrine")->getEntityManager() ;
}
 ....

The code for generateSchema is taken somewhere from internet and used in Phpunits tests I have and works perfectly.

But; when I run bin/behat, I get

 SQLSTATE[HY000]: General error: 1 no such table: tbl_user

after login part of scenario.

The echo statement I have is also shown in output, just to make sure the method is actually executed. Also, $admin gets an ID of 1 which is also visible in output.

My test env is using default sqlite DB, and it is irrelevant if I put 'http://mysite.local/app_dev.php/' or 'http://mysite.local/app_test.php/' for base_url in config; the login doesn't work although I copy&pasted it from knpLabs page. To make sure $admin is still in DB, I tried to reload it from repository and it works (I removed that part of code).

Help?

Upvotes: 1

Views: 1865

Answers (2)

Vincent Moulene
Vincent Moulene

Reputation: 1283

I had the same problem and for me the problem was in config_test.yml file.

I changed pdo_sqlite to pdo_mysql.

doctrine:
    dbal:
        driver: pdo_mysql
#        driver:   pdo_sqlite

And it works like a charm.

Upvotes: 0

Zeljko
Zeljko

Reputation: 5158

Actually, I found the problem. Sqlite works in-memory and upon each request to some page like login url, the previous state had been lost. I created new enviroment app_behat.php with these setting in config_behat.yml:

imports:
  - { resource: config.yml }

framework:
  test: ~
  session:
    storage_id: session.storage.mock_file

doctrine:
  dbal:
    dbname:   other_database

and it works now. Maybe someone will find this usefull.

Upvotes: 3

Related Questions