codeception/module-yii2
Codeception Yii2 module for acceptance and functional testing. Provides helpers to bootstrap Yii2 apps, handle fixtures, navigate routes, interact with models and components, and integrate Yii2-specific assertions into your Codeception test suite.
Installation
Add the module via Composer in your Laravel project (if using Yii2 integration via yii2-laravel or similar):
composer require --dev codeception/module-yii2
For pure Laravel projects, ensure you’re using a bridge (e.g., yii2-laravel) or mock Yii2 components directly.
Configure codeception.yml
Add the module to your modules section under the appropriate suite (e.g., acceptance):
modules:
enabled:
- Yii2:
part: [app, tests] # Paths to Yii2 app and test files
entryScript: index-test.php # Yii2 entry script (if applicable)
First Use Case: Testing Controllers Write a basic acceptance test to verify a Yii2-style controller action:
<?php
use Codeception\Test\Acceptance;
use Codeception\Module\Yii2;
class SiteCept extends Acceptance
{
public function _before()
{
$I = $this;
$I->amOnPage('/site/index'); // Yii2 route
}
public function testHomepageLoads()
{
$I = $this;
$I->see('Welcome to Yii2'); // Assert content
}
}
Run with:
./vendor/bin/codecept run acceptance
Dependency Injection
Use the module to inject Yii2 services (e.g., Yii::$app) into tests:
$I->haveInDatabase('users', ['id' => 1, 'name' => 'Test User']);
$I->seeRecord('users', ['name' => 'Test User']);
Database Testing
Leverage Yii2’s Db module for migrations/seeding:
$I->runYiiCommand('migrate/up'); // Run Yii2 migrations
$I->loadFixtures('users.yml'); // Load test fixtures
Authentication Simulate user logins with Yii2’s auth system:
$I->amLoggedInAs('admin@example.com', 'password123');
$I->see('Admin Dashboard');
Integration with Laravel
If using yii2-laravel, bridge Laravel’s Auth facade to Yii2’s User model:
// In a Laravel service provider:
Yii::$container->set('auth', function () {
return new \yii\web\User([
'identityClass' => \app\models\User::class,
]);
});
Mocking Yii2 Components Use PHPUnit’s mocking to replace Yii2 dependencies in unit tests:
$mockAuth = $this->getMockBuilder(Yii::$app->authManager)
->disableOriginalConstructor()
->getMock();
Yii::$app->set('authManager', $mockAuth);
Custom Commands Extend the module to support custom Yii2 commands:
# codeception.yml
modules:
config:
Yii2:
commands:
- name: custom-command
script: yii custom/command
Event Testing Listen for Yii2 events in tests:
Yii::$app->on('user.login', function () {
$this->see('Login event triggered');
});
Path Configuration
part: [app, tests] paths must be relative to codeception.yml.part: [/var/www/project/app, /var/www/project/tests]
Yii2 App Initialization
entryScript (e.g., index-test.php) isn’t configured.// index-test.php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/test.php';
new yii\web\Application($config);
Laravel-Yii2 Conflicts
Yii::$container->set('app', function () {
return new yii\web\Application(\Yii::createObject(\app\config\test.php));
});
Database Transactions
Db module may not rollback transactions in Codeception.codeception.yml:
modules:
config:
Db:
dsn: 'mysql:host=localhost;dbname=test_db'
cleanup: true
populate: true
Enable Debug Mode Set Yii2’s debug mode in tests:
Yii::$app->set('debug', true);
Yii::$app->set('logTarget', ['class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning']]);
Log Output Redirect Yii2 logs to Codeception’s output:
$I->debug('Yii2 Logs:', Yii::$app->getLogger()->getLogs());
Isolated Testing
Use Yii::$app->set('request', $this->makeMockRequest()) to mock HTTP requests in unit tests.
Custom Assertions Extend the module to add Yii2-specific assertions:
// In a custom module
public function seeYiiFlashMessage($message)
{
$this->see($message, 'div.alert');
}
Hooks Override Yii2’s lifecycle hooks in tests:
Yii::$app->on('app.init', function () {
// Modify app config before tests run
});
Parallel Testing
Configure multiple Yii2 environments in codeception.yml for parallel suites:
suites:
acceptance:
modules:
config:
Yii2:
entryScript: index-test.php
config: config/test.php
api:
modules:
config:
Yii2:
entryScript: index-api.php
config: config/api.php
How can I help you explore Laravel packages today?