Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Common Contexts Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps for Laravel Integration

  1. Verify Compatibility:

    • Confirm your project uses Behat 2.x (unlikely in Laravel; typically Behat 3.x+ or Pest is used). If not, skip this package entirely.
    • Check if your stack includes Symfony 2.x + Doctrine ORM v2.x (Laravel rarely uses these).
  2. 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).

  3. Basic Setup (Symfony 2.x only):

    • Extend your FeatureContext as shown in the README:
      use Behat\CommonContexts\SymfonyDoctrineContext;
      
      class FeatureContext extends BehatContext
      {
          public function __construct()
          {
              $this->useContext('symfony_doctrine', new SymfonyDoctrineContext());
          }
      }
      
    • Use in a @BeforeScenario hook:
      /**
       * @BeforeScenario
       */
      public function resetDatabase()
      {
          $this->getSubcontext('symfony_doctrine')->buildSchema();
      }
      
  4. Laravel Workaround (High Risk):

    • If you must use this in Laravel, mock the Symfony container:
      $this->useContext('symfony_doctrine', new SymfonyDoctrineContext(
          $this->getContainer()->get('doctrine') // Hypothetical; Laravel uses Eloquent
      ));
      
    • Expect failures: This package assumes Symfony’s Kernel and ContainerInterface, which Laravel lacks.

Implementation Patterns

Workflows for Laravel/Behat 2.x Projects

  1. Database Fixtures:

    • Use DoctrineFixturesContext to load ORM fixtures before scenarios:
      $this->getSubcontext('doctrine_fixtures_context')->loadFixtureClasses(
          new Loader(),
          [YourFixtureClass::class]
      );
      
    • Laravel Alternative: Use DatabaseSeeder or laravel-shift/database-seeder.
  2. Schema Management:

    • Reset the database schema with SymfonyDoctrineContext:
      $this->getSubcontext('symfony_doctrine_context')->buildSchema();
      
    • Laravel Alternative: Run php artisan migrate:fresh in phpunit.xml or use RefreshDatabase trait.
  3. Email Testing:

    • Capture sent emails with SymfonyMailerContext:
      $this->getSubcontext('symfony_mailer_context')->assertEmailSent(
          'user@example.com',
          'Welcome Email'
      );
      
    • Laravel Alternative: Use Mail::fake() in tests.

Integration Tips

  • Symfony 2.x Bridge:
    • If using Symfony 2.x alongside Laravel (e.g., legacy modules), inject the Symfony container into Behat contexts:
      $this->useContext('symfony_doctrine', new SymfonyDoctrineContext(
          $this->getKernel()->getContainer()
      ));
      
  • Avoid Mixing with Laravel:
    • Do not use this package for new Laravel projects. Instead, leverage:
      • Pest PHP for simpler testing.
      • Laravel Test Tools (spatie/laravel-test-tools) for fixtures/mail testing.
  • Hooks:
    • Place context initialization in your FeatureContext constructor.
    • Use @BeforeScenario/@AfterScenario hooks for setup/teardown.

Gotchas and Tips

Pitfalls

  1. Behat 2.x Dependency:

    • This package will not work with Behat 3.x+ or Laravel’s default Behat setup. Attempting to install it may break your vendor/ dependencies.
    • Fix: Use composer require --dev behat/common-contexts:dev-master only in a dedicated legacy branch.
  2. Symfony-Specific Assumptions:

    • Assumes 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).
    • Fix: Fork the package and rewrite contexts for Laravel (e.g., replace Swiftmailer with Laravel’s Mail facade).
  3. Doctrine ORM Version:

    • Requires Doctrine ORM v2.x. Laravel typically uses:
      • Eloquent (not Doctrine).
      • Doctrine DBAL (v3.x) for raw SQL.
    • Fix: Use Laravel’s Schema::dropAllTables() or RefreshDatabase trait.
  4. No Laravel Service Provider:

    • The package lacks Laravel integration (e.g., no register() method in a service provider).
    • Fix: Manually bind contexts in BehatServiceProvider:
      $this->app->bind('symfony_doctrine_context', function () {
          return new SymfonyDoctrineContext($this->app['kernel']->getContainer());
      });
      
  5. Archived Status:

    • No updates since 2015. Security vulnerabilities in dependencies (e.g., old Behat/Symfony versions) may exist.
    • Fix: Audit dependencies with composer why-not behat/common-contexts and composer audit.

Debugging Tips

  1. Dependency Conflicts:

    • If Composer fails, run:
      composer why-not behat/common-contexts
      
    • Resolve conflicts by locking Behat 2.x versions in composer.json:
      "require-dev": {
          "behat/behat": "2.5.*",
          "behat/common-contexts": "dev-master"
      }
      
  2. Context Not Found:

    • Ensure the context is registered in the constructor:
      $this->useContext('symfony_doctrine', new SymfonyDoctrineContext());
      
    • Verify the subcontext name matches when calling:
      $this->getSubcontext('symfony_doctrine')->buildSchema();
      
  3. Symfony Container Errors:

    • If $this->kernel is undefined, your project is not a Symfony application. Laravel does not provide this by default.
    • Workaround: Mock the container or use Laravel’s app() helper (not recommended).
  4. Fixture Loading Failures:

    • Ensure Loader and ORMExecutor classes are available. These are Symfony-specific and may not work in Laravel.
    • Fix: Use Laravel’s DatabaseSeeder or Factory classes instead.

Extension Points

  1. Custom Contexts:

    • Extend existing contexts (e.g., DoctrineFixturesContext) to add Laravel-specific methods:
      class LaravelDoctrineContext extends DoctrineFixturesContext
      {
          public function loadEloquentFixtures(array $fixtures)
          {
              // Custom logic for Laravel Eloquent
          }
      }
      
  2. Behat 3.x Migration:

    • If you must use this package, migrate to Behat 3.x first, then adapt contexts:
      // Behat 3.x Context Interface
      use Behat\Testwork\Hook\Scope\ScenarioScope;
      
      class SymfonyDoctrineContext implements Context
      {
          public function __construct(private ScenarioScope $scope) {}
          // ...
      }
      
  3. Laravel Service Integration:

    • Bind contexts to Laravel’s container in a service provider:
      $this->app->singleton('symfony_doctrine_context', function () {
          return new SymfonyDoctrineContext(
              $this->app->make('kernel')->getContainer()
          );
      });
      
    • Note: This is fragile and not recommended for new projects.

Performance Quirks

  • Fixture Loading:

    • DoctrineFixturesContext loads fixtures via Doctrine’s ORMExecutor, which can be slow for large datasets. In Laravel, consider:
      • Using Factory::new() for faster test data generation.
      • Caching fixtures with 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony