LoginSignup
0
0

More than 1 year has passed since last update.

phpunit のメモ

Last updated at Posted at 2022-05-20

繰り返しのテスト

@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());
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0