home / skills / hoangnguyen0403 / agent-skills-standard / testing

testing skill

/skills/php/testing

This skill enforces PHP testing standards using Pest or PHPUnit, emphasizing TDD, isolation, and data-driven tests for unit and integration layers.

npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill testing

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.2 KB
---
name: PHP Testing
description: Unit and integration testing standards for PHP applications.
metadata:
  labels: [php, testing, phpunit, pest, tdd]
  triggers:
    files: ['tests/**/*.php', 'phpunit.xml']
    keywords: [phpunit, pest, mock, assert, tdd]
---

# PHP Testing

## **Priority: P1 (HIGH)**

## Structure

```text
tests/
├── Unit/
├── Integration/
└── Feature/
```

## Implementation Guidelines

- **Pest/PHPUnit**: Use Pest for DX or PHPUnit for legacy parity.
- **TDD Flow**: Follow Red-Green-Refactor cycle for new logic.
- **Isolation**: Mock dependencies via **Mockery** or PHPUnit mocks.
- **Strict Assertions**: Favor `assertSame` over `assertTrue`.
- **Data Providers**: Run tests against multiple sets via `@dataProvider`.
- **Categorize**: Separate Unit (isolated) from Integration (DB/API).

## Anti-Patterns

- **Testing Private**: **No Private Testing**: Validate public behavior only.
- **Over-Mocking**: **No Brittle Mocks**: Mock system boundaries only.
- **Blocking Tests**: **No Networking**: Use in-memory DBs and mocks.
- **Metric Chasing**: **No 100% Mania**: Prioritize quality over coverage.

## References

- [Testing Patterns & Mocks](references/implementation.md)

Overview

This skill codifies unit and integration testing standards for PHP applications to ensure reliable, maintainable test suites. It focuses on test structure, recommended tools (Pest or PHPUnit), isolation strategies, and practical anti-patterns to avoid. The guidance helps teams adopt a consistent TDD workflow and clear separation between Unit, Integration, and Feature tests.

How this skill works

The skill inspects test layout and tooling choices and enforces conventions like tests/Unit, tests/Integration, and tests/Feature. It recommends using Pest for developer experience or PHPUnit for legacy parity, applying Red-Green-Refactor TDD, and mocking external dependencies with Mockery or PHPUnit mocks. It also promotes strict assertions, data providers for varied inputs, and rules to prevent brittle or slow tests.

When to use it

  • When establishing or auditing PHP project testing standards.
  • When migrating legacy tests to a consistent structure or tooling set.
  • When onboarding developers to a team’s TDD and testing conventions.
  • When creating CI pipelines that must distinguish fast unit tests from slower integration tests.
  • When reducing flaky tests by eliminating network and database dependencies in unit tests.

Best practices

  • Organize tests into tests/Unit, tests/Integration, and tests/Feature to make intent and scope explicit.
  • Use Pest for improved DX or PHPUnit when maintaining backward compatibility.
  • Follow Red-Green-Refactor for new logic and prefer strict assertions like assertSame over loose truths.
  • Mock external dependencies at system boundaries only; use Mockery or PHPUnit mocks to isolate units.
  • Use data providers to run the same test against multiple input sets and surface edge cases quickly.
  • Keep unit tests fast and deterministic—use in-memory databases or mocks instead of real network or DB calls.

Example use cases

  • Writing a new service class: start with a failing unit test (Red), implement behavior, then refactor while keeping tests green.
  • Refactoring legacy code: add unit tests around public behavior before changing internals to prevent regressions.
  • CI configuration: run tests/Unit in a fast stage and tests/Integration in a staged or nightly pipeline to balance speed and coverage.
  • Investigating flaky tests: identify network or DB dependencies in unit tests and replace them with mocks or in-memory stores.
  • Adding parameterized tests: use @dataProvider to validate a function across valid and invalid inputs without duplicating code.

FAQ

Should I test private methods directly?

No. Test public behavior and outcomes; private methods are implementation details and should be covered by public-facing tests.

When should I use Pest vs PHPUnit?

Choose Pest for developer ergonomics and cleaner syntax; use PHPUnit when you need compatibility with existing legacy suites or tooling that depends on it.