Story 3.2: router php urls

This commit is contained in:
2026-02-04 17:06:28 +01:00
parent d520fe848f
commit 0409bb1327
13 changed files with 253 additions and 34 deletions

33
tests/router.test.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
require_once __DIR__ . '/../includes/router.php';
function assertTrue($cond, $msg) {
if (!$cond) {
fwrite(STDERR, $msg . PHP_EOL);
exit(1);
}
}
$router = new Router();
$router
->add('/', 'pages/home.php')
->add('/projets', 'pages/projects.php')
->add('/projet/{slug}', 'pages/project-single.php');
[$handler, $params] = $router->resolve('/');
assertTrue($handler === 'pages/home.php', 'home route failed');
[$handler, $params] = $router->resolve('/projets');
assertTrue($handler === 'pages/projects.php', 'projects route failed');
[$handler, $params] = $router->resolve('/projet/ecommerce-xyz');
assertTrue($handler === 'pages/project-single.php', 'project route failed');
assertTrue(($params[0] ?? '') === 'ecommerce-xyz', 'slug param failed');
[$handler, $params] = $router->resolve('/unknown');
assertTrue($handler === 'pages/404.php', '404 route failed');
[$handler, $params] = $router->resolve('/projets/');
assertTrue($handler === 'pages/projects.php', 'trailing slash failed');
fwrite(STDOUT, "OK\n");

15
tests/router.test.ps1 Normal file
View File

@@ -0,0 +1,15 @@
$ErrorActionPreference = 'Stop'
function Assert-True {
param(
[bool]$Condition,
[string]$Message
)
if (-not $Condition) { throw $Message }
}
Assert-True (Test-Path '.htaccess') 'Missing .htaccess'
$ht = Get-Content -Raw '.htaccess'
Assert-True ($ht -match 'RewriteRule') 'Missing RewriteRule in .htaccess'
'OK'

View File

@@ -8,5 +8,7 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path
& (Join-Path $here 'cta.test.ps1')
& (Join-Path $here 'home.test.ps1')
& (Join-Path $here 'quicknav.test.ps1')
& (Join-Path $here 'router.test.ps1')
php (Join-Path $here 'projects.test.php')
php (Join-Path $here 'router.test.php')
'OK'