dayspring-tech/service-test-helper
Installation Add the package via Composer:
composer require dayspring-tech/service-test-helper
Register the service provider in config/app.php under providers:
DayspringTech\ServiceTestHelper\ServiceTestHelperServiceProvider::class,
First Use Case Test a service class with mock dependencies:
use DayspringTech\ServiceTestHelper\ServiceTestHelper;
public function testExampleService()
{
$service = new ExampleService();
$helper = new ServiceTestHelper($service);
// Mock a dependency
$mockDependency = $this->createMock(DependencyInterface::class);
$helper->mock(DependencyInterface::class, $mockDependency);
// Execute and assert
$result = $service->doSomething();
$this->assertEquals('expected', $result);
}
Where to Look First
src/ServiceTestHelper.php for core functionality.tests/ for real-world examples (if available).Mocking Dependencies Replace real dependencies with mocks for isolated testing:
$helper->mock(RepositoryInterface::class, $this->createMock(RepositoryInterface::class));
Stubbing Methods Stub specific methods on dependencies:
$mock = $this->createMock(LoggerInterface::class);
$mock->method('log')->willReturn(true);
$helper->mock(LoggerInterface::class, $mock);
Testing Service Lifecycle Verify constructor injection and method calls:
$helper->verifyConstructorInjection(DependencyInterface::class);
$helper->verifyMethodCall('doSomething', [$arg1, $arg2]);
Integration with Laravel
Use with Laravel’s built-in testing tools (e.g., Mockery or PHPUnit):
use Illuminate\Foundation\Testing\TestCase;
class ExampleServiceTest extends TestCase
{
use ServiceTestHelperTrait; // If provided
public function testService()
{
$helper = new ServiceTestHelper(new ExampleService());
// ... test logic
}
}
Testing Exception Handling Assert exceptions are thrown correctly:
$mock->method('fetchData')->willThrowException(new \RuntimeException('Error'));
$this->expectException(\RuntimeException::class);
$service->fetchData();
Static Method Limitations The package may not handle static method calls natively. Workaround:
// Manually stub static calls or refactor to instance methods.
Circular Dependencies Mocking services with circular dependencies requires manual setup:
$mockA = $this->createMock(ServiceA::class);
$mockB = $this->createMock(ServiceB::class);
$mockA->method('getB')->willReturn($mockB);
$mockB->method('getA')->willReturn($mockA);
$helper->mock(ServiceA::class, $mockA);
Laravel Service Container Conflicts Avoid mixing with Laravel’s container bindings unless explicitly supported:
// Prefer manual mocking over container binding in tests.
Magic Methods
Services using __call or __callStatic may need custom handling.
var_dump($helper->getMock(DependencyInterface::class)) to inspect mocks.verifyMethodCall() to ensure expected interactions:
$helper->verifyMethodCall('save', [$entity]);
strict_types=1 to PHP files to catch type-related issues early.Custom Matchers Extend the helper to support custom PHPUnit matchers:
$helper->extendMatcher('customMatcher', function ($actual, $expected) {
return $actual === $expected;
});
Trait Integration
If the package provides a trait (e.g., ServiceTestHelperTrait), use it in test classes:
use DayspringTech\ServiceTestHelper\ServiceTestHelperTrait;
class ExampleServiceTest extends TestCase
{
use ServiceTestHelperTrait;
}
Logging Interactions Log method calls for debugging:
$helper->enableCallLogging();
// Later, inspect logs with $helper->getCallLog().
Partial Mocking Use partial mocks for services with complex dependencies:
$partialMock = $this->getMockBuilder(ComplexService::class)
->disableOriginalConstructor()
->onlyMethods(['methodToMock'])
->getMock();
$helper->mock(ComplexService::class, $partialMock);
How can I help you explore Laravel packages today?