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, assertions, test doubles, and TDD workflow. Use when writing tests for WooCommerce extensions or PHP projects.

How to use this skill

Skill file

PHP Testing with PHPUnit

Before writing code

Fetch live docs: Web-search site:phpunit.de documentation for the latest PHPUnit documentation. Check https://phpunit.de/ for current version and features.

PHPUnit Fundamentals

Test Class Structure

Assertions

Core assertions:

Exception Testing

Test Doubles

Mocks

Create mock objects to verify interactions:

$mock = $this->createMock( PaymentProcessor::class );
$mock->expects( $this->once() )
     ->method( 'charge' )
     ->with( $this->equalTo( 29.99 ) )
     ->willReturn( true );

Stubs

Return predetermined values without verifying calls:

$stub = $this->createStub( PriceCalculator::class );
$stub->method( 'calculate' )->willReturn( 29.99 );

Expectation Methods

willReturn Variants

Data Providers

What They Do

Supply multiple test cases to a single test method:

public static function priceProvider(): array {
    return [
        'regular price' => [ 29.99, 29.99, '' ],
        'sale price'    => [ 29.99, 19.99, '19.99' ],
        'zero price'    => [ 0.00, 0.00, '' ],
    ];
}

#[DataProvider('priceProvider')]
public function test_get_display_price( float $regular, float $expected, string $sale ): void {
    // test with each data set
}

Best Practices for Providers

WordPress/WooCommerce Testing Specifics

WP_UnitTestCase

Mocking WordPress Functions

Use WP_Mock or Brain\Monkey libraries:

Mocking HTTP Requests

Filter pre_http_request to intercept wp_remote_get() / wp_remote_post():

add_filter( 'pre_http_request', function( $preempt, $args, $url ) {
    return [ 'response' => [ 'code' => 200 ], 'body' => '{"status":"ok"}' ];
}, 10, 3 );

Test Organization

Directory Structure

tests/
├── bootstrap.php           # Test bootstrap
├── Unit/                   # Pure unit tests (no WP)
│   └── PriceCalculatorTest.php
├── Integration/            # Tests with WP/WC loaded
│   ├── ProductTest.php
│   └── OrderTest.php
└── E2E/                    # Playwright end-to-end
    └── specs/

phpunit.xml.dist Configuration

TDD Workflow

  1. Red — write a failing test that describes the desired behavior
  2. Green — write the minimum code to make the test pass
  3. Refactor — clean up code while keeping tests green
  4. Repeat

Best Practices

Fetch the PHPUnit documentation for exact assertion signatures, mock API, and configuration options before implementing.

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.