yiisoft/yii2-codeception
Yii2 integration for Codeception: run functional, acceptance, and unit tests with Yii2 bootstrapping, fixtures, and helper classes. Provides Codeception modules and configuration support to test Yii2 apps and components effectively.
Installation Add the package via Composer (though deprecated, still functional for legacy projects):
composer require --dev yiisoft/yii2-codeception
Ensure yiisoft/yii2 is installed as a base dependency.
Configuration
Update codeception.yml to include Yii2-specific modules:
actor: Tester
modules:
enabled:
- Yii2:
configFile: 'config/web.php'
entryScript: 'index.php'
First Test Case
Create a functional test in tests/acceptance/ (e.g., LoginCest.php):
<?php
use yii\codeception\Module;
class LoginCest {
public function tryToLogin(Module $I) {
$I->amOnPage('/site/login');
$I->fillField('#username', 'admin');
$I->fillField('#password', 'secret');
$I->click('#login-button');
$I->see('Welcome');
}
}
Run tests:
./vendor/bin/codecept run acceptance
Database Testing
Use Db module to seed/test data:
$I->haveInDatabase('user', ['username' => 'admin', 'auth_key' => 'test123']);
$I->seeRecord('user', ['username' => 'admin']);
Authentication Leverage Yii’s auth manager:
$I->amLoggedIn(['id' => 1, 'username' => 'admin']);
$I->see('Logout');
URL Generation
Use Yii’s Url helper:
$I->amOnPage(Yii::$app->urlManager->createUrl(['site/about']));
Event Testing Mock events in tests:
Yii::$app->on('user.login', function() {
// Assertion logic
});
Laravel-Like Patterns
Mimic Laravel’s Http tests by combining with Codeception\Module\Yii2:
$I->sendGET('/api/users', ['X-CSRF-Token' => $token]);
$I->seeResponseCodeIs(200);
Fixtures
Use Db module for bulk inserts:
$I->haveInDatabase('user', [
['username' => 'user1', 'email' => 'user1@example.com'],
['username' => 'user2', 'email' => 'user2@example.com'],
]);
Mocking Services Override Yii components in tests:
Yii::$container->set('yii\web\User', function() {
return $this->make(MockUser::class);
});
Deprecation Warnings
Ignore yiisoft/yii2-codeception deprecation notices; focus on codeception/yii2 (if available) for new projects.
Configuration Overrides
Ensure configFile in codeception.yml points to the correct Yii config (e.g., web.php for frontend, console.php for CLI tests).
Session Handling Yii2’s session may conflict with Codeception’s. Reset sessions explicitly:
$I->resetSession();
Asset Publishing If testing assets (e.g., JS/CSS), publish them first:
Yii::$app->assetManager->publish(Yii::getAlias('@app/assets'));
Enable Debug Mode
Set YII_DEBUG to true in config/web.php for verbose errors:
'components' => [
'request' => ['enableCsrfValidation' => false], // Temporarily disable for testing
],
Log Output Redirect Yii logs to Codeception’s output:
Yii::debug('Test message', 'test');
Browser Testing
Use Codeception\Module\WebDriver for Selenium integration:
modules:
enabled:
- WebDriver:
url: 'http://localhost'
browser: 'chrome'
- Yii2
Custom Modules
Extend yii\codeception\Module for project-specific logic:
class CustomYii2Module extends \yii\codeception\Module {
public function customAction() {
// Custom test logic
}
}
Hooks Use Yii events to trigger test actions:
Yii::$app->on('beforeTest', function() {
// Pre-test setup
});
Legacy Code
For pre-Yii2.0 projects, mock Yii::$app:
Yii::$app = $this->createMock(\yii\base\Application::class);
How can I help you explore Laravel packages today?