Search code examples
mysqlsymfonybehat

Symfony2 using second database while testing


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?


Solution

  • 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.