I'm trying to get some Behavior tests integrated into my API test suite. I'm leveraging Laravel 4.2 and already have a nice suite of unit tests. The problem I'm running into is persistent data after these tests suite run - as well as correctly populated seed data.
I've tried to link sqlite into my bootstrap process following a few examples I've seen in various places online, but all this is really doing is setting up my DB when I call behat from CLI - during the application run (most specifically any curl requests out to the API) laravel still ties my DB to my local configuration which uses a mysql db.
here's an example snippet of a test suite, the Adding a new track
scenario is one where I'd like to have my API use the sqlite when the request and payload is made to my API.
Feature: Tracks
Scenario: Finding all the tracks
When I request "GET /api/v1/tracks"
Then I get a "200" response
Scenario: Finding an invalid track
When I request "GET /api/v1/tracks/1"
Then I get a "404" response
Scenario: Adding a new track
Given I have the payload:
"""
{"title": "behat add", "description": "add description", "short_description": "add short description"}
"""
When I request "POST /api/v1/tracks"
Then I get a "201" response
Here is a snippet of my bootstrap/start.php file. What I'm I am trying to accomplish is for my behat scenario (ie: Adding a new track) request to hit the testing config so I can manage w/ a sqlite db.
$env = $app->detectEnvironment(array(
'local' => array('*.local'),
'production' => array('api'),
'staging' => array('api-staging'),
'virtualmachine' => array('api-vm'),
'testing' => array('*.testing'),
));
Laraval does not know about Behat. Create a special environment for it, with its own database.
Here is what I have in my start.php:
if (getenv('APP_ENV') && getenv('APP_ENV') != '')
{
$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV');
});
}
else
{
$env = $app->detectEnvironment(array(
'local' => array('*.local', 'homestead'),
/* ... */
));
}
APP_ENV is set in your Apache/Nginx VirtualHost config. For apache:
SetEnv APP_ENV acceptance
Create a special local test URL for Behat to use and put that in the VirtualHost entry.
I recommend using an SQLite file-based database. Delete the file before each Feature or Scenario. Found it to be much quicker than MySQL. I want to use the SQLite in-memory mode but I could not find a way to persist data between requests with the in-memory database.