Testing
The Yii application template includes a ready test setup. It uses Codeception with PHPUnit assertions and has four suites:
Unitfor isolated PHP classes.Functionalfor application code called in the same PHP process.Webfor requests sent through an HTTP server.Consolefor console commands.
The main files are:
codeception.yml.tests/bootstrap.php.tests/Unit.suite.yml.tests/Functional.suite.yml.tests/Web.suite.yml.tests/Console.suite.yml.tests/Support/*Tester.php.
Run tests locally
Build actor classes after installing dependencies or changing a suite:
shell
vendor/bin/codecept buildRun all tests:
shell
APP_ENV=test vendor/bin/codecept runThe template also defines a Composer script:
shell
APP_ENV=test composer testRun one suite:
shell
APP_ENV=test vendor/bin/codecept run Unit
APP_ENV=test vendor/bin/codecept run Functional
APP_ENV=test vendor/bin/codecept run Web
APP_ENV=test vendor/bin/codecept run ConsoleRun one test class or method:
shell
APP_ENV=test vendor/bin/codecept run Functional HomePageCest
APP_ENV=test vendor/bin/codecept run Functional HomePageCest:baseAPP_ENV=test is required for local commands because tests/bootstrap.php prepares the application environment before tests run.
Run tests in Docker
Build actor classes:
shell
make codecept buildRun all tests:
shell
make testRun one suite:
shell
make test Unit
make test Functional
make test Web
make test ConsoleThe Docker test environment reads docker/test/.env, where APP_ENV=test is already set.
Choose a suite
Start with unit tests for code that has no framework boundary: value objects, validators, domain services, and transformers.
Use functional tests when code needs Yii configuration, dependency injection, routing, middleware, request handling, or template rendering.
Use web tests for behavior that must go through an HTTP server: status codes, links, redirects, cookies, and rendered pages as seen by a client.
Use console tests for commands in src/Console.
Run static analysis in CI and before changing shared contracts.