New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

soulcodex/laravel-behat

Laravel Extension for easy integration with Behat
71,178 17
Install
composer require soulcodex/laravel-behat
Latest Version:1.0.7
PHP:^8.0
Laravel:8 – 12
License:MIT
Last Updated:May 9, 2025
Links: GitHub  ·  Packagist
Maintainer: soulcodex
Project icon Project icon

Laravel Behat Extension

A powerfully extension to integrate laravel with behat from scratch and start writing great feature histories.

Getting started

1. Install Dependencies

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.

2. Create the Behat.yml Configuration File

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

3. Setting up a new context

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!

Features 🎁

💫 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();
    }
}
💫 Direct access to mink session and shortcut to perform GET request to specific url
$this->visitUrl($url); // Perform GET request to specific URI
$this->session(); // Access to the mink session to perform actions
💫 Runtime access to the container to get dependencies or do anything
$this->container(); // Get laravel application container access
💫 PHPUnit assertions from your context
$this->assertSame(...);
$this->assertEquals(...);
$this->assertInstanceOf(...);

FAQ ❓

Will be released new functionalities ?

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.

Could be possible contribute to help and maintain this package?

Yes, contact with me through email with subject Behat Extension - Contributor and send me the following data:

  • Full name 👋
  • GitHub link 💡 :octocat:
  • LinkedIn to connect 😁
  • The most important thing ➡️ ideas 💡

I'm getting a "PHP Fatal error: Maximum function nesting level of '100' reached, aborting!" error.

Sounds like you're using Xdebug. Increase the max nesting level.

How contribute / things pending to do 📄

  • 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 between KernelBrowser (default) or SeleniumBrowser implementation.
  • Create or amplify a base context for api and web approaches.

Related Packages

laracasts/behat-laravel-extension

Laravel extension for Behat

1,294,180 260
cevinio/behat-laravel-extension

Laravel extension for Behat

98,695 4
teoble/behat-lumen-extension

Lumen extension for Behat

5,405 3
arisro/behat-lumen-extension

Lumen extension for Behat

2,500 5
xedi/laravel-behat

Laravel Extension for Behat

1,551 1

Version History

Version Released PHP Laravel License
1.0.7 ^8.0 ^8.0|^9.0|^10.0|^11.0|^12.0 MIT
1.0.6 ^8.0|^8.1 ^8.0|^9.0|^10.0|^11.0 MIT
1.0.5 ^8.0|^8.1 ^8.0|^9.0|^10.0|^11.0 MIT
1.0.4 ^8.0|^8.1 ^8.0|^9.0|^10.0 MIT
1.0.3 ^8.0|^8.1 ^8.0|^9.0|^10.0 MIT