A powerfully extension to integrate laravel with behat from scratch and start writing great feature histories. |
As always, we need to pull in some dependencies through Composer.
composer require behat/behat behat/mink friends-of-behat/mink-extension soulcodex/laravel-behat --dev
This will give us access to Behat, Mink, and, of course, the Laravel extension.
Next, within your project root, create a behat.yml
file, and add:
default:
extensions:
Soulcodex\Behat:
kernel: # Default values
bootstrap_path: '/bootstrap/app.php'
environment_path: '.env.behat'
Behat\MinkExtension: # Default mink extension configuration
default_session: laravel
laravel: ~
# Your test suites here
suites:
user:
paths: [ '%paths.base%/path/to/your/features/tests/files' ]
# The context needed by your features tests
contexts: ~
Here, is where we reference the Laravel extension, and tell Behat to use it as our default session. You may pass an
optional parameter, env_path
(currently commented out above) to specify the name of the environment file that should
be referenced from your tests. By default, it'll look for a .env.behat
file.
This file should, like the standard .env
file in your project root, contain any special environment variables
for your tests (such as a special acceptance test-specific database).
Create a new one context in the directory specified in the paths
configuration property using
the base context and register the RootContext class like this:
behat.yaml
suites:
user:
paths: [ '%paths.base%/app/User/Test/Feature/' ]
# The context needed by your features tests
contexts:
- Soulcodex\Behat\Addon\RootContext: ~
- App\User\Test\Feature\UserContext:
userRepository: '@App\User\Infrastructure\Persistence\Eloquent\EloquentMySqlUserRepository'
app/User/Test/Feature/UserContext.php
<?php
declare(strict_types=1);
namespace App\User\Test\Feature;
use App\User\Domain\UserRepository;
use Soulcodex\Behat\Addon\Context;
final class UserContext extends Context
{
public function __construct(private UserRepository $userRepository)
{
}
/**
* @Given I send a request to :url
*/
public function iSendARequestTo(string $url): void
{
$this->visitUrl($url); // Perform an action using the mink session
}
}
Note: Its recommended use from time been
Start writing your features test with Behat. ¡Happy coding!
💫 Plug & Play philosophy, just create you context, extends from base context and start your feature context.
<?php
use Soulcodex\Behat\Addon\Context;
final class MyMarvelousContext extends Context
{
/**
* @Given I do a marvelous action in my application
*/
public function iDoAMarvelousActionInMyApp(): void
{
$this->doMarvelousThings();
}
}
$this->visitUrl($url); // Perform GET request to specific URI
$this->session(); // Access to the mink session to perform actions
$this->container(); // Get laravel application container access
$this->assertSame(...);
$this->assertEquals(...);
$this->assertInstanceOf(...);
Yes, of course but i need help and support in order to maintain and upscale the package according this bullet points:
- Transform this package in more than a simple behat setup package.
- Improve and make easier the developer life working in favor of Plug & Play philosophy.
- Push in favor of BDD pattern as great way/approach to have aligned business and technical layers.
Yes, contact with me through email with subject Behat Extension - Contributor
and
send me the following data:
- Full name 👋
- GitHub link 💡
- LinkedIn to connect 😁
- The most important thing ➡️ ideas 💡
Sounds like you're using Xdebug. Increase the max nesting level.
- Implement a good and readable CHANGELOG using this library or another one.
- Add test coverage for all laravel version matrix using GitHub Actions.
- Automatize test and package release to packagist.
- Add usefully traits to give
Plug & Play
tools. ⏱ in progress - Add
mode
to configuration to let choose betweenKernelBrowser (default)
orSeleniumBrowser
implementation. - Create or amplify a base context for
api
andweb
approaches.