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

Getting Started

Minimal Setup

  1. Installation:

    composer require friends-of-behat/test-context --dev
    

    Add to your behat.yml or behat.php:

    # behat.yml
    default:
        contexts:
            - FriendsOfBehat\TestContext\Context\TestContext
    

    Or programmatically:

    // behat.php
    return (new Config())->withProfile(
        (new Profile('default'))
            ->withSuite(
                (new Suite('default'))
                    ->withContexts(TestContext::class)
            )
    );
    
  2. First Use Case: Test a Behat extension by creating a feature file and context in a scenario:

    Feature: Extension Test
        Scenario: Basic extension test
            Given a Behat configuration containing:
            """
            default:
                extensions:
                    My\Extension: ~
            """
            And a feature file containing:
            """
            Feature: Test
                Scenario: Test
                    Then it passes
            """
            And a context file "features/bootstrap/MyContext.php" containing:
            """
            <?php
            use Behat\Behat\Context\Context;
            use Behat\Step\Then;
            class MyContext implements Context {
                #[Then('it passes')] public function itPasses() {}
            }
            """
            When I run Behat
            Then it should pass
    

Implementation Patterns

Workflows

  1. Extension Testing:

    • Use Given a Behat configuration containing to define extension dependencies.
    • Generate feature files and contexts dynamically with Given steps.
    • Run Behat and assert results with When I run Behat + Then assertions.
  2. Isolated Scenarios:

    • Leverage temporary directories per scenario to avoid file conflicts.
    • Example: Test configuration errors without polluting the real project:
      Scenario: Invalid extension config
          Given a Behat configuration containing:
          """
          default:
              extensions:
                  Invalid\Extension: ~
          """
          When I run Behat
          Then it should fail with "ExtensionInitializationException"
      
  3. Smoke Testing:

    • Use shorthand steps for quick validation:
      Scenario: Smoke test
          Given a feature file with passing scenario
          When I run Behat
          Then it should pass
      

Integration Tips

  • Laravel + Behat: Combine with Laravel’s testing tools (e.g., Artisan::call()) to mock dependencies in contexts.
  • CI/CD: Run Behat tests in parallel by isolating scenarios with unique temp directories.
  • Custom Contexts: Extend TestContext to add domain-specific setup steps (e.g., Laravel-specific configurations).

Gotchas and Tips

Pitfalls

  1. File Paths:

    • Use relative paths (e.g., "features/bootstrap/MyContext.php") for portability.
    • Absolute paths may break across environments.
  2. Behat Version Mismatch:

    • Ensure the test environment matches the target Behat version (e.g., behat.php for Behat 4).
    • Use behat.yml for Behat 3 or earlier.
  3. Output Assertions:

    • Then it should pass with "text" is strict—use Then it should end with "text" for partial matches.
  4. Symfony Autowiring:

    • If testing Symfony-based extensions, ensure the test environment has the same autowiring config as production.

Debugging

  • Verbose Output: Add --verbose to Behat commands in your test scenario to inspect generated files.
  • Temp Directory: Check $this->getWorkingDirectory() in custom contexts to debug file paths.

Tips

  1. Reusable Snippets:

    Background:
        Given a Behat configuration containing:
        """
        default:
            extensions:
                My\Extension: ~
        """
    

    Reduce boilerplate for extension tests.

  2. Multiline Strings: Use """ for YAML/feature file content to avoid escaping issues.

  3. Extension Points: Override TestContext to add Laravel-specific steps:

    class LaravelTestContext extends TestContext {
        public function __construct() {
            parent::__construct();
            $this->registerStep('Given a Laravel service provider', function() {
                // Custom logic
            });
        }
    }
    
  4. Performance:

    • Cache generated feature files/contexts if tests are slow (e.g., using FileCache).
    • Avoid heavy operations in Given steps—keep them lightweight.
  5. Edge Cases:

    • Test empty configurations:
      Scenario: Empty config
          Given a Behat configuration containing: ""
          When I run Behat
          Then it should pass
      
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