soulcodex / laravel-behat

A powerfully extension to integrate laravel with behat from scratch and start writing great feature histories.
7 stars 2 forks source link
behat behat-extension laravel laravel-package laravel-testing mink testing
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:

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:

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 ๐Ÿ“„