change env when acceptance testing laravel app with codeception and selenium

1.3k views Asked by At

I'm trying to write some acceptance tests for laravel 4 with codeception and the selenium module.

I got two problems with that.

  • The first one is that my app is running in the homestead vagrant vm and the selenium server is running on the host machine. So is there a easy way to run the selenium server in the vm and the call the browser o n the host machine ?

  • My second problem is that when testing the actual live database is used, because the environment of the laravel app is not set to testing. Obviously i would like to have it to use the test database and reset it after each test.

codeception.yaml

actor: Tester
paths:
    tests: app/tests
    log: app/tests/_output
    data: app/tests/_data
    helpers: app/tests/_support
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
    suite_class: \PHPUnit_Framework_TestSuite
modules:
    config:
        Db:
            dsn: 'sqlite:app/tests/_data/testdb.sqlite'
            user: ''
            password: ''
            dump: app/tests/_data/dump.sql

acceptance.yaml

class_name: AcceptanceTester
modules:
    enabled: [WebDriver,AcceptanceHelper]
    config:
        WebDriver:
            url: 'http://app.dev'
            browser: firefox
            window_size: 1920x1024
            wait: 10 
1

There are 1 answers

0
Matt A On

The easy way to run acceptance tests in the vm is to use phantom js in ghostdriver mode. There is a tutorial here:

https://gist.github.com/antonioribeiro/96ce9675e5660c317bcc

You can still see screenshots and the rendered html when tests fail, so it isn't a big deal that you can't see the browser.

For your second question, I prefer to keep a separate tests installation with it's own database. That way changes you make in dev don't alter your test results and it's a better approximation of production.

If you want to use the same installation, you can automate switching out your settings with .env files.

http://laravel.com/docs/4.2/configuration#protecting-sensitive-configuration

your config would look like this:

'host'     => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],

and your .env.php would look like:

return array( 'DB_HOST' => 'hostname', 'DB_NAME' => '' ..etc

than you can use a task runner like robo to automatically update your .env file and run codeception tests.

http://robo.li/

$this->replaceInFile('.env.php')
    ->from('production_db_name')
    ->to('test_db_name')
     ->run();

$this->taskCodecept()->suite('acceptance')->run();

.env files are changing in Laravel 5, but this workflow still works with minimal modification.

http://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables