PHPUnit とは?
PHP の Unit テストフレームワーク。 コード品質を一定に保つことを目的にする
参考ドキュメント
composer
を利用して PHP プロジェクトを作成します。
$ composer init
上記コマンドを実行すると対話形式で PHP のプロジェクトが作成できます。
以下のようなプロジェクトを作成しました。
{
"name": "ouvill/phpunit-practice",
"description": "phpunit practice",
"license": "MIT",
"require": {}
}
$ composer require --dev phpunit/phpunit ^9
$ ./vendor/bin/phpunit --version
autoload を設定すると自動でクラスをロードしてくれます。
composer.json
{
"require-dev": {
"phpunit/phpunit": "^9"
},
"autoload": {
"classmap": [
"src/"
]
}
}
autoload を追加したのでマップファイルを更新します。
$ composer update
src/Email.php
<?php
declare(strict_types = 1);
final class Email
{
private $email;
private function __construct(string $email)
{
$this->ensureIsValidEmail($email);
$this->email = $email;
}
public static function fromString(string $email): self
{
return new self($email);
}
public function __toString(): string
{
return $this->email;
}
private function ensureIsValidEmail()
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException(
sprintf(
'"%s" is not a valid email address',
$email
)
);
}
}
}
tests/EmailTest.php
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class EmailTest extends TestCase
{
public function testCanBeCreatedFromValidEmailAddress(): void
{
$this->assertInstanceOf(
Email::class,
Email::fromString('[email protected]')
);
}
public function testCannotBeCreatedFromInvalidEmailAddress(): void
{
$this->expectException(InvalidArgumentException::class);
Email::fromString('invalid');
}
public function testCanBeUsedAsString(): void
{
$this->assertEquals(
'[email protected]',
Email::fromString('[email protected]')
);
}
}
コードとテストが記述できたので、テストを実行します。
$ ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest.php
結果
PHPUnit 9.0.0 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 11 ms, Memory: 4.00 MB
OK (3 tests, 3 assertions)
無事テストが通ったら完了です。
通ったテスト一覧を表示することもできます。
./vendor/bin/phpunit --bootstrap vendor/autoload.php --testdox tests
結果
PHPUnit 9.0.0 by Sebastian Bergmann and contributors.
Email
✔ Can be created from valid email address
✔ Cannot be created from invalid email address
✔ Can be used as string
Time: 12 ms, Memory: 4.00 MB
OK (3 tests, 3 assertions)
テスト実行コマンドが長いので、composer scripts を記載します。
参考: Scripts
composer.json
{
"require-dev": {
"phpunit/phpunit": "^9"
},
"autoload": {
"classmap": [
"src/"
]
},
"scripts": {
"test": [
"phpunit --bootstrap vendor/autoload.php --testdox tests"
]
}
}
以下のコマンドでテストが実行できるようになります。
$ composer run-script test
Git でコードを管理していると思います。
GitHub に Push したとき、自動でテストしてくれるように設定しましょう。
最近追加された GitHub Actions を利用します。
GitHub のリポジトリから Actions を開きます。
PHP のSet up this workflow
をクリック。
以下のように - name: Run test suite
のコメントを外します。
.github/workflows/php.yml
name: PHP Composer
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Validate composer.json and composer.lock
run: composer validate
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md
- name: Run test suite
run: composer run-script test
コードが編集できたら、start commit
-> Commit new file
でコードを追加します。
以上で GitHub にコードが’ Push されるたびにテストが実行されます。
しっかりとテストを書いて、安心してコード変更できるようになりましょう。