behat/common-contexts
Abandoned Behat 2.x add-on providing reusable subcontexts with extra steps/hooks. Includes contexts like SymfonyMailerContext, DoctrineFixturesContext, and SymfonyDoctrineContext for loading fixtures, purging DB, and resetting schema in Symfony-based suites.
Verify Compatibility:
Temporary Installation (for legacy projects only):
composer require --dev behat/common-contexts:dev-master
Note: This may fail due to dependency conflicts (e.g., Behat 2.x vs. Laravel’s Behat 3.x).
Basic Setup (Symfony 2.x only):
FeatureContext as shown in the README:
use Behat\CommonContexts\SymfonyDoctrineContext;
class FeatureContext extends BehatContext
{
public function __construct()
{
$this->useContext('symfony_doctrine', new SymfonyDoctrineContext());
}
}
@BeforeScenario hook:
/**
* @BeforeScenario
*/
public function resetDatabase()
{
$this->getSubcontext('symfony_doctrine')->buildSchema();
}
Laravel Workaround (High Risk):
$this->useContext('symfony_doctrine', new SymfonyDoctrineContext(
$this->getContainer()->get('doctrine') // Hypothetical; Laravel uses Eloquent
));
Kernel and ContainerInterface, which Laravel lacks.Database Fixtures:
DoctrineFixturesContext to load ORM fixtures before scenarios:
$this->getSubcontext('doctrine_fixtures_context')->loadFixtureClasses(
new Loader(),
[YourFixtureClass::class]
);
DatabaseSeeder or laravel-shift/database-seeder.Schema Management:
SymfonyDoctrineContext:
$this->getSubcontext('symfony_doctrine_context')->buildSchema();
php artisan migrate:fresh in phpunit.xml or use RefreshDatabase trait.Email Testing:
SymfonyMailerContext:
$this->getSubcontext('symfony_mailer_context')->assertEmailSent(
'user@example.com',
'Welcome Email'
);
Mail::fake() in tests.$this->useContext('symfony_doctrine', new SymfonyDoctrineContext(
$this->getKernel()->getContainer()
));
spatie/laravel-test-tools) for fixtures/mail testing.FeatureContext constructor.@BeforeScenario/@AfterScenario hooks for setup/teardown.Behat 2.x Dependency:
vendor/ dependencies.composer require --dev behat/common-contexts:dev-master only in a dedicated legacy branch.Symfony-Specific Assumptions:
KernelInterface, ContainerInterface, and Symfony’s SwiftmailerBundle. Laravel uses:
Illuminate\Foundation\Application (not KernelInterface).Illuminate\Mail\Mailer (not Symfony Mailer).Illuminate\Database\Eloquent\Model (not Doctrine ORM).Swiftmailer with Laravel’s Mail facade).Doctrine ORM Version:
Schema::dropAllTables() or RefreshDatabase trait.No Laravel Service Provider:
register() method in a service provider).BehatServiceProvider:
$this->app->bind('symfony_doctrine_context', function () {
return new SymfonyDoctrineContext($this->app['kernel']->getContainer());
});
Archived Status:
composer why-not behat/common-contexts and composer audit.Dependency Conflicts:
composer why-not behat/common-contexts
composer.json:
"require-dev": {
"behat/behat": "2.5.*",
"behat/common-contexts": "dev-master"
}
Context Not Found:
$this->useContext('symfony_doctrine', new SymfonyDoctrineContext());
$this->getSubcontext('symfony_doctrine')->buildSchema();
Symfony Container Errors:
$this->kernel is undefined, your project is not a Symfony application. Laravel does not provide this by default.app() helper (not recommended).Fixture Loading Failures:
Loader and ORMExecutor classes are available. These are Symfony-specific and may not work in Laravel.DatabaseSeeder or Factory classes instead.Custom Contexts:
DoctrineFixturesContext) to add Laravel-specific methods:
class LaravelDoctrineContext extends DoctrineFixturesContext
{
public function loadEloquentFixtures(array $fixtures)
{
// Custom logic for Laravel Eloquent
}
}
Behat 3.x Migration:
// Behat 3.x Context Interface
use Behat\Testwork\Hook\Scope\ScenarioScope;
class SymfonyDoctrineContext implements Context
{
public function __construct(private ScenarioScope $scope) {}
// ...
}
Laravel Service Integration:
$this->app->singleton('symfony_doctrine_context', function () {
return new SymfonyDoctrineContext(
$this->app->make('kernel')->getContainer()
);
});
Fixture Loading:
DoctrineFixturesContext loads fixtures via Doctrine’s ORMExecutor, which can be slow for large datasets. In Laravel, consider:
Factory::new() for faster test data generation.laravel-shift/database-seeder.Schema Rebuilds:
SymfonyDoctrineContext::buildSchema() drops and recreates the entire database. In Laravel, use:
$this->artisan('migrate:fresh --env=testing');
for faster, transaction-based resets.How can I help you explore Laravel packages today?