✨ Story 3.2: router php urls
This commit is contained in:
33
tests/router.test.php
Normal file
33
tests/router.test.php
Normal 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
15
tests/router.test.ps1
Normal 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'
|
||||
@@ -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'
|
||||
Reference in New Issue
Block a user