setono/sylius-behat-pack
Dev pack for running Behat tests in Sylius apps and plugins. Pulls in common Behat/Mink tooling and documents a JS-enabled setup using Chromedriver, Selenium, and the Symfony CLI web server for test environments.
Install the Package
composer require --dev setono/sylius-behat-pack
Note: Ensure your composer.json includes sylius/sylius or a Sylius plugin as a dependency.
Set Up Environment
java -jar /usr/local/bin/selenium.jar > /dev/null 2>&1 &
APP_ENV=test symfony server:start --port=8080 --daemon
Configure Behat
Copy the default behat.yml from the package:
cp vendor/setono/sylius-behat-pack/Resources/config/behat.yml.dist config/autoload/behat.yml
Update paths to point to your Laravel/Sylius Features directory:
suites:
default:
paths: [ %paths.base%/tests/Features ]
filters: { tags: "@default" }
Run a Basic Test
Create a feature file (e.g., tests/Features/homepage.feature):
Feature: Homepage
In order to access the storefront
As a visitor
I want to see the homepage
Scenario: Visit homepage
Given I am on the homepage
Then I should see "Welcome"
Run Behat:
vendor/bin/behat
Laravel-Specific Adjustments
tests/Features/Context/ if needed.Artisan commands in Behat via SymfonyExtension:
Given I run artisan command "migrate:fresh --env=test"
.feature files (e.g., tests/Features/checkout.feature).Sylius\Behat\Context\Setup\AdminUserContext) for admin flows.// tests/Features/Context/LaravelContext.php
namespace Tests\Features\Context;
use Behat\Behat\Context\Context;
use Laravel\BrowserKitTesting\TestCase;
class LaravelContext implements Context {
protected $testCase;
public function __construct(TestCase $testCase) {
$this->testCase = $testCase;
}
/**
* @Given I am logged in as a user with email :email
*/
public function iAmLoggedInAsUserWithEmail($email) {
$this->testCase->actingAs(\App\Models\User::whereEmail($email)->first());
}
}
Laravel\BrowserKitTesting\TestCase as a base for Behat contexts:
use Laravel\BrowserKitTesting\TestCase as LaravelTestCase;
class MyContext extends LaravelTestCase implements Context {
// ...
}
Given there is a product named "Test Product"
with price 10.00
(Implement the step in a context using Laravel’s factory() helper.)Dockerfile to spin up dependencies:
FROM selenium/standalone-chrome:latest
# Add your app code and run Behat
jobs:
behat:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker-compose up -d selenium
- run: composer install
- run: vendor/bin/behat --tags ~@wip
suites:
plugin:
paths: [ %paths.base%/tests/Features/Plugin ]
filters: { tags: "@plugin" }
composer.json includes the plugin’s test requirements:
composer require --dev vendor/plugin-name
Tagging Strategies Use Behat tags to organize tests:
@wip @checkout
Scenario: Checkout as guest
Run specific tags:
vendor/bin/behat --tags @checkout
Debugging Tools
mink:
selenium2:
browser: chrome
capabilities:
chromeOptions:
args: ["--headless", "--disable-gpu", "--screenshot"]
behat.yml:
symfony2:
debug_toolbar: true
Performance Optimization
mink:
base_url: http://localhost:8080
sessions:
default:
selenium2:
capabilities: { "browserName": "chrome" }
--parallel flag (requires Selenium Grid).Laravel-Specific Shortcuts
Given I run artisan command "migrate:fresh --env=test"
// In a context
public function beforeScenario(ScenarioInterface $scenario) {
$this->testCase->getDatabaseConnection()->beginTransaction();
}
public function afterScenario(ScenarioInterface $scenario) {
if ($scenario->getResult()->isFailed()) {
$this->testCase->getDatabaseConnection()->rollBack();
} else {
$this->testCase->getDatabaseConnection()->commit();
}
}
Sylius-Specific Assumptions
Product, Order). Override or extend them for Laravel models:
// Avoid direct Sylius entity usage; map to Laravel models
public function thereIsAProductNamed($name) {
$product = \App\Models\Product::whereName($name)->firstOrFail();
// ...
}
sylius-fixtures; Laravel uses factories. Bridge them:
// Convert Sylius fixture to Laravel factory
public function thereIsAUserWithEmail($email) {
\App\Models\User::factory()->create(['email' => $email]);
}
Environment Quirks
--no-sandbox and --disable-dev-shm-usage flags in CI:
capabilities:
chromeOptions:
args: ["--headless", "--no-sandbox", "--disable-dev-shm-usage"]
4444 for Selenium, 8080 for Symfony).Dependency Hell
behat/behat, mink/mink, and sylius/sylius versions in composer.json to avoid conflicts:
"require-dev": {
"behat/behat": "3.7",
"mink/mink": "1.8",
"sylius/sylius": "1.12.*"
}
AppKernel. For Laravel, mock the kernel or use Laravel\BrowserKitTesting\TestCase.Flaky Tests
behat.yml:
mink:
selenium2:
capabilities:
timeouts: { script: 30000, pageLoad: 30000 }
public function iShouldSee($text, $max
How can I help you explore Laravel packages today?