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.
Mockery, createMock()) without overlapping with feature/integration tests (where Laravel’s HttpTests or DatabaseTests are preferred).composer require --dev, with no global configuration required.fake()/mock() are already sufficient.AppServiceProvider::bind()).fake() is more idiomatic.disableOriginalConstructor() overrides.Static::method()).fake()/mock() (this trait may be redundant).createMock().// Before (verbose)
$mockRepo = $this->createMock(UserRepository::class);
$mockLogger = $this->createMock(Logger::class);
$service = new UserService($mockRepo, $mockLogger);
// After (concise)
$service = $this->createService(UserService::class);
createRealPartialMockedServiceInstance for services with selective method overrides.HttpTests already provide better helpers (e.g., actingAs(), json()).DatabaseTests or DatabaseMigrations traits instead.fake()/mock() may suffice (evaluate redundancy).UserService, OrderProcessor).composer require --dev pkly/phpunit-service-create-trait
use PKLY\PHPUnit\ServiceCreateTrait;
class UserServiceTest extends TestCase {
use ServiceCreateTrait;
}
createService(), getMockedService()) in a internal wiki.fake() instead).fake()/mock()—assess redundancy.composer dump-autoload).Tests\TestCase).createService().getMockedService() for dependencies.var_dump() on ReflectionClass to inspect constructors.phpunit.xml to catch issues early.createMock() calls).Tests/TestCase extends a base class with the trait).getMockedService() to explicitly define mock behaviors.How can I help you explore Laravel packages today?