Stuff that helps you do BDD in a Laravel context., (*1)
This is stil WIP, but feel free to start using it in your projects!, (*2)
Install it trough Composer:, (*3)
"suhm/laravel-bdd-helpers": "dev-master"
The Laravel Behat context trait makes a Laravel Application
instance available to you in your Behat features., (*4)
Use it like this:, (*5)
<?php use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; /** * Behat context class. */ class FeatureContext implements SnippetAcceptingContext { use LaravelBdd\Behat\Laravel; /** * Initializes context. * * Every scenario gets its own context object. * You can also pass arbitrary arguments to the context constructor through behat.yml. */ public function __construct() { $this->prepareApplication(__DIR__.'/../../bootstrap/start.php'); } }
Now you can interact with the Application
instance like you can with Laravel's default TestCase
., (*6)
By default, the Laravel context will set the environment to testing
. If you need to set the environment to something else, or maybe you aren't using the Laravel context trait but still need to load your test configuration files., (*7)
The Environment
trait includes a method called setEnvironment()
. The first parameter is the name of the environment, and defaults to testing
. The second parameter is the name of the environment var to export, and defaults to APP_ENV
., (*8)
You can set the environment name of your application and optionally export it:, (*9)
<?php use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; /** * Behat context class. */ class FeatureContext implements SnippetAcceptingContext { use LaravelBdd\Behat\Environment; use LaravelBdd\Behat\Laravel; /** * Initializes context. * * Every scenario gets its own context object. * You can also pass arbitrary arguments to the context constructor through behat.yml. */ public function __construct() { $this->setEnvironment(); // Default env is 'testing', export 'APP_ENV=testing' $this->setEnvironment('acceptance'); // Set env to 'acceptance' and export 'APP_ENV=acceptance' $this->setEnvironment('testing', 'SOMETING'); // Set env to testing and export 'SOMETHING=testing' $this->prepareApplication(__DIR__.'/../../bootstrap/start.php'); } }
If you need to hit routes and controller actions, you can use this trait to get an instance of the Laravel HTTP client., (*10)
You can then do stuff like this:, (*11)
<?php use Behat\Behat\Context\SnippetAcceptingContext; use Behat\Gherkin\Node\PyStringNode; use Behat\Gherkin\Node\TableNode; /** * Behat context class. */ class FeatureContext implements SnippetAcceptingContext { use LaravelBdd\Behat\Laravel; use LaravelBdd\Behat\HttpClient; /** * Initializes context. * * Every scenario gets its own context object. * You can also pass arbitrary arguments to the context constructor through behat.yml. */ public function __construct() { $this->prepareApplication(__DIR__.'/../../bootstrap/start.php'); $this->createClient(); } /** * @When I visit :uri */ public function iVisit($uri) { $this->client->request('GET', $uri); }
For real life examples of doing BDD in a Laravel context, please see this repository: https://github.com/petersuhm/laravel-bdd, (*12)