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.
## Getting Started
### **First Steps**
1. **Installation**
Add the package via Composer (ensure compatibility with Laravel/Behat versions):
```bash
composer require --dev behatch/contexts:^3.3.0
Register the extension in behat.yml:
extensions:
Behat\Contexts\Extension: ~
Basic Usage
The package provides predefined context classes (e.g., FeatureContext, ApiContext, DatabaseContext) with autoloading. No manual setup is required for basic usage.
New: Supports Symfony 5 (useful if using Symfony components in Laravel).
First Test Case
Use built-in contexts in .feature files:
Feature: Example
Scenario: Regex header matching
Given I have a response with headers matching regex "/Content-Type: application\/json/"
Then the response should match the regex
New: Added regex matcher for headers (e.g., matching regex steps).
FeatureContext, ApiContext, DatabaseContext, etc.features/bootstrap/:
// features/bootstrap/MyCustomContext.php
use Behat\Contexts\Context\FeatureContext;
class MyCustomContext extends FeatureContext {
public function assertHeaderRegex(string $regex) {
// Use new regex matcher
}
}
Given I have a user).Then the response header "Content-Type" should match regex "/application\/json/"
Implementation:
public function assertHeaderMatchesRegex($header, $regex) {
$this->assertMatchesRegex($regex, $this->getResponseHeader($header));
}
ApiContext for HTTP assertions (now supports regex for headers/responses).DatabaseContext for DB assertions (regex support for queries/results).$app->bind('Behat\Contexts\Context\FeatureContext', function ($app) {
return new FeatureContext($app['db'], $app['auth']);
});
app(), route(), or view() inside contexts.abstract class BaseContext {
protected $user;
public function loginAsUser() { /* ... */ }
}
public function assertMatchesRegex($regex, $subject) {
if (!preg_match($regex, $subject)) {
throw new \Exception("Regex '$regex' failed for '$subject'");
}
}
behat-laravel-extension or codeception for long-term projects.behat.yml with Laravel’s config carefully./ → \/).preg_match('/your_regex/', $subject);
features/bootstrap/ or vendor/behat/contexts/src/.private $regexCache = [];
public function assertMatchesRegex($regex, $subject) {
$pattern = $this->regexCache[$regex] ?? preg_quote($regex);
// ...
}
public function afterScenario() {
DB::rollBack();
}
behat.yml:
behatch:
step_definitions:
- features/steps/regex_steps.php
BeforeScenario/AfterScenario for setup/teardown:
public function beforeScenario(Scenario $scenario) {
Auth::logout();
}
BeforeScenario:
public function beforeScenario(Scenario $scenario) {
Cache::flush();
}
Given the following users exist:
| email | name |
| test@example.com | John |
Then the following headers should match:
| header | regex |
| Content-Type | application\/json |
Then the response header "Authorization" should match regex "/Bearer [a-z0-9\-_]+/"
public function assertHeaderMatchesRegex($header, $regex) {
$this->assertMatchesRegex($regex, $this->getResponseHeader($header));
}
NO_UPDATE_NEEDED would **not** apply here—this assessment is updated to reflect the new release’s features (Symfony 5 compatibility, regex matcher) and fixes (Windows JSON schema paths).
How can I help you explore Laravel packages today?