2013-01-20 29 views
1

我想要知道我的应用程序,并且遇到很大问题;数据库表没有创建,所以我不能把任何灯具。在测试环境中的symfony2 behat:数据库表格未创建

我的情况是:

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/' for base_url放在config中,这是无关紧要的;登录不起作用,虽然我复制&从knpLabs页面粘贴它。为了确保$ admin仍然在数据库中,我尝试从存储库重新加载它并且它可以工作(我删除了那部分代码)。

帮助?

回答

3

其实我发现了这个问题。 Sqlite在内存中运行,并且在每次请求某个页面(如login url)时,之前的状态都已丢失。我在config_behat.yml创造了新的环境app_behat.php这些设置:

imports: 
    - { resource: config.yml } 

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

doctrine: 
    dbal: 
    dbname: other_database 

和现在的工作。也许有人会发现这个有用的。