PHPUnit  メモ

# 目的
テスト実行の効率化

# インストール手順
1). PHP Archive (PHAR)

```vim
wget -O phpunit https://phar.phpunit.de/phpunit-7.phar

chmod +x phpunit

./phpunit --version
```
2). Composer

```vim
composer require --dev phpunit/phpunit ^7

./vendor/bin/phpunit --version
```

# 対象クラスとテストクラス
### 対象クラス

> src/Email.php

```vim
<?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(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException(
                sprintf(
                    '"%s" is not a valid email address',
                    $email
                )
            );
        }
    }
}
```
### テストクラス

> tests/EmailTest.php

```vim
<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class EmailTest extends TestCase
{
    public function testCanBeCreatedFromValidEmailAddress(): void
    {
        $this->assertInstanceOf(
            Email::class,
            Email::fromString('user@example.com')
        );
    }

    public function testCannotBeCreatedFromInvalidEmailAddress(): void
    {
        $this->expectException(InvalidArgumentException::class);

        Email::fromString('invalid');
    }

    public function testCanBeUsedAsString(): void
    {
        $this->assertEquals(
            'user@example.com',
            Email::fromString('user@example.com')
        );
    }
}
```

# テスト実行手順
1). PHP Archive (PHAR)

```vim
./phpunit --bootstrap src/autoload.php tests/EmailTest
PHPUnit 7.0.0 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 70 ms, Memory: 10.00MB

OK (3 tests, 3 assertions)
```
2). Composer

```vim
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest
PHPUnit 7.0.0 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 70 ms, Memory: 10.00MB

OK (3 tests, 3 assertions)
```

# テストで検証された動作を文書化
1). PHP Archive (PHAR)

```vim
./phpunit --bootstrap src/autoload.php --testdox tests
PHPUnit 7.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
```
2). Composer

```vim
./vendor/bin/phpunit --bootstrap vendor/autoload.php --testdox tests
PHPUnit 7.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
```

# 参考
[https://docs.zendframework.com/zend-test/phpunit/](https://docs.zendframework.com/zend-test/phpunit/)

[https://phpunit.de/getting-started/phpunit-7.html](https://phpunit.de/getting-started/phpunit-7.html)

[https://phpunit.readthedocs.io/ja/latest/](https://phpunit.readthedocs.io/ja/latest/)