soyuka/contexts
Behatch contexts is a Behat extension providing ready-made contexts for browser, debug, system, JSON, table, REST, and XML testing. Configure the contexts you need in behat.yml to write common Behat tests quickly.
Installation Add the package via Composer (v3.4.1 supports PHP 8.4 / 8.5, so you can run it on modern runtimes):
composer require soyuka/contexts
Register the extension in your behat.yml:
extensions:
Soyuka\Contexts\Extension: ~
First Use Case
Define a custom context class extending Soyuka\Contexts\Context:
use Soyuka\Contexts\Context;
class FeatureContext extends Context
{
public function iSeeSomething($text)
{
// Your logic here
}
}
Reference it in your .feature file:
@javascript
Feature: Example
Scenario: Test something
Given I see something "Hello"
Key Files to Review
src/Context.php (Base class for custom contexts)src/Extension.php (Configuration and hooks)src/ServiceContainer/ContextExtension.php (Dependency injection)Context Organization
AuthContext, PaymentContext).namespace App\Features\Auth;
Step Reusability
class BaseApiContext extends Context
{
protected function makeRequest($method, $endpoint, $data = [])
{
// Shared HTTP logic
}
}
Data-Driven Testing
When I fill form with:
| field | value |
| email | [email protected] |
public function iFillFormWith(array $data)
{
foreach ($data as $field => $value) {
// ...
}
}
Integration with Laravel
use Illuminate\Contracts\Auth\Guard;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
Hooks for Setup/Teardown
beforeScenario()/afterScenario() for shared setup:
protected function beforeScenario()
{
$this->auth->loginUsingId(1); // Seed user
}
Custom Step Matchers
/**
* @Then /^I see "([^"]*)" in "([^"]*)"$/;
*/
public function iSeeIn($text, $element)
{
// ...
}
Parameter Types
behat.yml:
definitions:
parameters:
user:
type: User
class: App\Models\User
Event Dispatching
event(new UserRegistered($user));
Mocking Services
$this->mock(Auth::class)->shouldReceive('check')->andReturn(true);
Artisan Commands
$this->runArtisan('migrate:fresh');
Step Name Conflicts
Given I log in vs. Given I login).@auth Given I log in as admin).State Management
Performance
beforeScenario() can slow down tests.beforeFeature().Debugging
throw new \Exception("Debug message") for visibility.Laravel Environment
testing environment by default..env or use app()->setEnvironment('testing').JSON Assertion Messages (v3.4.1)
^3.4.1 if your JSON steps previously failed with a generic "assertion false" and you spent time bisecting the response body.Enable Verbose Output
Add to behat.yml:
default:
suites:
default:
filters:
tags: "@wip"
extensions:
Soyuka\Contexts\Extension:
verbose: true
Logging Inject the logger:
use Illuminate\Log\Logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
$this->logger->debug("Context initialized");
}
Step Tracing
Use dump() or dd() sparingly (outputs to Behat’s console):
public function iDoSomething()
{
dump($this->someVariable);
}
Trust the New JSON Assertion Output
dump() the response.Custom Parameter Types
Extend Soyuka\Contexts\ParameterType for domain-specific types.
Hooks Override:
beforeFeature()afterFeature()beforeScenario()afterScenario()Service Providers Bind custom services to the container:
$this->getServiceContainer()->set('custom.service', function () {
return new CustomService();
});
Event Listeners Attach listeners to Behat events:
$this->getServiceContainer()->getParameter('event_dispatcher')->addListener(
'beforeScenario',
[$this, 'customSetup']
);
Configuration
Override default settings in behat.yml:
extensions:
Soyuka\Contexts\Extension:
base_context: App\Features\BaseContext
verbose: false
How can I help you explore Laravel packages today?