Testing

Testing your routes with WPZylos Routing.

Unit Testing Routes

Testing Frontend Routes

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use WPZylos\Framework\Routing\Router;
use WPZylos\Framework\Routing\Route;

class RouterTest extends TestCase
{
    private Router $router;

    protected function setUp(): void
    {
        $this->router = new Router();
    }

    public function testGetRegistersRoute(): void
    {
        $route = $this->router->get('/users', fn() => 'users');

        $this->assertInstanceOf(Route::class, $route);
        $this->assertEquals('GET', $route->getMethod());
        $this->assertEquals('/users', $route->getPattern());
    }

    public function testRouteSupportsMiddleware(): void
    {
        $route = $this->router->get('/admin', fn() => 'admin')
            ->middleware('auth');

        $this->assertEquals(['auth'], $route->getMiddleware());
    }

    public function testRouteSupportsWhereConstraints(): void
    {
        $route = $this->router->get('/posts/{id}', fn() => 'post')
            ->where('id', '[0-9]+');

        $this->assertEquals(['id' => '[0-9]+'], $route->getConstraints());
    }

    public function testRouteGroupAppliesPrefix(): void
    {
        $this->router->group(['prefix' => '/admin'], function ($router) {
            $router->get('/dashboard', fn() => 'dashboard');
        });

        $routes = $this->router->getRoutes();
        $this->assertStringContainsString('/admin', $routes[0]->getPattern());
    }
}

Testing REST Routes

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use WPZylos\Framework\Routing\RestRouter;

class RestRouterTest extends TestCase
{
    private RestRouter $router;

    protected function setUp(): void
    {
        $this->router = new RestRouter('my-plugin', 'v1');
    }

    public function testNamespaceIncludesVersion(): void
    {
        $this->assertEquals('my-plugin/v1', $this->router->getNamespace());
    }

    public function testResourceCreatesAllRoutes(): void
    {
        $this->router->resource('posts', 'PostController');

        $routes = $this->router->getRoutes();
        $this->assertCount(5, $routes);
    }

    public function testRouteConvertsToWpPattern(): void
    {
        $route = $this->router->get('/users/{id}', fn() => 'user');

        $this->assertEquals('/users/(?P<id>[^/]+)', $route->getWpPattern());
    }
}

Testing AJAX Routes

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use WPZylos\Framework\Routing\AjaxRouter;
use WPZylos\Framework\Routing\AjaxRoute;

class AjaxRouterTest extends TestCase
{
    public function testPublicRouteIsPublic(): void
    {
        $route = new AjaxRoute('search', fn() => [], true);

        $this->assertTrue($route->isPublic());
    }

    public function testPrivateRouteIsNotPublic(): void
    {
        $route = new AjaxRoute('save', fn() => [], false);

        $this->assertFalse($route->isPublic());
    }

    public function testNonceCanBeDisabled(): void
    {
        $route = new AjaxRoute('public', fn() => [], true);
        $route->withoutNonce();

        $this->assertFalse($route->shouldVerifyNonce());
    }
}

Integration Testing

Testing REST API Endpoints

<?php

namespace Tests\Integration;

use WP_UnitTestCase;
use WP_REST_Request;

class RestApiTest extends WP_UnitTestCase
{
    public function testUsersEndpointReturnsData(): void
    {
        $request = new WP_REST_Request('GET', '/my-plugin/v1/users');
        $response = rest_do_request($request);

        $this->assertEquals(200, $response->get_status());
        $this->assertArrayHasKey('data', $response->get_data());
    }

    public function testUnauthorizedAccessReturns401(): void
    {
        $request = new WP_REST_Request('POST', '/my-plugin/v1/posts');
        $response = rest_do_request($request);

        $this->assertEquals(401, $response->get_status());
    }

    public function testAuthenticatedUserCanCreatePost(): void
    {
        $user_id = $this->factory->user->create(['role' => 'author']);
        wp_set_current_user($user_id);

        $request = new WP_REST_Request('POST', '/my-plugin/v1/posts');
        $request->set_param('title', 'Test Post');
        $request->set_param('content', 'Test content');

        $response = rest_do_request($request);

        $this->assertEquals(201, $response->get_status());
    }
}

Running Tests

# Run all tests
composer run test

# Run with coverage
composer run test -- --coverage-html coverage

# Run specific test file
vendor/bin/phpunit tests/Unit/RouterTest.php

Mocking

Mock Container

$container = $this->createMock(ContainerInterface::class);
$container->method('get')
    ->willReturnCallback(fn($class) => new $class());

Mock Context

$context = $this->createMock(ContextInterface::class);
$context->method('prefix')->willReturn('myplugin_');
$context->method('slug')->willReturn('my-plugin');