AgentSkillsCN

phpunit-testing

Oh My Brand!主题中,针对PHP代码的PHPUnit测试。包括测试环境搭建、模拟WordPress/ACF函数、测试辅助函数与渲染模板。在为PHP代码编写单元测试时,请使用本指南。

SKILL.md
--- frontmatter
name: phpunit-testing
description: PHPUnit testing for PHP in Oh My Brand! theme. Test setup, mocking WordPress/ACF functions, testing helper functions and render templates. Use when writing unit tests for PHP code.
metadata:
  author: Wesley Smits
  version: "1.0.0"

PHPUnit Testing

Unit testing PHP code with PHPUnit for the Oh My Brand! WordPress FSE theme.


When to Use

  • Testing block helper functions
  • Testing render template logic
  • Mocking WordPress globals and functions
  • Mocking ACF field retrieval

Configuration

FileTemplatePurpose
phpunit.xmlTest configurationTest suites and coverage
bootstrap.phpTest bootstrapWordPress function mocks

Test Templates

Helper Function Tests

TemplatePurpose
GalleryHelpersTest.phpBlock attribute processing
FaqHelpersTest.phpJSON-LD generation, escaping

Mocking Tests

TemplatePurpose
AcfBlockTest.phpMock get_field() for ACF
DataProviderTest.phpParameterized tests

Test Structure

php
<?php

declare(strict_types=1);

namespace OMB\Tests\Blocks;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;

class ExampleTest extends TestCase
{
    #[Test]
    public function it_does_something(): void
    {
        // Arrange
        $input = [];

        // Act
        $result = some_function($input);

        // Assert
        $this->assertEmpty($result);
    }
}

Mocking WordPress Functions

Add to tests/php/bootstrap.php:

php
if (!function_exists('esc_html')) {
    function esc_html(string $text): string {
        return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
    }
}

Common mocks needed: esc_html, esc_attr, esc_url, wp_kses_post, __, get_the_ID, get_block_wrapper_attributes.

See bootstrap.php for complete mock list.


Mocking ACF

Use static property pattern for get_field:

php
private static array $mockFields = [];

public static function setUpBeforeClass(): void
{
    if (!function_exists('get_field')) {
        function get_field(string $field, $post_id = null) {
            return TestClass::getMockField($field);
        }
    }
}

See AcfBlockTest.php for complete example.


Data Providers

php
use PHPUnit\Framework\Attributes\DataProvider;

#[Test]
#[DataProvider('alignmentProvider')]
public function it_applies_alignment(string $align, string $expected): void
{
    // Test with provided data
}

public static function alignmentProvider(): array
{
    return [
        'wide' => ['wide', 'alignwide'],
        'full' => ['full', 'alignfull'],
    ];
}

See DataProviderTest.php for more patterns.


Running Tests

CommandPurpose
composer testRun all PHP tests
composer test -- --filter ClassNameRun specific class
composer test -- --filter method_nameRun specific test
composer test -- --coverage-html coverage/With coverage report
composer test -- --testdoxVerbose output
composer test -- --stop-on-failureStop on first failure

Related Skills


References