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

Test Context Laravel Package

friends-of-behat/test-context

Reusable Behat context for testing Behat extensions by running “Behat inside Behat”. Creates isolated temp projects per scenario, writes configs/features/contexts, runs a real Behat process, and lets you assert on exit codes and output.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Purpose Alignment: The package is explicitly designed for testing Behat extensions, making it a perfect fit for Laravel projects leveraging Behat (e.g., for BDD-driven development, API testing, or custom Behat extensions).
  • Isolation Model: The package’s scenario-level temporary directory isolation ensures clean, non-overlapping test environments—ideal for Laravel’s dependency-heavy ecosystem (e.g., testing extensions like Laravel\Behat or custom Behat\Mink drivers).
  • Laravel Synergy: While not Laravel-specific, it integrates seamlessly with Laravel’s testing stack (PHPUnit, Pest) and Behat configurations (e.g., behat.yml in tests/Features/).

Integration Feasibility

  • Low Friction: Requires zero Laravel-specific modifications—just install via Composer and register the TestContext in behat.yml or behat.php.
  • Dependency Compatibility:
    • Supports Behat 3.x–4.x (Laravel’s ecosystem typically uses Behat 3.x).
    • Works with PHP 8.1+ (Laravel 10+ default) and Symfony 6–8 (used in Laravel’s testing utilities).
    • No conflicts with Laravel’s core testing tools (e.g., laravel/browsing-testing or laravel/pint).
  • Customization Points:
    • Can extend TestContext to mock Laravel-specific services (e.g., App\Services\AuthService) in nested Behat runs.
    • Supports custom step definitions for Laravel-specific assertions (e.g., "Then the response should be JSON").

Technical Risk

  • False Positives in Output Matching:
    • Risk: Assertions like Then it should fail with "text" may misfire if Laravel’s error output (e.g., stack traces) includes dynamic data (e.g., UUIDs, timestamps).
    • Mitigation: Use regex patterns in assertions or preprocess output with Laravel’s Str::of().
  • Performance Overhead:
    • Risk: Spawning Behat processes per scenario adds ~50–200ms latency per test (negligible for small suites, but noticeable in CI with 100+ tests).
    • Mitigation: Cache vendor/ and node_modules/ in CI; use behat --no-interaction for faster runs.
  • Laravel-Specific Edge Cases:
    • Risk: Testing Laravel-specific Behat extensions (e.g., Laravel\Behat\ServiceContainer\LaravelExtension) may require mocking Laravel’s service container in nested Behat runs.
    • Mitigation: Use TestContext's file setup to inject Laravel’s services.php or aliases.php into the nested config.

Key Questions

  1. Behat Version:
    • Is the Laravel project using Behat 3.x or 4.x? (Package supports both, but syntax differs for behat.yml vs. behat.php.)
  2. Extension Complexity:
    • Are extensions tightly coupled to Laravel (e.g., database queries, auth)? If so, how will nested Behat runs access Laravel’s environment?
  3. CI/CD Impact:
    • Will the additional process spawning slow down CI? (Benchmark with a sample suite.)
  4. Maintenance:
    • Who will update the package if Laravel’s Behat ecosystem evolves (e.g., new Mink drivers)?
  5. Alternatives:
    • Could Laravel’s built-in testing helpers (e.g., actingAs(), assertDatabaseHas()) replace some Behat extension tests?

Integration Approach

Stack Fit

  • Primary Use Case:
    • Testing custom Behat extensions (e.g., Laravel-specific steps like "Given I am logged in as a user").
    • Validating Behat configurations (e.g., ensuring Laravel\Behat\ServiceContainer\LaravelExtension loads correctly).
  • Secondary Use Case:
    • Smoke testing Laravel’s BDD workflow (e.g., verifying feature files render without syntax errors).
  • Compatibility Matrix:
    Component Laravel Version Behat Version PHP Version Notes
    friends-of-behat/test-context 8.x–11.x 3.x–4.x 8.1+ Full support
    Laravel Behat Ext. 8.x–11.x 3.x 8.0+ May need mocking for nested runs
    Mink Drivers N/A 3.x 8.1+ Test with laravel/browsing-testing

Migration Path

  1. Phase 1: Proof of Concept (1–2 days)
    • Install the package in a dedicated tests/Features/Extensions/ directory.
    • Test a single extension (e.g., a custom step definition) using the package’s shorthand steps.
    • Example:
      Feature: Laravel Auth Extension
        Scenario: Login step works
          Given a feature file with passing scenario
          And a context file "bootstrap/AuthContext.php" containing:
            """
            <?php
            use Laravel\Behat\Context\AuthContext;
            class AuthContext extends \Behat\Behat\Context\Context {}
            """
          When I run Behat
          Then it should pass
      
  2. Phase 2: Full Integration (3–5 days)
    • Refactor existing Behat tests to use TestContext for extension-specific logic.
    • Mock Laravel services in nested runs (e.g., inject App\Providers\AuthServiceProvider via Given a file).
    • Update CI pipeline to cache vendor/ and node_modules/ for nested Behat runs.
  3. Phase 3: Optimization (Ongoing)
    • Parallelize tests using Behat’s --tags or Laravel’s parallel-tests package.
    • Add custom steps for Laravel-specific assertions (e.g., "Then the session contains 'laravel_session'").

Compatibility

  • Laravel-Specific Considerations:
    • Service Container: Use TestContext to inject Laravel’s services.php into nested configs:
      Given a file "config/services.php" containing:
        """
        <?php return [
            'auth' => App\Services\AuthService::class,
        ];
        """
      
    • Environment Variables: Pass .env.testing via Given a file or use Laravel’s putenv() in a custom step.
    • Database: For testing DB-dependent extensions, use Laravel’s DatabaseMigrations or DatabaseTransactions in the outer test suite.
  • Tooling Conflicts:
    • Avoid: Running TestContext alongside laravel/horizon or laravel/fortify (may conflict with nested Behat processes).
    • Workaround: Use --no-interaction and mock queues/auth in nested runs.

Sequencing

  1. Prerequisite: Ensure the Laravel project has:
    • A stable Behat setup (e.g., behat.yml configured for Laravel extensions).
    • PHPUnit/Pest tests for non-BDD logic (to isolate BDD-specific tests).
  2. Order of Adoption:
    • Start with extension tests (highest ROI).
    • Then move to configuration validation (e.g., testing behat.yml includes).
    • Finally, smoke tests for feature files.
  3. Dependency Graph:
    [Laravel Core] → [Behat 3.x] → [friends-of-behat/test-context] → [Custom Extensions]
    

Operational Impact

Maintenance

  • Pros:
    • Reduced Boilerplate: Eliminates manual setup of temp directories, Behat configs, and cleanup.
    • Self-Documenting Tests: Gherkin syntax clearly describes test intent (e.g., "Given a failing scenario").
    • Community Support: Actively maintained (last release 2026, Symfony 8 support).
  • Cons:
    • Indirect Dependencies: Changes to Behat 4.x or Symfony 8 may require package updates.
    • Debugging Complexity: Nested Behat runs add layers to stack traces (e.g., "Behat called from Behat").
  • Maintenance Tasks:
    • Quarterly: Update the package to match Laravel’s Behat version.
    • Annual: Review custom steps for Laravel-specific deprecations (e.g., Auth::routes()use Laravel\Fortify).

Support

  • Troubleshooting:
    • Common Issues:
      • Exit Code Mismatches: Use Then it should end with "text" to debug output.
      • File Path Errors: Verify paths in `Given
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle