Testing
How to test with the WPZylos Assets package.
Setup
composer install
composer test
Test Bootstrap
The tests/bootstrap.php mocks WordPress functions:
| Function | Mock Behavior |
|---|---|
wp_register_script | No-op |
wp_enqueue_script | No-op |
wp_register_style | No-op |
wp_enqueue_style | No-op |
wp_localize_script | Returns true |
wp_add_inline_script | Returns true |
wp_add_inline_style | Returns true |
add_action | No-op |
add_filter | No-op |
esc_url | Returns 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/