Php testing

From OrcaQubits/agentic-commerce-skills-plugins · View interactive version

Php testing is an AI agent skill from the GitHub repository OrcaQubits/agentic-commerce-skills-plugins. Write PHP tests with PHPUnit — unit tests, mocking, data providers, test doubles, assertions, and TDD practices. Use when writing tests for PHP code, whether in Magento or standalone PHP applications.

How to use this skill

Skill file

PHP Testing with PHPUnit

Before writing code

Fetch live docs: Web-search site:docs.phpunit.de phpunit 10 (or current version) for the latest PHPUnit documentation. Check https://phpunit.de/ for current version.

PHPUnit Fundamentals

Test Class Structure

Assertions

Common assertions:

Data Providers

Supply multiple test cases to a single test method:

Test Doubles

Mocks

Verify behavior — assert that methods were called with expected arguments:

Stubs

Provide canned responses — no behavior verification:

Method Return Behaviors

Consecutive Calls

willReturnOnConsecutiveCalls($val1, $val2, $val3) — different return per call.

Note: willReturnOnConsecutiveCalls() is deprecated in PHPUnit 10.3+. Use willReturn() with $this->onConsecutiveCalls() or willReturnCallback() with a counter instead.

Testing Patterns

Arrange-Act-Assert (AAA)

  1. Arrange — set up test data, mocks, dependencies
  2. Act — call the method under test
  3. Assert — verify the result

Testing Exceptions

$this->expectException(NoSuchEntityException::class);
$this->expectExceptionMessage('Entity not found');
$repository->getById(999);

Testing Private/Protected Methods

Don't test private methods directly — test through public methods. If a private method is complex enough to test independently, it should probably be extracted to its own class.

Testing with Dependency Injection

Create the class under test with mocked dependencies:

  1. Create mocks for all constructor parameters
  2. Instantiate the class with mocks
  3. Configure mock behavior per test
  4. Call the method and assert

Code Coverage

PHPUnit Configuration

phpunit.xml or phpunit.xml.dist:

Best Practices

Fetch PHPUnit docs for exact assertion methods, mock API, and configuration options for your PHPUnit version before writing tests.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.