Weave 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
Service Container Extension

Service Container Extension Laravel Package

friends-of-behat/service-container-extension

Behat extension that lets you declare your own services inside the Behat container without writing an extension, by importing XML, YAML, or PHP service definition files into behat.yml.

View on GitHub
Deep Wiki
Context7

FriendsOfBehat Service Container Extension lets you register your own services in the Behat container without writing a custom extension. Just enable the extension in behat.yml, point it at one or more service definition files, and Behat imports them automatically.

  • Supports XML, YAML, and PHP service definitions
  • Multiple imports in a single configuration
  • No boilerplate extension class required
  • Ideal for injecting test doubles, repositories, or fixtures into Behat contexts
  • Drop-in integration with Symfony's DependencyInjection component
extensions:
    FriendsOfBehat\ServiceContainerExtension:
        imports:
            - "features/bootstrap/config/services.yml"
Frequently asked questions about Service Container Extension
Does friends-of-behat/service-container-extension work with Laravel?
It is not a Laravel package; it is a Behat extension built on Symfony DependencyInjection that registers services into Behat's own compiled container. It can run alongside a Laravel application to define test doubles, fixtures and environment-specific clients for the acceptance layer. Those services live in a separate, test-scoped container that shares nothing with Illuminate\Container, so treat it as a composition root for tests rather than a replacement for Laravel bindings.
How do I install and enable the extension in behat.yml?
Install it as a dev dependency with composer require friends-of-behat/service-container-extension --dev. Then enable FriendsOfBehat\ServiceContainerExtension under the extensions key in behat.yml. Finally point its imports list at one or more XML, YAML or PHP service definition files, typically stored under features/bootstrap/config/.
Which service definition formats are supported?
XML, YAML and PHP are all supported, and you can mix them in a single imports list. XML and YAML follow the standard Symfony service schema, while PHP files receive the container and call setDefinition() with a Definition instance. Definitions from every imported file are merged into Behat's container at bootstrap.
Which PHP, Symfony and Behat versions do I need?
Version 2.0.0 requires PHP ^8.3, Symfony ^7.4 or ^8.0, and Behat ^3.31 or ^4.0. Older stacks must pin to v1.x, which sits on a stale maintenance line. Check the version matrix first, because adopting v2 may force a PHP upgrade you had not otherwise scheduled.
Can services declared here access Laravel's application container, Eloquent or config?
No. Services registered through imports are built by Behat's Symfony-based container and cannot see Laravel's bootstrapped application, so app(), Eloquent connections, config() and the HTTP kernel are unavailable. Anything that needs them requires a bridge, such as a hand-written extension or an adapter that pulls the Laravel container in. That bridge is exactly the work this package is supposed to remove, so estimate it before adopting.
How do I inject a declared service into a Behat context or hook?
Give the service an id in your definition file, then have Behat resolve it by that id when it instantiates contexts and hooks. If you want constructor injection, the context class itself must be declared as a service. For contexts that Behat creates directly, setter injection on the existing instance is the simpler route.
Is this package safe to use in production?
It cannot affect runtime behaviour: it is a dev-only dependency and the extension only executes during Behat bootstrap. Your Laravel application never loads it. Production risk is effectively zero.
How do I stop stateful mocks from leaking between scenarios?
Behat treats services as shared singletons by default, so a stateful fake or mock will carry state across scenarios and produce order-dependent, intermittently failing suites. Declare anything stateful with prototype scope in the definition file. Add an explicit reset in a @BeforeScenario hook as a second line of defence.
When should I write a custom Behat extension instead of using service files?
Write an extension only when registration needs logic the container cannot express, such as computed arguments, conditional wiring, or bridging to Laravel's container. For plain service declarations, a definition file saves days of work and repeats cleanly for every service the suite needs. This package converts writing an extension into writing a service definition file, which is its entire value.
Is the package actively maintained, and what happens if it goes quiet?
Maintenance risk is real but bounded: a single active maintainer, very few dependents, and a CI badge that still points at Travis despite the v2 modernization. The code surface is small enough to vendor or fork at low cost. Treat it as a forkable convenience rather than a foundation, and name an owner for the upgrade path and fork contingency.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections