我想知道是否可以为Behat测试使用单独的数据库?有没有办法像使用PHPUnit一样删除和截断数据库并加载数据夹具?
答案 0 :(得分:4)
您可以创建新环境(例如test,dev和prod)并按照您的首选方式对其进行配置。
作为配置behat env的示例,请执行以下操作:
web_behat.php
config_behat.yml
parameters_behat.yml
然后
我不知道你可以在一个behat场景中设置你的灯具,但仅作为例子,你可以在命令行中做这样的事情:
php app/console doctrine:fixture:load --env=behat
<强> web_behat.php 强>
<?php
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
// Use APC for autoloading to improve performance
// Change 'sf2' by the prefix you want in order to prevent key conflict with another application
/*
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
*/
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('behat', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
在app/config
文件夹下定义一个config_behat.yml,您可以在其中调用具有所需配置(数据库,电子邮件等)的自定义参数,如下所示:
<强> config_behat.yml 强>
imports:
- { resource: config.yml }
- { resource: parameters_behat.yml }
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
assetic:
use_controller: true
framework:
test: ~
parameters:
router.options.matcher.cache_class: ~ # disable router cache
router.options.matcher_class: Symfony\Component\Routing\Matcher\ApacheUrlMatcher
<强> parameters_behat.yml 强>
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: ~
database_name: test_behat
database_user: root
database_password:
希望这个帮助
答案 1 :(得分:1)
在您的behat中设置自定义环境,并将容器+实体管理器注入要素上下文(或只是容器并从那里加载)。不相关的设置未显示。
<强> Behat.yml 强>
default:
suites:
default:
contexts:
- FeatureContext:
container: '@service_container'
entityManager: "@doctrine.orm.default_entity_manager"
extensions:
Behat\Symfony2Extension:
kernel:
env: "test"
<强> FeatureContext 强>
务必用你的$loader->loadFromDirectory
)替换灯具路径。
<?php
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @var EntityManager
*/
private $em;
/**
* FeatureContext constructor.
* @param ContainerInterface $container
* @param EntityManager $entityManager
*/
public function __construct(ContainerInterface $container, EntityManager $entityManager)
{
$this->container = $container;
$this->em = $entityManager;
$this->setupDatabase();
$this->loadFixtures();
}
protected function setupDatabase()
{
$metaData = $this->em->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool($this->em);
$schemaTool->dropDatabase();
if (!empty($metaData)) {
$schemaTool->createSchema($metaData);
}
}
protected function loadFixtures()
{
$loader = new ContainerAwareLoader($this->container);
$loader->loadFromDirectory(__DIR__ . '/../../src/AppBundle/DataFixtures');
$executor = new ORMExecutor($this->em);
$executor->execute($loader->getFixtures(), true);
}
}