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
Phpunit Service Create Trait

Phpunit Service Create Trait Laravel Package

pkly/phpunit-service-create-trait

Laravel/PHP trait that helps you quickly create service instances in PHPUnit tests, reducing boilerplate when setting up dependencies. Handy for service-layer unit tests where you want consistent, reusable test setup.

View on GitHub
Deep Wiki
Context7

A helper trait for PHPUnit 10+ for easier creation of services with dependencies in unit testing

Frequently asked questions about Phpunit Service Create Trait
How does this trait simplify dependency injection in Laravel service tests compared to manual mocking?
The trait automatically resolves constructor dependencies and Symfony’s `#[Required]` attributes, replacing manual mock creation. For example, instead of writing `$service = new UserService($this->createMock(UserRepository::class))`, you use `$service = $this->createService(UserService::class)`. This cuts boilerplate and keeps tests DRY when dependencies change.
Will this work with Laravel 9.x or older versions using PHPUnit 9.x?
No, this package explicitly requires PHPUnit 12+, which aligns with Laravel 10+. For older Laravel versions, you’d need to either upgrade PHPUnit or use a polyfill, though compatibility isn’t guaranteed. Check the [GitHub issues](https://github.com/pkly/phpunit-service-create-trait/issues) for workarounds.
Can I use this trait alongside Laravel’s built-in `make()` helper or Mockery?
Yes, the trait is agnostic and works independently of Laravel’s `make()` or Mockery. It defaults to PHPUnit’s `createMock()` but lets you override behavior for custom cases. Use it alongside existing tools without conflicts.
What if my service has non-standard constructors (e.g., dynamic property injection or custom `__invoke`)?
The trait handles standard constructor injection but may need manual overrides for edge cases. For dynamic properties or `__invoke`, extend the trait or pass custom dependencies via the `createService()` method’s second argument. Check the [README](https://github.com/pkly/phpunit-service-create-trait#usage) for examples.
Does this trait support partial mocks or method stubbing for services?
Yes, use `createRealPartialMockedServiceInstance()` for partial mocks. You’ll need to specify which methods to override, and the returned object will be a `MockObject`. This is useful for testing services where only specific methods require mocking.
How does this compare to Pest PHP’s `fake()` or `mock()` helpers for Laravel?
If your team uses Pest, evaluate overlap first—Pest’s `fake()` and `mock()` cover similar ground. This trait is PHPUnit-specific and may add redundancy. However, it offers finer control for complex service dependencies if Pest’s syntax feels limiting.
Will this slow down my test suite due to reflection-based service creation?
The trait uses reflection only during test setup, not runtime. For large dependency graphs, test startup time *might* increase slightly, but the tradeoff is usually worth it for reduced manual mocking. Profile your tests to confirm—most teams see negligible impact.
Can I use this for integration or E2E tests in Laravel?
No, this trait is designed for *unit* tests of service layers. For integration tests (e.g., API routes, database interactions), use Laravel’s `HttpTests`, `DatabaseTransactions`, or `RefreshDatabase` traits instead. This package avoids reinventing those tools.
How do I handle services with circular dependencies or custom DI resolvers?
Circular dependencies may require manual mocking or refactoring to break cycles. For custom DI (e.g., `App::bindConditionally()`), override the trait’s default behavior by passing resolved dependencies explicitly to `createService()`. Check the [GitHub issues](https://github.com/pkly/phpunit-service-create-trait/issues) for community solutions.
Is there a migration path if I switch from this trait to Pest or another tool later?
Yes, the trait’s methods are explicit and self-documenting. Replace `createService()` calls with Pest’s `mock()` or Laravel’s `make()` incrementally. Start by identifying trait usage in critical tests, then refactor one class at a time to minimize disruption.
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.
phalcon/cli-options-parser
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi