Installation:
composer require chaplean/unit-bundle
Ensure LiipFunctionalTestBundle and DoctrineFixturesBundle are also installed (required for testing infrastructure).
Register Bundles in config/bundles.php (or AppKernel.php for Symfony <5.0):
return [
// ...
Chaplean\Bundle\UnitBundle\ChapleanUnitBundle::class => ['all' => true],
Liip\FunctionalTestBundle\LiipFunctionalTestBundle::class => ['test' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['test' => true],
];
Import Config:
Add to config/packages/test/chaplean_unit.yaml (Symfony 5+) or config_test.yml (Symfony <5.0):
imports:
- { resource: '@ChapleanUnitBundle/Resources/config/config.yml' }
First Use Case:
Create a test class extending Chaplean\Bundle\UnitBundle\Test\WebTestCase:
use Chaplean\Bundle\UnitBundle\Test\WebTestCase;
class MyControllerTest extends WebTestCase
{
public function testExample()
{
$client = static::createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Run with:
php bin/phpunit
Service Mocking:
MockedServiceOnSetUpInterface to define mocks globally (e.g., for external APIs, PDF generators).class MockService implements MockedServiceOnSetUpInterface
{
public static function getMockedServices(): array
{
return [
'guzzle.client' => \Mockery::mock(Client::class)
->shouldReceive('request')
->andReturn(new Response()),
];
}
}
config_test.yml:
chaplean_unit:
mocked_service: App\Test\MockService
Test Isolation:
setUp() to reset mocks or fixtures:
protected function setUp(): void
{
parent::setUp();
$this->resetMocks(); // Clears all mocks from previous tests
}
Database Fixtures:
DoctrineFixturesBundle in tests:
public function testWithFixtures()
{
$this->loadFixtures(['App\DataFixtures\ORM\LoadUserData']);
// Test logic...
}
Kernel Bootstrapping:
createKernel() to customize the test environment:
protected function createKernel(): KernelInterface
{
return new Kernel('test', true);
}
static::getContainer() to access services in tests:
$service = static::getContainer()->get('my_service');
Assert:
$this->assertTrue($service->isValid());
dataProvider).Mock Lifecycle:
MockedServiceOnSetUpInterface are reset per test method by default. Use resetMocks() explicitly if needed.getMockedServices()—mock behavior should be deterministic.Configuration Overrides:
config_test.yml) takes precedence over config/packages/ but may conflict with parameters.yml. Use environment variables for sensitive data:
# config_test.yml
parameters:
app.secret: '%env(APP_SECRET)%'
Doctrine Fixtures:
loadFixtures() manually or use a trait:
use Doctrine\Bundle\FixturesBundle\Test\FixturesTrait;
class MyTest extends WebTestCase
{
use FixturesTrait;
}
Symfony 5+ Compatibility:
config/packages/test/ is used instead of config_test.yml for Symfony 5+. The bundle may not auto-detect the new structure.shouldHaveReceived() to verify interactions:
$mock->shouldHaveReceived('method')->once();
dump(static::getContainer()->getServiceIds());
phpunit.xml:
<php>
<server name="KERNEL_ENV" value="test"/>
<server name="APP_DEBUG" value="1"/>
</php>
WebTestCase to add shared setup:
abstract class BaseTestCase extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->initCustomMocks();
}
}
getMockedServices() per test class for context-specific mocks.kernel.test events for pre/post-test hooks:
$dispatcher->addListener(KernelEvents::REQUEST, function (RequestEvent $event) {
// Modify request before test
});
How can I help you explore Laravel packages today?