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 Mocking

Service Mocking Laravel Package

happyr/service-mocking

HappyR Service Mocking is a Laravel testing helper that makes it easy to replace real services with mocks in the container. Swap bindings during functional and integration tests to isolate dependencies and control behavior without manual container wiring or boilerplate.

View on GitHub
Deep Wiki
Context7

Easy mock Symfony services for testing

Frequently asked questions about Service Mocking
Can I use happyr/service-mocking in a Laravel application?
It is not a drop-in Laravel package. happyr/service-mocking is a Symfony bundle plus a ServiceMockTrait aimed at Symfony's KernelTestCase, and Laravel does not auto-register the bundle or use a compiled DI container. A small wrapper around app()->instance() and bind() is feasible, but at that point you are reimplementing the concept rather than integrating the package.
How do I install happyr/service-mocking for Laravel tests?
You can install it with composer require --dev happyr/service-mocking, but there is no documented Laravel installation path. The package expects symfony/dependency-injection and symfony/framework-bundle, and you would need to manually register the bundle or port the trait. For most Laravel apps, the native test helpers are the safer setup.
Does happyr/service-mocking support Laravel 10 and Laravel 11?
The package does not document Laravel version support; it tracks Symfony 5 through 7. Laravel 11 ships Symfony 7 components and Laravel 10 ships Symfony 6.4, so you must keep the package's Symfony constraints aligned with your Laravel version. This version lock-step is the main compatibility risk.
How is happyr/service-mocking different from Laravel's $this->mock() and $this->instance()?
Laravel's $this->mock(), $this->partialMock(), $this->spy(), and $this->instance() already cover most container-service mocking in feature and integration tests. happyr/service-mocking solves the Symfony compiled-container problem, where services are fixed at compile time. Laravel binds at runtime, so the package is closer to a pattern reference than a needed dependency.
What does the ServiceMockTrait do and can I use it with Laravel's TestCase?
The ServiceMockTrait swaps real service definitions inside a compiled Symfony DI container for PHPUnit or Mockery doubles, targeting KernelTestCase. Laravel's TestCase is not a Symfony KernelTestCase, so the trait does not apply directly. In Laravel you would rebind services with app()->instance() or bind() in setUp() and restore them with app()->forgetInstance() in tearDown().
Is happyr/service-mocking actively maintained and safe to depend on?
Maintenance is single-vendor, from a Swedish consultancy, and the project has about 45 stars and zero dependents. The 2024-10-08 release is a positive signal that it is not abandoned, but the bus factor is low. Check your organization's OSS policy before adding a small, single-vendor dev dependency.
What are the best alternatives to happyr/service-mocking in Laravel?
The best Laravel alternatives are the framework's own $this->mock(), $this->partialMock(), $this->spy(), and $this->instance() helpers, plus container bind() overrides in a shared test case. If you need compile-time-style mocking, build a small internal MockableServiceRegistry that records bindings before bootstrap() and applies them in register() or boot(). That covers roughly 90 percent of the described capability with no new dependency.
Can happyr/service-mocking mock services constructed inside Laravel service providers?
Laravel's native $this->mock() and $this->instance() handle services resolved from the container, including many provider-bound services, when you rebind before the code under test runs. The package's strength is mocking at container-compile time, which Laravel does not have. If your pain is a service constructed deep inside a provider, prefer a Laravel-idiomatic binding override rather than the Symfony bundle.
What are the production and CI risks of adding happyr/service-mocking?
The package is a require-dev dependency, so production runtime risk is limited and the main exposure is CI. The dominant risks are Symfony version lock-step with Laravel and sharp edges in container manipulation, such as private services, aliases, decorated services, final classes, and readonly promoted properties. Laravel-only engineers may lack intuition for those compiled-container behaviors.
What are the typical use cases for happyr/service-mocking?
Typical use cases are isolating dependencies in functional and integration tests, swapping container-bound services for mocks, and reducing repetitive service-container setup in Symfony projects. In Laravel, the same goals are usually met with $this->mock(), $this->instance(), and bind() overrides. If any part of your estate is genuinely Symfony, that is the legitimate home for this package.
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.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect