1

Im using behat for testing in my symfony2 aplication. Right now I need to have two databases. First (which is working right now), for normal use, like user doing something on site. Second database (which exists and behat work on it), for tests purpose.

What I got now is two working databases, and behat. Behat use second database, but problem is that while tests flow, site doesnt use it.

My config.yml:

doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

My config_test.yml:

doctrine:
    dbal:
        dbname:   "%database_name%_test"

My 'behat.yml':

default:
suites:
    default:
        paths:
            features: '%paths.base%/features'
            bootstrap:  '%paths.base%/features/bootstrap'
        contexts:
            - FeatureContext: ~
            - EwidencjaContext:
                userManager: '@fos_user.user_manager'
                em: '@doctrine.orm.entity_manager'
                packageManager: '@em.package_manager'
extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
        base_url:  http://my_nginx/app_test.php
        goutte: ~
        selenium2:
            browser: "chrome"
            wd_host: http://selenium_chrome:4444/wd/hub
            capabilities: { "browserName": "chrome", "browser": "chrome"}
    Bex\Behat\ScreenshotExtension:
        image_drivers:
            local:
                screenshot_directory: tests/features/images/
                clear_screenshot_directory: true

What can I do to change database for time tests are in progress?

6
  • What do you mean with site doesn't use it? Is that while executing functional tests or when browsing to the site to test Commented Mar 23, 2017 at 8:40
  • It doesnt use test database on the site. Commented Mar 23, 2017 at 8:53
  • To use it on your site you should make sure the kernel uses the test environment. Just like the dev environment is used in app_dev.php Commented Mar 23, 2017 at 8:58
  • I had no app_test.php file at all, I created one, copied everything from app_dev.php, and changed just one line for $kernel = new AppKernel('test', true);. It still doesnt work, and when i go on localhost/app_test.php it just download the file, not like when I go on localhost/app_dev.php, when i got on site in dev invironment. Commented Mar 23, 2017 at 9:23
  • Check your webserver configuration symfony.com/doc/current/setup/web_server_configuration.html Commented Mar 23, 2017 at 9:49

1 Answer 1

1

This uses SQLite for Behat (test) environment but you can use MySQL if you want to.

config_test.yml

doctrine:
    dbal:
        connections:
            default:
                driver: pdo_sqlite
                path: %kernel.cache_dir%/default.db
                charset: UTF8

app_test.php

The relevant line should be: $kernel = new AppKernel('test', true);

AppKernel.php

The relevant line should be: if (in_array($this->getEnvironment(), ['dev', 'test'])) {

behat.yml

Pay attention to app_test.php.

default:
    extensions:
        Behat\Symfony2Extension: ~
        Behat\MinkExtension:
            base_url: http://your_app_domain.dev/app_test.php
        ...
        ...
        ...

Virtual hosts settings:

<VirtualHost *:80>
   ServerName your_app_domain.dev
   DocumentRoot "/path/to/your/app/web"

   <Directory "/path/to/your/app/web">
       Options Indexes FollowSymlinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

   ErrorLog ${APACHE_LOG_DIR}/your_app_domain.dev.error.log
   CustomLog ${APACHE_LOG_DIR}/your_app_domain.dev.access.log combined
</VirtualHost>

Some information:

Outcome:

If you call http://your_app_domain.dev/app_dev.php it will use default DB settings in config but if you call http://your_app_domain.dev/app_test.php then it will use config_test settings.

Sign up to request clarification or add additional context in comments.

2 Comments

Along with it I had to change one line in vhost.conf to location ~ ^/(app_dev|app|app_test|config)\.php(/|$) { couse it was lacking an app_test part. Now everything works good, thanks. And im using mysql. Now that i read about it, seems like sqlite would be better choice. Im going to see how to set it up, thanks for help.
@Megami Just a note, I see you are dependency injecting services to EwidencjaContext. In the case of behat, your context should implement KernelAwareContext and access services&parameters from $container. An example is here: Installing behat3 with composer.json. This way you don't need to inject anything to your context files from behat.yml and keep behat.yml as clean as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.