phpunit-testing

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.

SKILL.md

GitHub

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

File Template Purpose
phpunit.xml Test configuration Test suites and coverage
bootstrap.php Test bootstrap WordPress function mocks

Test Templates

Helper Function Tests

Template Purpose
GalleryHelpersTest.php Block attribute processing
FaqHelpersTest.php JSON-LD generation, escaping

Mocking Tests

Template Purpose
AcfBlockTest.php Mock get_field() for ACF
DataProviderTest.php Parameterized tests

Test Structure

<?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:

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:

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

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

Command Purpose
composer test Run all PHP tests
composer test -- --filter ClassName Run specific class
composer test -- --filter method_name Run specific test
composer test -- --coverage-html coverage/ With coverage report
composer test -- --testdox Verbose output
composer test -- --stop-on-failure Stop on first failure

Related Skills


References