Skip to main content
skill CX Codex

phpunit-skill — Claude Code skill: tests and installation

This skill generates PHPUnit tests for PHP code, including assertions, data providers, mocks, and test doubles. Use it when you need tests shaped around a class or service.

MIT 287 installs ★ 393 updated 2026-10-13

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add LambdaTest/agent-skills --skill phpunit-skill -a claude-code
Open on GitHub

What phpunit-skill does

phpunit-skill helps an agent write PHPUnit tests in PHP. It covers TestCase structure, strict and loose comparisons, exception checks, multiple assertions, data providers, and the setUp() and tearDown() lifecycle. Mock examples use repository and service dependencies.

The skill also describes project setup with composer.json and phpunit.xml, test suites, coverage, test groups, and commands such as ./vendor/bin/phpunit. Its reference/playbook.md adds patterns for API and HTTP tests, database work, fixtures, CI/CD, debugging, and in-memory fakes. PHPUnit must be added with composer require --dev phpunit/phpunit before those test commands can run.

It is available for Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, and OpenCode. Trigger it with requests that mention PHPUnit, TestCase, assertEquals, or PHP unit tests.

What it helps with

  • write a PHPUnit test for a PHP class
  • check an exception from invalid input
  • cover cases with a data provider
  • mock repository and service dependencies
  • run a selected PHPUnit group

Example prompts for the agent

  • Write PHPUnit tests for Calculator, including addition and division-by-zero cases.
  • Add a data provider covering positive, negative, zero, and mixed inputs.
  • Test the user service with mocks for UserRepository and EmailService.

How to install phpunit-skill

1Skills CLI (Vercel)

npx skills add LambdaTest/agent-skills --skill phpunit-skill -a claude-code

2Manually: put the files in place

~/.claude/skills/phpunit-skill/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/phpunit-skill/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about phpunit-skill

How do I install phpunit-skill in Claude Code?

Run npx skills add LambdaTest/agent-skills --skill phpunit-skill -a claude-code, then check ~/.claude/skills/phpunit-skill/SKILL.md.

What does phpunit-skill do?

It guides an agent in creating PHP tests with PHPUnit assertions, data providers, mocks, test doubles, and exception checks.

What is required to run the PHPUnit tests?

Add PHPUnit with composer require --dev phpunit/phpunit, then run ./vendor/bin/phpunit or ./vendor/bin/phpunit --group smoke. Project settings go in phpunit.xml.

Source text

SKILL.md · MIT
---
name: phpunit-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('[email protected]');

    $service = new UserService($mockRepo, $mockEmail);
    $result = $service->createUser('[email protected]', '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

| Bad | Good | Why |
|-----|------|-----|
| `assertEquals` for strict | `assertSame` for type+value | Type coercion |
| No data providers | `@dataProvider` | DRY |
| Global state | `setUp()`/`tearDown()` | Isolation |
| No groups | `@group smoke` | Run 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:

| Section | What You Get |
|---------|-------------|
| §1 Project Setup | composer.json, phpunit.xml with suites, coverage config, project structure |
| §2 Test Patterns | Assertions, #[DataProvider], Generator yields, strict comparisons |
| §3 Mocking | createMock with callbacks, Mockery spies, consecutive returns |
| §4 Test Doubles | In-memory fakes, repository pattern, test helpers |
| §5 Faker & Fixtures | TestDataFactory with overrides, bulk generation |
| §6 Exception Testing | Detailed exception assertions, warning testing |
| §7 HTTP & API Testing | Symfony WebTestCase, auth, validation, pagination |
| §8 Database Testing | Transaction rollback, repository integration, Doctrine |
| §9 CI/CD Integration | GitHub Actions with MySQL/Redis, coverage thresholds |
| §10 Debugging Table | 12 common problems with causes and fixes |
| §11 Best Practices | 14-item production PHP testing checklist |

© TestMu AI · MIT · LambdaTest/agent-skills

Similar skills

All skills for Testing and QA →
php-pro https://github.com/Jeffallan php-pro enforces PHP 8.3+ standards across Laravel and Symfony projects with strict typing, PHPStan level 9 checks, and automated Pest tests. CX Codex +1 16,400installs laravel Mindrally Configures your coding agent for Laravel and PHP 8.1+ development using strict typing and clean patterns. Helpful for building REST APIs, queues, and test suites. CX Codex +1 876installs php-expert personamanagmentlayer The php-expert skill guides PHP 8+, Laravel, Composer, and PHPUnit work. Use it for modern PHP implementation, testing, and code review. CX Codex +1 269installs Python Unit Test Generator — Comprehensive, Coverage-Mapped & Production-Ready sivasaiyadav8143 Python Unit Test Generator — Comprehensive, Coverage-Mapped & Production-Ready is a prompt that turns a Python snippet into a pytest suite with a coverage map and mocks for external dependencies. GP ChatGPT ★ 178,201repo stars test-doubles-mocking dev-toolings test-doubles-mocking — a skill for creating test doubles with PHPUnit mocks in Symfony, used to replace slow or external dependencies, verify method calls, and write simplified repository fakes in isolated unit tests. CX Codex +1 ★ 234repo stars typo3cms-extension-cursorrules-prompt-file PatrickJS The typo3cms-extension-cursorrules-prompt-file rule guides Cursor through TYPO3 CMS extension work, from PHP structure to tests and documentation. Use it when an agent is designing a new extension. CX Codex +1 ★ 41,178repo stars

From the same set47

The whole set →
selenium-skill Generates production-grade Selenium WebDriver tests across six programming languages. Useful for local test suites or cloud execution on TestMu AI. CX Codex +1 333installs jest-skill Produces Jest unit and integration tests across JavaScript and TypeScript, covering mocks, snapshots, and React components. CX Codex +1 292installs cicd-pipeline-skill cicd-pipeline-skill generates CI/CD configs for test automation: GitHub Actions, Jenkins, GitLab CI, Azure DevOps. Includes TestMu AI cloud integration. CX Codex +1 290installs junit-5-skill Generates production-grade JUnit 5 unit tests for Java with assertions, parameterized tests, Mockito mocks, and nested test groups. Use it to quickly cover your Java code with tests. CX Codex +1 289installs cucumber-skill Creates Gherkin feature files and matching step definitions. Use it to turn application behavior into Cucumber BDD scenarios. CX Codex +1 283installs webdriverio-skill Generates WebdriverIO end-to-end tests in JavaScript or TypeScript for local runs and TestMu AI cloud. CX Codex +1 265installs cypress-skill Generates production-grade E2E and component tests in JavaScript or TypeScript. Runs tests locally or on TestMu AI cloud, understands Cypress commands, and writes tests with proper selectors and network interception. CX Codex +1 264installs robot-framework-skill robot-framework-skill turns a described scenario into a .robot test in keyword-driven syntax, with SeleniumLibrary for browser flows and RequestsLibrary for API checks. CX Codex +1 253installs

Project sponsors

Companies that keep this analytics open