どうもきよです!
今回はPHPUnitのAssertionの使い方詳細をメモしていきます。
PHPUnitの初期設定方法についての記事は下記にありますので、
そちらご覧ください。
・参考
・バージョン
Laravel 6.5.2
PHPUnit 9.5.8
・Assertionの詳細と例
1. assertArrayHasKey
・チェック内容
配列にキーが存在している
・引数
@param int|string $key @param array|ArrayAccess $array @param string $message = ''
※$messageは失敗時に表示されるメッセージ
・使用方法
public function testExample() { $hoges = ['test' => 1]; $this->assertArrayHasKey('test', $hoges, '失敗時に表示されるよ'); }
2. assertArrayNotHasKey
・チェック内容
配列にキーが存在していない
・引数
@param int|string $key @param array|ArrayAccess $array @param string $message = ''
※$messageは失敗時に表示されるメッセージ
・使用方法
public function testExample()
{
$hoges = ['test' => 1];
$this->assertArrayNotHasKey('hoge', $hoges, '失敗時に表示されるよ');
}
3. assertClassHasAttribute
・チェック内容
指定のクラスに属性(プロパティ、メンバ変数)が存在している
・引数
@param string $attributeName @param string $className @param string $message
※$messageは失敗時に表示されるメッセージ
・使用方法
class HogeHoge { public $hoge; }
public function testExample() { $this->assertClassHasAttribute('hoge', HogeHoge::class, '失敗時に表示されるよ'); }
4. assertClassNotHasAttribute
・チェック内容
指定のクラスに属性(プロパティ、メンバ変数)が存在していない
・引数
@param string $attributeName @param string $className @param string $message
※$messageは失敗時に表示されるメッセージ
・使用方法
class HogeHoge { public $hoge; }
public function testExample() { $this->assertClassHasAttribute('hogehoge', HogeHoge::class, '失敗時に表示されるよ'); }
※stdClassだと多分チェックできない
5. assertClassHasStaticAttribute
・チェック内容
指定のクラスにstatic(静的)な属性(プロパティ、メンバ変数)が存在している
・引数
@param string $attributeName @param string $className @param string $message
※$messageは失敗時に表示されるメッセージ
・使用方法
class HogeHoge { static public $hoge; }
public function testExample() { $this->assertClassHasStaticAttribute('hoge', HogeHoge::class, '失敗時に表示されるよ'); }
※stdClassだと多分チェックできない
6. assertClassNotHasStaticAttribute
・チェック内容
指定のクラスにstatic(静的)な属性(プロパティ、メンバ変数)が存在していない
・引数
@param string $attributeName @param string $className @param string $message
※$messageは失敗時に表示されるメッセージ
・使用方法
class HogeHoge { public $hoge; }
public function testExample() { $this->assertClassNotHasStaticAttribute('hoge', HogeHoge::class, '失敗時に表示されるよ'); }
※stdClassだと多分チェックできない
7. assertContains
・チェック内容
配列に指定の値が存在している
・引数
@param mixed $needle @param array or Traversable $haystack @param string $message = ''
※第2引数の$haystackはPHPUnit 9.0以上からstringはサポートされなくなった。代わりにassertStringContainsStringまたはassertStringContainsStringIgnoringCaseを使用する。
※第4引数の$ignoreCaseはPHPUnit 9.0以上から削除されている。
※第5引数の$checkForObjectIdentityはPHPUnit 9.0以上から削除されている。代わりにassertContainsEqualsを使用する
※第6引数の$checkForNonObjectIdentityはPHPUnit 9.0以上から削除されている。
・使用方法
public function testExample() { $hoges = [1 => 'hoge']; $this->assertContains('hoge', $hoges, '失敗時に表示されるよ'); }
8. assertNotContains
・チェック内容
配列に指定の値が存在していない
・引数
@param mixed $needle @param array or Traversable $haystack @param string $message = ''
※第2引数の$haystackはPHPUnit 9.0以上からstringはサポートされなくなった。代わりにassertStringContainsStringまたはassertStringContainsStringIgnoringCaseを使用する。
※第4引数の$ignoreCaseはPHPUnit 9.0以上から削除されている。
※第5引数の$checkForObjectIdentityはPHPUnit 9.0以上から削除されている。代わりにassertContainsEqualsを使用する
※第6引数の$checkForNonObjectIdentityはPHPUnit 9.0以上から削除されている。
・使用方法
public function testExample() { $hoges = [1 => 'hoge']; $this->assertNotContains('test', $hoges, '失敗時に表示されるよ'); }
9. assertStringContainsString
・チェック内容
文字列が同じである
・引数
@param string $needle @param string $haystack @param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertStringContainsString('hoge', $hoge, '失敗時に表示されるよ'); }
10. assertStringNotContainsString
・チェック内容
文字列が同じではない
・引数
@param string $needle @param string $haystack @param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertStringNotContainsString('test', $hoge, '失敗時に表示されるよ'); }
11. assertStringContainsStringIgnoringCase
・チェック内容
検索対象の文字列内に、指定の文字が存在する
・引数
@param string $needle @param string $haystack @param string $message = ''
・使用方法
public function testExample() { $hoge = 'aaaahogebbbbb'; $this->assertStringContainsStringIgnoringCase('hoge', $hoge, '失敗時に表示されるよ'); }
12. assertStringNotContainsStringIgnoringCase
・チェック内容
検索対象の文字列内に、指定の文字が存在しない
・引数
@param string $needle @param string $haystack @param string $message = ''
・使用方法
public function testExample()
{
$hoge = 'aaaahogebbbbb';
$this->assertStringNotContainsStringIgnoringCaset('test', $hoge, '失敗時に表示されるよ');
}
13. assertContainsOnly
・チェック内容
配列内の値が全て指定の型になっている
・引数
@param string $type @param array or Traversable $haystack @param ?bool $isNativeType (nullable) @param string $message = ''
※第3引数の$isNativeType = true の場合は、第1引数は下記のどれかでないとだめ
'numeric' 'integer' 'int' 'iterable' 'float' 'string' 'boolean' 'bool' 'null' 'array' 'object' 'resource' 'scalar'
※第3引数の$isNativeType = false の場合は、第1引数はクラス名
※第3引数の$isNativeType =null の場合は、true or false のどっちでもOK
・使用方法
class HogeHoge { static public $hoge; private $hoge2; }
public function testExample() { $hoges = [new HogeHoge, new HogeHoge, new HogeHoge]; $this->assertContainsOnly(HogeHoge::class, $hoges, null, '失敗時に表示されるよ'); }
14. assertNotContainsOnly
・チェック内容
配列内の値が全て指定の型ではない
・引数
@param string $type @param array or Traversable $haystack @param ?bool $isNativeType (nullable) @param string $message = ''
※第3引数の$isNativeType = true の場合は、第1引数は下記のどれかでないとだめ
'numeric' 'integer' 'int' 'iterable' 'float' 'string' 'boolean' 'bool' 'null' 'array' 'object' 'resource' 'scalar'
※第3引数の$isNativeType = false の場合は、第1引数はクラス名
※第3引数の$isNativeType =null の場合は、true or false のどっちでもOK
・使用方法
public function testExample() { $hoges = ['hoge', 'huga', 'hogehoge']; $this->assertContainsOnly('int', $hoges, null, '失敗時に表示されるよ'); }
15. assertContainsOnlyInstancesOf
・チェック内容
配列内の値が全て指定のクラスである
・引数
@param string $type @param array or Traversable $haystack @param string $message = ''
・使用方法
class HogeHoge { public $hoge; private $hoge2; }
public function testExample() { $hoges = [new HogeHoge, new HogeHoge]; $this->assertContainsOnlyInstancesOf(HogeHoge::class, $hoges, '失敗時に表示されるよ'); }
16. assertCount
・チェック内容
配列の要素数が指定の数値と同じであること
Countableの場合は、countの結果が指定の数値と同じであること
・引数
@param int $expectedCount @param Countable or array or Traversable $haystack @param string $message = ''
・使用方法
public function testExample() { $hoges = ['hoge', 'hoge2']; $this->assertCount(2, $hoges, '失敗時に表示されるよ'); }
17. assertNotCount
・チェック内容
配列の要素数が指定の数値以外であること
Countableの場合は、countの結果が指定の数値以外であること
・引数
@param int $expectedCount @param Countable or array or Traversable $haystack @param string $message = ''
・使用方法
public function testExample() { $hoges = ['hoge', 'hoge2']; $this->assertCount(1, $hoges, '失敗時に表示されるよ'); }
18. assertDirectoryExists
・チェック内容
ディレクトリが存在していること
・引数
@param string $directory
@param string $message = ''
・使用方法
public function testExample() { $this->assertDirectoryExists('/tmp', '失敗時に表示されるよ'); }
18. assertDirectoryDoesNotExist
・チェック内容
ディレクトリが存在していないこと
・引数
@param string $directory
@param string $message = ''
・使用方法
public function testExample() { $this->assertDirectoryDoesNotExist('/hoge', '失敗時に表示されるよ'); }
19. assertDirectoryIsReadable
・チェック内容
ディレクトリが存在している かつ 読みこみ可能であること
・引数
@param string $directory
@param string $message = ''
・使用方法
public function testExample() { $this->assertDirectoryIsReadable('/tmp', '失敗時に表示されるよ'); }
20. assertDirectoryIsNotReadable
・チェック内容
ディレクトリが存在している かつ 読みこみ可能でないこと
・引数
@param string $directory
@param string $message = ''
・使用方法
public function testExample() { $this->assertDirectoryIsNotReadable('/root', '失敗時に表示されるよ'); }
21. assertDirectoryIsWritable
・チェック内容
ディレクトリが存在している かつ 書き込み可能であること
・引数
@param string $directory
@param string $message = ''
・使用方法
public function testExample() { $this->assertDirectoryIsWritable('/tmp', '失敗時に表示されるよ'); }
22. assertDirectoryIsNotWritable
・チェック内容
ディレクトリが存在している かつ 書き込み可能でないこと
・引数
@param string $directory
@param string $message = ''
・使用方法
public function testExample() { $this->assertDirectoryIsNotWritable('/root', '失敗時に表示されるよ'); }
23. assertEmpty
・チェック内容
変数が空であること
・引数
@param mixed $actual
@param string $message = ''
・使用方法
public function testExample() { $hoge = ''; $this->assertEmpty($hoge, '失敗時に表示されるよ'); }
24. assertNotEmpty
・チェック内容
変数が空でないこと
・引数
@param mixed $actual
@param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertNotEmpty($hoge, '失敗時に表示されるよ'); }
25. assertEquals
・チェック内容
値が同一であること
・引数
@param mixed $expected @param mixed $actual @param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertEquals('hoge', $hoge, '失敗時に表示されるよ'); }
26. assertNotEquals
・チェック内容
値が同一でないこと
・引数
@param mixed $expected @param mixed $actual @param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertNotEquals('huga', $hoge, '失敗時に表示されるよ'); }
27. assertEqualsCanonicalizing
・チェック内容
値が同一であること
・引数
@param mixed $expected @param mixed $actual @param string $message = ''
・使用方法
public function testExample() { $hoges = ['hoge' => 1, 'hoge2' => 2, 'hoge3' => 3]; $this->assertEqualsCanonicalizing(['hoge2' => 2, 'hoge' => 1, 'hoge3' => 3], $hoges, '失敗時に表示されるよ'); }
※配列の場合、値でソートされる
※連想配列の場合、キーは条件に含まれない(0~に変更される)
28. assertNotEqualsCanonicalizing
・チェック内容
値が同一でないこと
・引数
@param mixed $expected @param mixed $actual @param string $message = ''
・使用方法
public function testExample() { $hoges = ['hoge' => 1, 'hoge2' => 2, 'hoge3' => 3]; $this->assertNotEqualsCanonicalizing(['hoge2' => 2, 'hoge' => 1, 'hoge3' => 0], $hoges, '失敗時に表示されるよ'); }
※配列の場合、値でソートされる
※連想配列の場合、キーは条件に含まれない(0~に変更される)
29. assertEqualsIgnoringCase
・チェック内容
値が同一であること(大文字小文字を区別しない)
・引数
@param mixed $expected @param mixed $actual @param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertEqualsIgnoringCase('HOGE', $hoge, '失敗時に表示されるよ'); }
※assertEqualsとの違いは、大文字小文字を区別しないこと
30. assertNotEqualsIgnoringCase
・チェック内容
値が同一でない(大文字小文字を区別しない)
・引数
@param mixed $expected @param mixed $actual @param string $message = ''
・使用方法
public function testExample() { $hoge = 'hoge'; $this->assertNotEqualsIgnoringCase('huga', $hoge, '失敗時に表示されるよ'); }
※assertEqualsとの違いは、大文字小文字を区別しないこと
一旦ここまで。
時間がある時に随時更新する。
以上、よろしくお願いいたします。