phpunit

PHPUnit PHP testing framework. Use for PHP testing.

7 stars

Best use case

phpunit is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

PHPUnit PHP testing framework. Use for PHP testing.

Teams using phpunit should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/phpunit/SKILL.md --create-dirs "https://raw.githubusercontent.com/G1Joshi/Agent-Skills/main/skills/testing/phpunit/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/phpunit/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How phpunit Compares

Feature / AgentphpunitStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

PHPUnit PHP testing framework. Use for PHP testing.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# PHPUnit

PHPUnit is the standard unit testing framework for the PHP ecosystem. Examples include usage in Laravel, Symfony, and WordPress.

## When to Use

- **PHP Application**: The industry standard.
- **TDD**: Built-in support for mocking and code coverage.

## Quick Start

```php
<?php
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    public function testPushAndPop(): void
    {
        $stack = [];
        $this->assertEmpty($stack);

        array_push($stack, 'foo');
        $this->assertNotEmpty($stack);
        $this->assertEquals('foo', array_pop($stack));
    }
}
```

## Core Concepts

### Assertions

Methods like `$this->assertTrue()`, `$this->assertSame()`, `$this->expectException()`.

### Mock Objects

PHPUnit gives you control over the behavior of dependencies.

```php
$stub = $this->createMock(SomeClass::class);
$stub->method('doSomething')->willReturn('foo');
$this->assertEquals('foo', $stub->doSomething());
```

### Data Providers

Pass data to a test method (similar to parameterized tests).

```php
#[DataProvider('additionProvider')]
public function testAdd(int $a, int $b, int $expected): void { ... }
```

## Best Practices (2025)

**Do**:

- **Use Strict Assertions**: `$this->assertSame()` checks types (===), whereas `assertEquals` is loose (==). Strict is safer.
- **Use Namespaces**: Organize tests `Tests\Unit\UserTest` matching `App\Models\User`.
- **PHPUnit 11 features**: Use the new attributes `#[Test]` instead of `/** @test */` annotations if on PHP 8.2+.

**Don't**:

- **Don't test private methods**: Test the public API. If a private method is complex, extract it to a new class.

## References

- [PHPUnit Documentation](https://phpunit.de/documentation.html)