# Testing and Quality Skill ## Purpose Use this skill for tests, static analysis, quality gates, Composer scripts, verification steps, and code style checks. --- ## Testing Standard Automated tests are expected for new behavior. Preferred tools: - The project’s existing `tests/run.php` runner - PHPUnit, if the project adds or already uses it - Pest, if the project already uses it Rules: - Add or update tests with every meaningful behavior change. - Cover success paths and failure paths. - Unit-test business logic. - Integration-test database and framework wiring where useful. - Functional-test important user flows. - Avoid relying on `var_dump()` or manual browser testing as the only verification. --- ## Basic Test Command Run basic tests: ```bash php tests/run.php ``` If PHPUnit is installed: ```bash vendor/bin/phpunit ``` Example PHPUnit test: ```php final class InvoiceCalculatorTest extends TestCase { public function testItCalculatesTotalInCents(): void { $calculator = new InvoiceCalculator(); $total = $calculator->calculateTotal([ ['amountCents' => 1000], ['amountCents' => 2500], ]); self::assertSame(3500, $total); } } ``` --- ## Static Analysis and Quality Gates Use static analysis when available. Recommended tools: ```bash composer require --dev phpstan/phpstan composer require --dev vimeo/psalm ``` Common quality commands: ```bash composer validate composer audit vendor/bin/phpcs --standard=PSR12 src tests vendor/bin/phpunit vendor/bin/phpstan analyse src tests ``` Do not ignore tool failures without documenting why. --- ## Recommended Style Tools ```bash composer require --dev squizlabs/php_codesniffer composer require --dev friendsofphp/php-cs-fixer ``` Example checks: ```bash vendor/bin/phpcs --standard=PSR12 src tests vendor/bin/php-cs-fixer fix --dry-run --diff ``` Example fix: ```bash vendor/bin/php-cs-fixer fix ``` --- ## Recommended Composer Scripts A project may include scripts like this: ```json { "scripts": { "test": "phpunit", "style": "phpcs --standard=PSR12 src tests", "style:fix": "php-cs-fixer fix", "analyse": "phpstan analyse src tests", "quality": [ "@style", "@test", "@analyse" ] } } ``` Then run: ```bash composer quality ``` --- ## Verification Rules When completing work: - Run relevant checks when possible. - Explain any checks that could not be run. - If tests are not added, explain why. - If a test fails, do not hide the failure. - Do not change unrelated tests just to make failures disappear. --- ## Quality Checklist Before considering work complete: - [ ] New behavior is tested. - [ ] Existing tests pass, or failures are explained. - [ ] Code follows PSR-12 or project-specific style. - [ ] Composer files are valid. - [ ] No unrelated dependency updates were introduced. - [ ] Static analysis passes if configured. - [ ] Manual verification steps are documented when automated testing is not practical.