soyuka/contexts
Add lightweight context handling to PHP/Laravel apps with soyuka/contexts. Store and retrieve per-request or runtime context data (like user, locale, tracing IDs) in a clean API to simplify logging, debugging, and cross-cutting concerns without global state.
Installation Add the package via Composer:
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 | user@example.com |
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').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);
}
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?