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

Sylius Behat Pack Laravel Package

setono/sylius-behat-pack

Dev pack for running Behat tests in Sylius apps and plugins. Pulls in common Behat/Mink tooling and documents a JS-enabled setup using Chromedriver, Selenium, and the Symfony CLI web server for test environments.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps for Laravel + Sylius Integration

  1. Install the Package

    composer require --dev setono/sylius-behat-pack
    

    Note: Ensure your composer.json includes sylius/sylius or a Sylius plugin as a dependency.

  2. Set Up Environment

    • Install Chromedriver and Selenium (as per README).
    • Start Selenium server:
      java -jar /usr/local/bin/selenium.jar > /dev/null 2>&1 &
      
    • Launch Symfony server (for local testing):
      APP_ENV=test symfony server:start --port=8080 --daemon
      
  3. Configure Behat Copy the default behat.yml from the package:

    cp vendor/setono/sylius-behat-pack/Resources/config/behat.yml.dist config/autoload/behat.yml
    

    Update paths to point to your Laravel/Sylius Features directory:

    suites:
      default:
        paths: [ %paths.base%/tests/Features ]
        filters: { tags: "@default" }
    
  4. Run a Basic Test Create a feature file (e.g., tests/Features/homepage.feature):

    Feature: Homepage
      In order to access the storefront
      As a visitor
      I want to see the homepage
    
      Scenario: Visit homepage
        Given I am on the homepage
        Then I should see "Welcome"
    

    Run Behat:

    vendor/bin/behat
    
  5. Laravel-Specific Adjustments

    • Override Sylius contexts in tests/Features/Context/ if needed.
    • Use Laravel’s Artisan commands in Behat via SymfonyExtension:
      Given I run artisan command "migrate:fresh --env=test"
      

Implementation Patterns

Workflows for Laravel Developers

1. Feature Development Workflow

  • Write Gherkin Features First: Define user stories in .feature files (e.g., tests/Features/checkout.feature).
  • Leverage Sylius Contexts: Reuse Sylius-specific contexts (e.g., Sylius\Behat\Context\Setup\AdminUserContext) for admin flows.
  • Extend for Laravel Logic: Create custom contexts for Laravel-specific features:
    // tests/Features/Context/LaravelContext.php
    namespace Tests\Features\Context;
    
    use Behat\Behat\Context\Context;
    use Laravel\BrowserKitTesting\TestCase;
    
    class LaravelContext implements Context {
        protected $testCase;
    
        public function __construct(TestCase $testCase) {
            $this->testCase = $testCase;
        }
    
        /**
         * @Given I am logged in as a user with email :email
         */
        public function iAmLoggedInAsUserWithEmail($email) {
            $this->testCase->actingAs(\App\Models\User::whereEmail($email)->first());
        }
    }
    

2. Integration with Laravel Testing

  • Combine with Laravel TestCase: Use Laravel\BrowserKitTesting\TestCase as a base for Behat contexts:
    use Laravel\BrowserKitTesting\TestCase as LaravelTestCase;
    
    class MyContext extends LaravelTestCase implements Context {
        // ...
    }
    
  • Share Fixtures: Use Laravel’s factories in Behat:
    Given there is a product named "Test Product"
      with price 10.00
    
    (Implement the step in a context using Laravel’s factory() helper.)

3. CI/CD Pipeline Integration

  • Dockerize Selenium/Chromedriver: Use a Dockerfile to spin up dependencies:
    FROM selenium/standalone-chrome:latest
    # Add your app code and run Behat
    
  • GitHub Actions Example:
    jobs:
      behat:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: docker-compose up -d selenium
          - run: composer install
          - run: vendor/bin/behat --tags ~@wip
    

4. Testing Sylius Plugins in Laravel

  • Plugin-Specific Suites: Isolate plugin tests in a separate suite:
    suites:
      plugin:
        paths: [ %paths.base%/tests/Features/Plugin ]
        filters: { tags: "@plugin" }
    
  • Load Plugin Dependencies: Ensure composer.json includes the plugin’s test requirements:
    composer require --dev vendor/plugin-name
    

Tips for Daily Use

  1. Tagging Strategies Use Behat tags to organize tests:

    @wip @checkout
    Scenario: Checkout as guest
    

    Run specific tags:

    vendor/bin/behat --tags @checkout
    
  2. Debugging Tools

    • Screenshots on Failure: Configure Mink to take screenshots:
      mink:
        selenium2:
          browser: chrome
          capabilities:
            chromeOptions:
              args: ["--headless", "--disable-gpu", "--screenshot"]
      
    • Symfony Debug Toolbar: Enable in behat.yml:
      symfony2:
        debug_toolbar: true
      
  3. Performance Optimization

    • Reuse Browser Sessions: Configure Mink to reuse sessions:
      mink:
        base_url: http://localhost:8080
        sessions:
          default:
            selenium2:
              capabilities: { "browserName": "chrome" }
      
    • Parallel Testing: Use Behat’s --parallel flag (requires Selenium Grid).
  4. Laravel-Specific Shortcuts

    • Artisan Commands: Run migrations or seeders in Behat:
      Given I run artisan command "migrate:fresh --env=test"
      
    • Database Transactions: Wrap scenarios in transactions:
      // In a context
      public function beforeScenario(ScenarioInterface $scenario) {
          $this->testCase->getDatabaseConnection()->beginTransaction();
      }
      
      public function afterScenario(ScenarioInterface $scenario) {
          if ($scenario->getResult()->isFailed()) {
              $this->testCase->getDatabaseConnection()->rollBack();
          } else {
              $this->testCase->getDatabaseConnection()->commit();
          }
      }
      

Gotchas and Tips

Pitfalls

  1. Sylius-Specific Assumptions

    • Context Conflicts: Sylius contexts assume Sylius entities (e.g., Product, Order). Override or extend them for Laravel models:
      // Avoid direct Sylius entity usage; map to Laravel models
      public function thereIsAProductNamed($name) {
          $product = \App\Models\Product::whereName($name)->firstOrFail();
          // ...
      }
      
    • Fixture Mismatches: Sylius uses sylius-fixtures; Laravel uses factories. Bridge them:
      // Convert Sylius fixture to Laravel factory
      public function thereIsAUserWithEmail($email) {
          \App\Models\User::factory()->create(['email' => $email]);
      }
      
  2. Environment Quirks

    • Headless Chrome Issues: Use --no-sandbox and --disable-dev-shm-usage flags in CI:
      capabilities:
        chromeOptions:
          args: ["--headless", "--no-sandbox", "--disable-dev-shm-usage"]
      
    • Port Conflicts: Ensure Selenium and Symfony server use distinct ports (e.g., 4444 for Selenium, 8080 for Symfony).
  3. Dependency Hell

    • Version Locking: Pin behat/behat, mink/mink, and sylius/sylius versions in composer.json to avoid conflicts:
      "require-dev": {
          "behat/behat": "3.7",
          "mink/mink": "1.8",
          "sylius/sylius": "1.12.*"
      }
      
    • Symfony vs. Laravel: The package assumes Symfony’s AppKernel. For Laravel, mock the kernel or use Laravel\BrowserKitTesting\TestCase.
  4. Flaky Tests

    • Selenium Timeouts: Increase timeouts in behat.yml:
      mink:
        selenium2:
          capabilities:
            timeouts: { script: 30000, pageLoad: 30000 }
      
    • Race Conditions: Add retries for flaky steps:
      public function iShouldSee($text, $max
      
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.
datacore/hub-sdk
alengo/sulu-http-cache-bundle
croct/coding-standard
croct/plug-php
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php
trappistes/laravel-custom-fields