测试环境中的symfony2 behat:未创建数据库表

问题描述 投票:1回答:2

我试图做我的申请,我有一个大问题;数据库表没有创建,所以我不能放任何灯具。

我的情况是:

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

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

generateSchema的代码是从互联网的某个地方获取并用于我拥有的Phpunits测试并且工作得很好。

但;当我跑bin/behat时,我明白了

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

登录部分方案后。

我输出的echo语句也显示在输出中,只是为了确保方法实际执行。此外,$ admin获取的ID为1,在输出中也可见。

我的测试环境使用的是默认的sqlite数据库,如果我在配置中将'http://mysite.local/app_dev.php/''http://mysite.local/app_test.php/'用于base_url则无关紧要;登录不起作用,虽然我从knpLabs页面复制并粘贴它。为了确保$ admin仍在DB中,我尝试从存储库重新加载它并且它可以工作(我删除了那部分代码)。

救命?

symfony behat mink
2个回答
3
投票

实际上,我发现了这个问题。 Sqlite在内存中工作,并且在每个请求到login url之类的某个页面时,之前的状态已经丢失。我在app_behat.php中使用这些设置创建了新的环境config_behat.yml

imports:
  - { resource: config.yml }

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

doctrine:
  dbal:
    dbname:   other_database

它现在有效。也许有人会觉得这很有用。


0
投票

我有同样的问题,对我来说问题是在config_test.yml文件中。

我把pdo_sqlite换成了pdo_mysql

doctrine:
    dbal:
        driver: pdo_mysql
#        driver:   pdo_sqlite

它就像一个魅力。

© www.soinside.com 2019 - 2024. All rights reserved.