mcpField
P
phpunit-skill

phpunit-skill

github.com/lambdatestテスト・品質384271 回インストール2 週間前に更新
リポジトリ

phpunit-skill について

phpunit-skillは、PHP開発者向けのPHPUnitテスト資料です。基本パターンに加え、テストデータからCI連携までの実践例を扱います。

主な機能

  1. 01テストケースとライフサイクルフック
  2. 02アサーションとデータプロバイダー
  3. 03モックとテストダブル
  4. 04HTTP、データベース、CIのパターン

活用シーン

  1. 01分離されたPHPUnitテストの構成
  2. 02厳密なアサーションによる動作確認
  3. 03リポジトリやAPIエンドポイントのテスト
  4. 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 を使う型の強制変換
データプロバイダーを使わない@dataProviderDRY
グローバル状態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 項目

コメント