redhotmagma/symfony-test-utils
Utilities to simplify testing in Symfony projects. Provides helpers and small tools for common test setup tasks, assertions, and fixtures to reduce boilerplate and speed up writing reliable unit and integration tests.
Installation Add the package to your Laravel project via Composer:
composer require redhotmagma/symfony-test-utils --dev
Since Laravel uses Symfony components under the hood, this package integrates seamlessly.
First Use Case: Mocking Symfony Components
Use the SymfonyTestUtils facade to mock Symfony services (e.g., ContainerInterface, HttpClientInterface) in tests:
use RedHotMagma\SymfonyTestUtils\Facades\SymfonyTestUtils;
$mockContainer = SymfonyTestUtils::mockContainer([
'some.service' => $this->createMock(SomeService::class),
]);
Where to Look First
RedHotMagma\SymfonyTestUtils\Facades\SymfonyTestUtilsmockContainer(), mockHttpClient(), and createTestRequest() for common needs.Mocking the Symfony Container
Replace Laravel’s Container with a mock for isolated testing:
$mockContainer = SymfonyTestUtils::mockContainer([
'mailer' => $this->createMock(MailerInterface::class),
]);
$this->app->instance(ContainerInterface::class, $mockContainer);
Testing HTTP Clients Simulate API calls without hitting external services:
$mockClient = SymfonyTestUtils::mockHttpClient([
'GET /api/users' => ['status' => 200, 'body' => '{"id":1}'],
]);
$this->app->instance(HttpClientInterface::class, $mockClient);
Creating Test Requests Generate Symfony-style requests for controller testing:
$request = SymfonyTestUtils::createTestRequest('POST', '/login', [
'email' => '[email protected]',
]);
$response = $this->app->handle($request);
Integration with Laravel’s Testing
Combine with Laravel’s Http and Database testing tools:
use Illuminate\Foundation\Testing\TestCase;
class UserTest extends TestCase {
protected function setUp(): void {
parent::setUp();
$this->mockContainer(['auth' => $this->createMock(Auth::class)]);
}
}
HttpClient but use real Database).Laravel-Specific Quirks
Illuminate\Auth\AuthManager) may need manual mocking.Auth via Laravel’s createMock(AuthManager::class) instead of Symfony’s UserProviderInterface.Container Binding Conflicts
instance() sparingly or wrap in refresh():
$this->app->refresh();
$this->app->instance(ContainerInterface::class, $mockContainer);
HttpClient Mocking Limitations
*) for dynamic paths:
$mockClient = SymfonyTestUtils::mockHttpClient([
'GET /api/*' => ['status' => 200],
]);
assertInstanceOf() to confirm mocks are injected:
$this->assertInstanceOf(MockObject::class, $this->app->make('some.service'));
$this->getMockBuilder(SomeService::class)
->disableOriginalConstructor()
->setMethods(['someMethod'])
->getMock()
->expects($this->once())
->method('someMethod')
->will($this->returnValue(true));
HttpClient methods that differ from Laravel’s Http facade.use RedHotMagma\SymfonyTestUtils\MockFactory;
class CustomMockFactory extends MockFactory {
public function mockCustomService(array $methods = []) {
return $this->createMock(CustomService::class, $methods);
}
}
Middleware testing:
$request = SymfonyTestUtils::createTestRequest('GET', '/');
$response = $this->app->handle($request);
$this->assertEquals(200, $response->getStatusCode());
EventDispatcherInterface for testing event listeners:
$mockDispatcher = SymfonyTestUtils::mockEventDispatcher();
$this->app->instance(EventDispatcherInterface::class, $mockDispatcher);
How can I help you explore Laravel packages today?