behatch/contexts
Reusable Behat 3 context library with ready-made steps for browser/Mink, REST, JSON, XML, tables, system commands, and debugging. Easy to install via Composer and enable in behat.yml, with configurable timeouts, screenshots, and evaluation modes.
#267) improves API testing capabilities, addressing a common gap in Laravel’s native testing tools (e.g., assertHeader() limitations in PHPUnit).#269) ensures cross-platform reliability, which is critical for CI/CD pipelines using diverse environments.testing facade changes) persists.HttpClient, Mailer). However, manual adaptation is still needed for:
Http facade vs. Behat’s Guzzle/Mink.actingAs() vs. Behat’s custom contexts.#267) directly addresses Laravel API testing pain points (e.g., validating dynamic headers like X-Request-ID).testing helpers (e.g., assertRedirect()->assertSession()).laravel-behat or similar official packages.HttpClient differences)?spatie/laravel-behat or custom solutions.)Http::fake() for hybrid testing.behat/contexts with Laravel’s actingAs():
// Custom LaravelContext.php
use Laravel\Sanctum\Testing\CreatesPersonalAccessTokens;
class LaravelContext extends \behat\contexts\LaravelContext {
use CreatesPersonalAccessTokens;
public function iAmLoggedInAs(string $user) {
$this->actingAs(User::where('name', $user)->first());
}
}
DatabaseTransactions over Behat’s setup/teardown.HttpClient interactions.Model events).assertHeader().behat/contexts:3.3.0 + Behat 3.x in a Laravel 9 project.X-RateLimit-Remaining).// config/behat.php
'extensions' => [
'Behat\MinkExtension' => [
'base_url' => 'http://laravel.test',
],
'behat\contexts' => [
'laravel' => [
'auth' => true,
'api' => true,
],
],
],
use Behat\Behat\Context\Context;
use Laravel\Sanctum\PersonalAccessToken;
class ApiContext implements Context {
public function __construct(private $client) {}
/**
* @Given the API response has header :header matching :regex
*/
public function assertHeaderMatches(string $header, string $regex) {
$this->client->assertHeader($header, $regex);
}
}
.feature files.behat --tags=api).Mailer), mock Behat’s Symfony dependencies to avoid conflicts.// app/Providers/BehatServiceProvider.php
public function register() {
$this->app->bind(\Behat\Behat\Definition\Definition::class, function () {
return new \Behat\Behat\Definition\Definition();
});
}
VerifyCsrfToken for Behat API tests:
// routes/api.php
$this->middleware('throttle:api')->group(function () {
// ...
});
behat/contexts.How can I help you explore Laravel packages today?