mirror of
https://gh.wpcy.net/https://github.com/djav1985/v-wordpress-plugin-updater.git
synced 2026-04-25 04:12:14 +08:00
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
107 lines
3.9 KiB
PHP
107 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
// Ensure the autoloader is loaded from the correct location
|
|
require_once __DIR__ . '/../update-api/vendor/autoload.php';
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use App\Core\Router;
|
|
|
|
class RouterTest extends TestCase
|
|
{
|
|
private function runScript(string $code): array
|
|
{
|
|
$base = dirname(__DIR__);
|
|
$cmd = 'cd ' . escapeshellarg($base) . ' && php -r ' . escapeshellarg($code);
|
|
$output = [];
|
|
$exit = 0;
|
|
exec($cmd, $output, $exit);
|
|
return [$output, $exit];
|
|
}
|
|
|
|
public function testGetInstanceReturnsSameRouter(): void
|
|
{
|
|
$instance1 = Router::getInstance();
|
|
$instance2 = Router::getInstance();
|
|
$this->assertSame($instance1, $instance2);
|
|
}
|
|
|
|
public function testRedirectRoot(): void
|
|
{
|
|
$code = <<<'PHP'
|
|
namespace App\Core { function header($h){ echo $h; throw new \Exception(); } }
|
|
namespace { require 'tests/DummyControllers.php'; require 'update-api/vendor/autoload.php'; try { App\Core\Router::getInstance()->dispatch('GET', '/'); } catch (\Exception $e) {} }
|
|
PHP;
|
|
[$out] = $this->runScript($code);
|
|
$this->assertSame('Location: /home', $out[0] ?? '');
|
|
}
|
|
|
|
public function testNotFoundRoute(): void
|
|
{
|
|
$code = <<<'PHP'
|
|
namespace { require 'tests/DummyControllers.php'; require 'update-api/vendor/autoload.php'; App\Core\Router::getInstance()->dispatch('GET', '/missing'); }
|
|
PHP;
|
|
[$out] = $this->runScript($code);
|
|
$this->assertStringContainsString('404', implode('', $out));
|
|
}
|
|
|
|
public function testMethodNotAllowed(): void
|
|
{
|
|
$code = <<<'PHP'
|
|
namespace { require 'tests/DummyControllers.php'; require 'update-api/vendor/autoload.php'; App\Core\Router::getInstance()->dispatch('DELETE', '/home'); }
|
|
PHP;
|
|
[$out] = $this->runScript($code);
|
|
$this->assertSame('', implode('', $out));
|
|
}
|
|
|
|
public function testDispatchesRouteHandler(): void
|
|
{
|
|
$code = <<<'PHP'
|
|
namespace App\Core { class SessionManager { public static function getInstance(){ return new self(); } public function requireAuth(){ return true; } } }
|
|
namespace { require 'tests/DummyControllers.php'; require 'update-api/vendor/autoload.php'; ob_start(); App\Core\Router::getInstance()->dispatch('GET', '/home'); echo ob_get_clean(); }
|
|
PHP;
|
|
[$out] = $this->runScript($code);
|
|
$this->assertStringContainsString('home', implode('', $out));
|
|
}
|
|
|
|
public function testApiRouteMissingParamsRequiresAuth(): void
|
|
{
|
|
$code = <<<'PHP'
|
|
namespace App\Core { class SessionManager { public static int $count = 0; public static function getInstance(){ return new self(); } public function requireAuth(){ self::$count++; return true; } } }
|
|
namespace { require 'tests/DummyControllers.php'; require 'update-api/vendor/autoload.php'; ob_start(); App\Core\Router::getInstance()->dispatch('GET', '/api'); ob_end_clean(); echo App\Core\SessionManager::$count; }
|
|
PHP;
|
|
[$out] = $this->runScript($code);
|
|
$this->assertGreaterThanOrEqual(1, (int)($out[0] ?? 0));
|
|
}
|
|
|
|
public function testApiRouteWithParamsBypassesAuth(): void
|
|
{
|
|
$code = <<<'PHP'
|
|
namespace App\Core {
|
|
class SessionManager {
|
|
public static int $count = 0;
|
|
public static function getInstance() { return new self(); }
|
|
public function requireAuth() { self::$count++; return true; }
|
|
}
|
|
}
|
|
namespace {
|
|
// Populate all expected query parameters so the router should bypass auth
|
|
$_GET = [
|
|
'domain' => 'example.com',
|
|
'key' => 'test-key',
|
|
'slug' => 'dummy-plugin',
|
|
'version' => '1.0.0',
|
|
];
|
|
require 'tests/DummyControllers.php';
|
|
require 'update-api/vendor/autoload.php';
|
|
ob_start();
|
|
App\Core\Router::getInstance()->dispatch('GET', '/api');
|
|
ob_end_clean();
|
|
echo App\Core\SessionManager::$count;
|
|
}
|
|
PHP;
|
|
[$out] = $this->runScript($code);
|
|
$this->assertSame(0, (int)($out[0] ?? 0));
|
|
}
|
|
}
|