codeception/module-symfony
Symfony module for Codeception that integrates the framework’s kernel, container, and HTTP client for functional/acceptance testing. Boot the app, make requests, assert responses, and access services to test controllers and app behavior with minimal setup.
composer require --dev codeception/module-symfony
codeception.yml):
modules:
enabled:
- Symfony:
environment: 'test'
kernel_class: 'App\\Kernel'
AcceptanceTestCest:
public function testHomepageRoute(AcceptanceTester $I)
{
$I->seePageIsAvailable('/');
}
seeResponseIsSuccessful, assertEmailCount).tests/bootstrap.php loads .env.testing if needed.// Verify a route exists and returns success
$I->seePageIsAvailable('/dashboard');
$I->seeResponseIsSuccessful();
// Check response headers/cookies
$I->assertResponseHasHeader('Content-Type', 'application/json');
$I->assertResponseCookieValueSame('session_id', 'abc123');
// Submit a form and validate
$I->submitSymfonyForm('#login-form', ['_username' => 'admin', '_password' => 'secret']);
$I->seeCurrentUrlEquals('/dashboard');
$I->seeSessionHasValues(['flashbag' => ['success' => 'Logged in!']]);
// Check form errors
$I->seeFormErrorMessages('#login-form', ['email' => 'This field is required.']);
// Configure Mailpit (local SMTP server)
$I->haveSymfonyMailerConfigured([
'dsn' => 'mailpit://localhost:1025',
]);
// Send an email and assert
$I->sendEmailTo('user@example.com', 'Welcome', 'Hello!');
$I->assertEmailCount(1);
$I->assertEmailHeaderSame('From', 'noreply@example.com');
// Grab a service (e.g., Doctrine EntityManager)
$em = $I->grabService('doctrine.orm.entity_manager');
$user = $em->find(User::class, 1);
$I->assertNotNull($user);
// Test a private service (if allowed)
$I->seeServiceIsCalled('app.some_service', 'someMethod');
// Verify an event was dispatched
$I->seeEventTriggered('kernel.request');
$I->dontSeeEventTriggered('kernel.exception');
// Check event data
$I->seeOrphanEvent('app.user.created', ['userId' => 1]);
// Run a console command and assert output
$I->runSymfonyConsoleCommand('app:user:create', ['--name' => 'John']);
$I->seeCommandOutputContains('User John created!');
// Test translations
$I->seeTranslation('auth.login', 'Login');
$I->dontSeeMissingTranslations();
// Validate data
$I->assertValidatorErrors(['email' => 'invalid@example'], [
'email' => 'This value is not a valid email address.'
]);
Environment-Specific Config:
Use codeception.yml to switch environments:
modules:
config:
Symfony:
environment: '%env(APP_ENV)%'
Pass environment variables via .env.testing.
Reset State Between Tests:
Use resetKernel() to clear services/cache:
$I->resetKernel();
Debugging: Enable Symfony debug mode in tests:
$I->haveSymfonyDebugModeEnabled();
Custom Assertions: Extend the module by creating a custom trait:
use Codeception\Module\Symfony;
trait CustomSymfonyAssertions
{
public function seeCustomAssertion()
{
// Your logic
}
}
Kernel Booting:
bootKernelOnce() in _before():
public function _before(AcceptanceTester $I)
{
$I->bootKernelOnce();
}
Service Access:
grabService() with caution:
$I->grabService('app.private_service'); // May fail if not public.
seeServiceIsCalled() to verify interactions without direct access.Email Testing:
config/packages/test/mailer.yaml:
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
docker run -p 1025:1025 -p 8025:8025 axicmailer/mailpit
Event Listeners:
seeEventTriggered() to verify:
$I->seeEventTriggered('app.user.registered');
$I->resetKernel(); // Clears listeners too
Doctrine & Database:
resetDatabase():
$I->resetDatabase(); // Requires `doctrine` module.
Symfony 6+ Changes:
HttpClient instead of BrowserKit for some assertions. Update tests to use:
$I->assertHttpClientRequest('GET', '/api/users');
Performance:
_before() for multi-test suites:
static $kernel;
public function _before(AcceptanceTester $I)
{
if (!$I->hasKernel()) {
$I->bootKernel();
}
}
Enable Debug Mode:
$I->haveSymfonyDebugModeEnabled();
Check the Symfony debug toolbar for errors.
Log Output:
Use seeLogMessage() to debug:
$I->seeLogMessage('DEBUG', 'User loaded');
Dump Services: List all services to debug:
$services = $I->grabService('service_container')->getServiceIds();
print_r($services);
Assertion Failures:
debug() to inspect variables:
$I->debug($I->grabLastSentEmail());
Custom Assertions: Create a trait to extend functionality:
use Codeception\Module;
trait CustomAssertions
{
public function seeCustomResponse($expected)
{
$response = $this->getModule('Symfony')->getResponse();
$this->assertEquals($expected, $response->getContent());
}
}
Override Module Config:
Extend the module in codeception.yml:
modules:
config:
Symfony:
cache_router: true
cache_pool: true
Hook into Lifecycle:
Use _before/_after in tests or a custom helper:
public function _before(AcceptanceTester $I)
{
$I->haveSymfonyEnvironment('test');
$I->haveSymfonyDebugModeDisabled();
}
Mock Services: Replace services in tests:
$I->haveService('app.mailer', function () {
How can I help you explore Laravel packages today?