Einius Einius - 5 days ago 5
PHP Question

Behat and Symfony data fixtures

I'm wondering is it possible to use separate database for the Behat tests? is there any way to drop and truncate database and load data fixtures with it like you can do with PHPUnit?

Answer

You can create a new environment (like test, dev and prod) and configure it in your preferred way.

As example for configure a behat env, do the following:

  1. create a new file in the web folder called web_behat.php
  2. define a config_behat.yml in the config folder
  3. customize your parameters in the parameters_behat.yml

Then

  1. Use this env in your behat test where you can load your fixtures etc

I don't know as you can setup your fixtures in a behat scenario, but only as example you can do something like this in a command line:

php app/console doctrine:fixture:load --env=behat

DETAILED STEP

  1. create a new file in the web folder called web_behat.php (copying it from web_dev.php and remove ip restrictions ) depends on your sf2 versions but:

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);

Define a config_behat.yml under app/config folder where you recall your custom parameters with the desired configurations (database, email etc) something like this:

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:

Hope this help

Comments