5分は誇張ですが…
サクッとPHPUnitをGitHub Actionsで動かす際のメモ
Step1 PHPUnitを動かす
PHPUnitのGetting Startedを参考にして
composerでPHPUnit追加
composer require --dev phpunit/phpunit ^9
composer.jsonにautoloadの設定を追加
composer.json
{
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^9"
}
}
テスト対象とテストコードを準備
Getting Startedにある、src/Email.phpとtests/EmailTest.phpをコピペすればOK
PHPUnit実行
↑まで終わったら
./vendor/bin/phpunit tests
で、PHPUnitが動くはず。
Step2 GitHubActionsで動かす
後は、
.github/workflows/に適当な名前でyamlを作成してあげて
build.yml
name: Build
on: push
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: composer install
- run: ./vendor/bin/phpunit tests
な感じに書いて、リポジトリにPush
以降はpushするたびにActionsが動いてテストされる
最終的な構成
$ tree -a -I '.git|vendor|LICENSE|README.md|.gitignore|.phpunit.result.cache'
.
├── .github
│ └── workflows
│ └── build.yml
├── composer.json
├── composer.lock
├── src
│ └── Email.php
└── tests
└── EmailTest.php