AgentSkillsCN

phpunit-testing-skill

使用PHP生成PHPUnit测试。涵盖断言、数据提供者、模拟以及测试双工。当用户提及“PHPUnit”“TestCase”“assertEquals”“PHP测试”时使用此功能。可通过“PHPUnit”“TestCase PHP”“assertEquals PHP”“PHP单元测试”等指令触发。

SKILL.md
--- frontmatter
name: phpunit-testing-skill
description: >
  Generates PHPUnit tests in PHP. Covers assertions, data providers, mocking,
  and test doubles. Use when user mentions "PHPUnit", "TestCase", "assertEquals",
  "PHP test". Triggers on: "PHPUnit", "TestCase PHP", "assertEquals PHP",
  "PHP unit test".
languages:
  - PHP
category: unit-testing
license: MIT
metadata:
  author: TestMu AI
  version: "1.0"

PHPUnit Testing Skill

Core Patterns

Basic Test

php
<?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));
    }
}

Data Providers

php
/**
 * @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],
    ];
}

Assertions

php
$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);

Mocking

php
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('alice@test.com');

    $service = new UserService($mockRepo, $mockEmail);
    $result = $service->createUser('alice@test.com', 'Alice');

    $this->assertEquals(1, $result->getId());
}

Lifecycle

php
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

Anti-Patterns

BadGoodWhy
assertEquals for strictassertSame for type+valueType coercion
No data providers@dataProviderDRY
Global statesetUp()/tearDown()Isolation
No groups@group smokeRun subsets

Setup: composer require --dev phpunit/phpunit

Run: ./vendor/bin/phpunit or ./vendor/bin/phpunit --group smoke

Config: phpunit.xml with testsuites and coverage

Deep Patterns

See reference/playbook.md for production-grade patterns:

SectionWhat You Get
§1 Project Setupcomposer.json, phpunit.xml with suites, coverage config, project structure
§2 Test PatternsAssertions, #[DataProvider], Generator yields, strict comparisons
§3 MockingcreateMock with callbacks, Mockery spies, consecutive returns
§4 Test DoublesIn-memory fakes, repository pattern, test helpers
§5 Faker & FixturesTestDataFactory with overrides, bulk generation
§6 Exception TestingDetailed exception assertions, warning testing
§7 HTTP & API TestingSymfony WebTestCase, auth, validation, pagination
§8 Database TestingTransaction rollback, repository integration, Doctrine
§9 CI/CD IntegrationGitHub Actions with MySQL/Redis, coverage thresholds
§10 Debugging Table12 common problems with causes and fixes
§11 Best Practices14-item production PHP testing checklist