$npx -y skills add LambdaTest/agent-skills --skill phpunit-skillGenerates 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".
| 1 | # PHPUnit Testing Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Basic Test |
| 6 | |
| 7 | ```php |
| 8 | <?php |
| 9 | use PHPUnit\Framework\TestCase; |
| 10 | |
| 11 | class CalculatorTest extends TestCase |
| 12 | { |
| 13 | private Calculator $calculator; |
| 14 | |
| 15 | protected function setUp(): void |
| 16 | { |
| 17 | $this->calculator = new Calculator(); |
| 18 | } |
| 19 | |
| 20 | public function testAddition(): void |
| 21 | { |
| 22 | $this->assertEquals(5, $this->calculator->add(2, 3)); |
| 23 | } |
| 24 | |
| 25 | public function testDivideByZero(): void |
| 26 | { |
| 27 | $this->expectException(\DivisionByZeroError::class); |
| 28 | $this->calculator->divide(10, 0); |
| 29 | } |
| 30 | |
| 31 | public function testMultipleAssertions(): void |
| 32 | { |
| 33 | $this->assertSame(4, $this->calculator->add(2, 2)); |
| 34 | $this->assertSame(0, $this->calculator->subtract(2, 2)); |
| 35 | $this->assertSame(6, $this->calculator->multiply(2, 3)); |
| 36 | } |
| 37 | } |
| 38 | ``` |
| 39 | |
| 40 | ### Data Providers |
| 41 | |
| 42 | ```php |
| 43 | /** |
| 44 | * @dataProvider additionProvider |
| 45 | */ |
| 46 | public function testAdd(int $a, int $b, int $expected): void |
| 47 | { |
| 48 | $this->assertEquals($expected, $this->calculator->add($a, $b)); |
| 49 | } |
| 50 | |
| 51 | public static function additionProvider(): array |
| 52 | { |
| 53 | return [ |
| 54 | 'positive numbers' => [2, 3, 5], |
| 55 | 'negative numbers' => [-1, -1, -2], |
| 56 | 'zeros' => [0, 0, 0], |
| 57 | 'mixed' => [10, -5, 5], |
| 58 | ]; |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ### Assertions |
| 63 | |
| 64 | ```php |
| 65 | $this->assertEquals($expected, $actual); |
| 66 | $this->assertSame($expected, $actual); // Strict type |
| 67 | $this->assertNotEquals($unexpected, $actual); |
| 68 | $this->assertTrue($condition); |
| 69 | $this->assertFalse($condition); |
| 70 | $this->assertNull($value); |
| 71 | $this->assertNotNull($value); |
| 72 | $this->assertCount(3, $array); |
| 73 | $this->assertContains('item', $array); |
| 74 | $this->assertArrayHasKey('key', $array); |
| 75 | $this->assertInstanceOf(MyClass::class, $obj); |
| 76 | $this->assertStringContainsString('sub', $string); |
| 77 | $this->assertMatchesRegularExpression('/\d+/', $string); |
| 78 | $this->assertEmpty($collection); |
| 79 | $this->assertGreaterThan(5, $value); |
| 80 | $this->assertJsonStringEqualsJsonString($expected, $actual); |
| 81 | ``` |
| 82 | |
| 83 | ### Mocking |
| 84 | |
| 85 | ```php |
| 86 | public function testCreateUser(): void |
| 87 | { |
| 88 | $mockRepo = $this->createMock(UserRepository::class); |
| 89 | $mockRepo->expects($this->once()) |
| 90 | ->method('save') |
| 91 | ->with($this->isInstanceOf(User::class)) |
| 92 | ->willReturn(new User(1, 'Alice')); |
| 93 | |
| 94 | $mockEmail = $this->createMock(EmailService::class); |
| 95 | $mockEmail->expects($this->once()) |
| 96 | ->method('sendWelcome') |
| 97 | ->with('alice@test.com'); |
| 98 | |
| 99 | $service = new UserService($mockRepo, $mockEmail); |
| 100 | $result = $service->createUser('alice@test.com', 'Alice'); |
| 101 | |
| 102 | $this->assertEquals(1, $result->getId()); |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ### Lifecycle |
| 107 | |
| 108 | ```php |
| 109 | public static function setUpBeforeClass(): void { } // Once before all |
| 110 | protected function setUp(): void { } // Before each test |
| 111 | protected function tearDown(): void { } // After each test |
| 112 | public static function tearDownAfterClass(): void { } // Once after all |
| 113 | ``` |
| 114 | |
| 115 | ### Anti-Patterns |
| 116 | |
| 117 | | Bad | Good | Why | |
| 118 | |-----|------|-----| |
| 119 | | `assertEquals` for strict | `assertSame` for type+value | Type coercion | |
| 120 | | No data providers | `@dataProvider` | DRY | |
| 121 | | Global state | `setUp()`/`tearDown()` | Isolation | |
| 122 | | No groups | `@group smoke` | Run subsets | |
| 123 | |
| 124 | ## Setup: `composer require --dev phpunit/phpunit` |
| 125 | ## Run: `./vendor/bin/phpunit` or `./vendor/bin/phpunit --group smoke` |
| 126 | ## Config: `phpunit.xml` with testsuites and coverage |
| 127 | |
| 128 | ## Deep Patterns |
| 129 | |
| 130 | See `reference/playbook.md` for production-grade patterns: |
| 131 | |
| 132 | | Section | What You Get | |
| 133 | |---------|-------------| |
| 134 | | §1 Project Setup | composer.json, phpunit.xml with suites, coverage config, project structure | |
| 135 | | §2 Test Patterns | Assertions, #[DataProvider], Generator yields, strict comparisons | |
| 136 | | §3 Mocking | createMock with callbacks, Mockery spies, consecutive returns | |
| 137 | | §4 Test Doubles | In-memory fakes, repository pattern, test helpers | |
| 138 | | §5 Faker & Fixtures | TestDataFactory with overrides, bulk generation | |
| 139 | | §6 Exception Testing | Detailed exception assertions, warning testing | |
| 140 | | §7 HTTP & API Testing | Symfony WebTestCase, auth, validation, pagination | |
| 141 | | §8 Database Testing | Transaction rollback, repository integration, Doctrine | |
| 142 | | §9 CI/CD Integration | GitHub Actions with MySQL/Redis, coverage thresholds | |
| 143 | | §10 Debugging Table | 12 common problems with causes and fixes | |
| 144 | | §11 Best Practices | 14-item production PHP testing checklist | |