Behat和Symfony数据夹具

Ein*_*ius 7 php phpunit fixtures symfony behat

我想知道是否可以使用单独的数据库进行Behat测试?有没有办法像使用PHPUnit那样删除和截断数据库并加载数据夹具?

Mat*_*teo 4

您可以创建一个新环境(如测试、开发和生产)并以您喜欢的方式配置它。

作为配置 behat env 的示例,请执行以下操作:

  1. 在 web 文件夹中创建一个新文件,名为web_behat.php
  2. config_behat.yml在config文件夹中定义一个
  3. 自定义您的参数parameters_behat.yml

然后

  1. 在行为测试中使用此环境,您可以在其中加载固定装置等

我不知道你是否可以在行为场景中设置你的装置,但仅作为示例,你可以在命令行中执行类似的操作:

php app/console doctrine:fixture:load --env=behat
Run Code Online (Sandbox Code Playgroud)

详细步骤

  1. 在 web 文件夹中创建一个名为 web_behat.php 的新文件(从 web_dev.php 复制并删除 ip 限制)取决于您的 sf2 版本,但是:

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);
Run Code Online (Sandbox Code Playgroud)

在文件夹下定义 config_behat.yml,app/config您可以在其中使用所需的配置(数据库、电子邮件等)调用自定义参数,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

参数_behat.yml

parameters:
    database_driver:   pdo_mysql
    database_host:     localhost
    database_port:     ~
    database_name:     test_behat
    database_user:     root
    database_password:
Run Code Online (Sandbox Code Playgroud)

希望这有帮助