Testing

How to test with the WPZylos Assets package.

Setup

composer install
composer test

Test Bootstrap

The tests/bootstrap.php mocks WordPress functions:

FunctionMock Behavior
wp_register_scriptNo-op
wp_enqueue_scriptNo-op
wp_register_styleNo-op
wp_enqueue_styleNo-op
wp_localize_scriptReturns true
wp_add_inline_scriptReturns true
wp_add_inline_styleReturns true
add_actionNo-op
add_filterNo-op
esc_urlReturns input

Writing Tests

use PHPUnit\Framework\TestCase;
use WPZylos\Framework\Assets\AssetManager;

class MyAssetTest extends TestCase
{
    private AssetManager $assets;

    protected function setUp(): void
    {
        $context = $this->createMock(ContextInterface::class);
        $context->method('assetHandle')->willReturnCallback(fn($h) => 'test-' . $h);
        $context->method('version')->willReturn('1.0.0');
        $context->method('url')->willReturnCallback(fn($p) => '/dist/' . $p);

        $this->assets = new AssetManager($context);
    }

    public function testScriptRegistration(): void
    {
        $script = $this->assets->script('app')->src('js/app.js')->admin();
        
        $this->assertEquals('admin', $script->getLocation());
        $this->assertStringContainsString('test-app', $script->getHandle());
    }
}

Running Tests

# All tests
vendor/bin/phpunit

# Specific test file
vendor/bin/phpunit tests/Unit/AssetManagerTest.php

# With coverage
vendor/bin/phpunit --coverage-html coverage/