phpunit-skill について
phpunit-skillは、PHP開発者向けのPHPUnitテスト資料です。基本パターンに加え、テストデータからCI連携までの実践例を扱います。
主な機能
- 01テストケースとライフサイクルフック
- 02アサーションとデータプロバイダー
- 03モックとテストダブル
- 04HTTP、データベース、CIのパターン
活用シーン
- 01分離されたPHPUnitテストの構成
- 02厳密なアサーションによる動作確認
- 03リポジトリやAPIエンドポイントのテスト
- 04よくあるテスト問題の調査
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ lambdatest/ agent-skills --skill phpunit-skillインストール後、Claude Code で/phpunit-skillを入力して読み込まれたか確認します。
同梱ファイルreference/advanced-patterns.mdreference/playbook.md
詳細
PHPUnit テストスキル
基本パターン
基本的なテスト
<?php
use PHPUnit\Framework\TestCase;
class CalculatorTest extends TestCase
{
private Calculator $calculator;
protected function setUp(): void
{
$this->calculator = new Calculator();
}
public function testAddition(): void
{
$this->assertEquals(5, $this->calculator->add(2, 3));
}
public function testDivideByZero(): void
{
$this->expectException(\DivisionByZeroError::class);
$this->calculator->divide(10, 0);
}
public function testMultipleAssertions(): void
{
$this->assertSame(4, $this->calculator->add(2, 2));
$this->assertSame(0, $this->calculator->subtract(2, 2));
$this->assertSame(6, $this->calculator->multiply(2, 3));
}
}
データプロバイダー
/**
* @dataProvider additionProvider
*/
public function testAdd(int $a, int $b, int $expected): void
{
$this->assertEquals($expected, $this->calculator->add($a, $b));
}
public static function additionProvider(): array
{
return [
'positive numbers' => [2, 3, 5],
'negative numbers' => [-1, -1, -2],
'zeros' => [0, 0, 0],
'mixed' => [10, -5, 5],
];
}
アサーション
$this->assertEquals($expected, $actual);
$this->assertSame($expected, $actual); // Strict type
$this->assertNotEquals($unexpected, $actual);
$this->assertTrue($condition);
$this->assertFalse($condition);
$this->assertNull($value);
$this->assertNotNull($value);
$this->assertCount(3, $array);
$this->assertContains('item', $array);
$this->assertArrayHasKey('key', $array);
$this->assertInstanceOf(MyClass::class, $obj);
$this->assertStringContainsString('sub', $string);
$this->assertMatchesRegularExpression('/\d+/', $string);
$this->assertEmpty($collection);
$this->assertGreaterThan(5, $value);
$this->assertJsonStringEqualsJsonString($expected, $actual);
モック
public function testCreateUser(): void
{
$mockRepo = $this->createMock(UserRepository::class);
$mockRepo->expects($this->once())
->method('save')
->with($this->isInstanceOf(User::class))
->willReturn(new User(1, 'Alice'));
$mockEmail = $this->createMock(EmailService::class);
$mockEmail->expects($this->once())
->method('sendWelcome')
->with('[email protected]');
$service = new UserService($mockRepo, $mockEmail);
$result = $service->createUser('[email protected]', 'Alice');
$this->assertEquals(1, $result->getId());
}
ライフサイクル
public static function setUpBeforeClass(): void { } // Once before all
protected function setUp(): void { } // Before each test
protected function tearDown(): void { } // After each test
public static function tearDownAfterClass(): void { } // Once after all
アンチパターン
| 悪い例 | 良い例 | 理由 |
|---|---|---|
厳密な比較に assertEquals を使う | 型と値の比較に assertSame を使う | 型の強制変換 |
| データプロバイダーを使わない | @dataProvider | DRY |
| グローバル状態 | setUp()/tearDown() | 分離 |
| グループを使わない | @group smoke | サブセットを実行 |
セットアップ:composer require --dev phpunit/phpunit
実行:./vendor/bin/phpunit または ./vendor/bin/phpunit --group smoke
設定:テストスイートとカバレッジを含む phpunit.xml
応用パターン
本番環境で使えるパターンについては、reference/playbook.md をご覧ください。
| セクション | 内容 |
|---|---|
| §1 プロジェクトのセットアップ | composer.json、テストスイートを含む phpunit.xml、カバレッジ設定、プロジェクト構成 |
| §2 テストパターン | アサーション、#[DataProvider]、Generator の yield、厳密な比較 |
| §3 モック | コールバックを使う createMock、Mockery のスパイ、連続した戻り値 |
| §4 テストダブル | インメモリのフェイク、リポジトリパターン、テストヘルパー |
| §5 Faker とフィクスチャ | オーバーライドに対応した TestDataFactory、一括生成 |
| §6 例外テスト | 詳細な例外アサーション、警告のテスト |
| §7 HTTP と API のテスト | Symfony WebTestCase、認証、バリデーション、ページネーション |
| §8 データベースのテスト | トランザクションのロールバック、リポジトリの統合、Doctrine |
| §9 CI/CD 統合 | MySQL/Redis を使用した GitHub Actions、カバレッジのしきい値 |
| §10 デバッグ表 | よくある問題 12 件と原因・解決策 |
| §11 ベストプラクティス | 本番環境向け PHP テストのチェックリスト 14 項目 |
コメント
Markdown 対応