繰り返しのテスト
@dataProvider
/**
* @dataProvider testData
* @params int $year1
* @params int $year2
* @params bool $expected
*/
public function testGroupNameValidate($year1, $year2, $expected)
{
$this->assertSame($expected, $year1 === $year2);
}
public function testData()
{
return [
[2022, 2022, true],
[2022, 2023, false],
...
];
}
繰り返しが少なければ
@testWith
/**
* @testWith [2022, 2022, true]
* [2022, 2023, false]
*/
public function testGroupNameValidate($year1, $year2, $expected)
{
$this->assertSame($expected, $year1 === $year2);
}
例外のテストで例外発生後にDBチェックとか別のアサートしたい
try~catch と assertInstanceOf()
で例外を検証すればよい
try {
$class->execute();
$this->fail('例外発生なし');
} catch (\Exception $e) {
$this->assertInstanceOf('App\Exceptions\CustomException', $e);
$this->assertEquals($expectedMsg, $e->getMessage());
}