Working state
This commit is contained in:
@@ -46,6 +46,21 @@ final class ExtensionTest extends TestCase
|
||||
], $extension->getGlobals());
|
||||
}
|
||||
|
||||
public function testSessionExtensionExposesNullDefaultsWhenSessionIsEmpty(): void
|
||||
{
|
||||
$_SESSION = [];
|
||||
|
||||
$extension = new SessionExtension();
|
||||
|
||||
self::assertSame([
|
||||
'session' => [
|
||||
'user_id' => null,
|
||||
'username' => null,
|
||||
'role' => null,
|
||||
],
|
||||
], $extension->getGlobals());
|
||||
}
|
||||
|
||||
public function testCsrfExtensionExposesTokens(): void
|
||||
{
|
||||
$storage = [];
|
||||
|
||||
75
tests/Shared/ProvisionerTest.php
Normal file
75
tests/Shared/ProvisionerTest.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Shared;
|
||||
|
||||
use App\Shared\Database\Provisioner;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ProvisionerTest extends TestCase
|
||||
{
|
||||
private PDO $db;
|
||||
private string $lockPath;
|
||||
private bool $lockExistedBefore;
|
||||
|
||||
/** @var array<string, string> */
|
||||
private array $envBackup;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->db = new PDO('sqlite::memory:', options: [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
$this->db->sqliteCreateFunction('strip_tags', 'strip_tags', 1);
|
||||
|
||||
$this->lockPath = dirname(__DIR__, 2) . '/database/.provision.lock';
|
||||
$this->lockExistedBefore = file_exists($this->lockPath);
|
||||
|
||||
$this->envBackup = [
|
||||
'ADMIN_USERNAME' => $_ENV['ADMIN_USERNAME'] ?? '',
|
||||
'ADMIN_EMAIL' => $_ENV['ADMIN_EMAIL'] ?? '',
|
||||
'ADMIN_PASSWORD' => $_ENV['ADMIN_PASSWORD'] ?? '',
|
||||
];
|
||||
|
||||
$_ENV['ADMIN_USERNAME'] = 'shared-admin';
|
||||
$_ENV['ADMIN_EMAIL'] = 'shared-admin@example.com';
|
||||
$_ENV['ADMIN_PASSWORD'] = 'strong-secret';
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->envBackup as $key => $value) {
|
||||
$_ENV[$key] = $value;
|
||||
}
|
||||
|
||||
if (!$this->lockExistedBefore && file_exists($this->lockPath)) {
|
||||
@unlink($this->lockPath);
|
||||
}
|
||||
}
|
||||
|
||||
public function testRunAppliesMigrationsSeedsAdminAndCreatesLockFile(): void
|
||||
{
|
||||
Provisioner::run($this->db);
|
||||
|
||||
self::assertFileExists($this->lockPath);
|
||||
|
||||
$migrationCount = (int) $this->db->query('SELECT COUNT(*) FROM migrations')->fetchColumn();
|
||||
self::assertGreaterThan(0, $migrationCount, 'Les migrations doivent être enregistrées');
|
||||
|
||||
$admin = $this->db->query("SELECT username, email, role FROM users WHERE username = 'shared-admin'")->fetch();
|
||||
self::assertIsArray($admin);
|
||||
self::assertSame('shared-admin@example.com', $admin['email']);
|
||||
self::assertSame('admin', $admin['role']);
|
||||
}
|
||||
|
||||
public function testRunIsIdempotentForAdminSeed(): void
|
||||
{
|
||||
Provisioner::run($this->db);
|
||||
Provisioner::run($this->db);
|
||||
|
||||
$adminCount = (int) $this->db->query("SELECT COUNT(*) FROM users WHERE username = 'shared-admin'")->fetchColumn();
|
||||
self::assertSame(1, $adminCount);
|
||||
}
|
||||
}
|
||||
61
tests/Shared/RoutesTest.php
Normal file
61
tests/Shared/RoutesTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Shared;
|
||||
|
||||
use App\Shared\Routes;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Slim\Factory\AppFactory;
|
||||
|
||||
final class RoutesTest extends TestCase
|
||||
{
|
||||
public function testRegisterDeclaresExpectedPublicAndProtectedRoutes(): void
|
||||
{
|
||||
$app = AppFactory::create();
|
||||
Routes::register($app);
|
||||
|
||||
$actual = [];
|
||||
foreach ($app->getRouteCollector()->getRoutes() as $route) {
|
||||
$pattern = $route->getPattern();
|
||||
$methods = $route->getMethods();
|
||||
|
||||
if (!isset($actual[$pattern])) {
|
||||
$actual[$pattern] = [];
|
||||
}
|
||||
|
||||
$actual[$pattern] = array_values(array_unique(array_merge($actual[$pattern], $methods)));
|
||||
sort($actual[$pattern]);
|
||||
}
|
||||
|
||||
$expected = [
|
||||
'/' => ['GET'],
|
||||
'/article/{slug}' => ['GET'],
|
||||
'/rss.xml' => ['GET'],
|
||||
'/auth/login' => ['GET', 'POST'],
|
||||
'/auth/logout' => ['POST'],
|
||||
'/password/forgot' => ['GET', 'POST'],
|
||||
'/password/reset' => ['GET', 'POST'],
|
||||
'/account/password' => ['GET', 'POST'],
|
||||
'/admin' => ['GET'],
|
||||
'/admin/posts' => ['GET'],
|
||||
'/admin/posts/edit/{id}' => ['GET', 'POST'],
|
||||
'/admin/posts/create' => ['POST'],
|
||||
'/admin/posts/delete/{id}' => ['POST'],
|
||||
'/admin/media/upload' => ['POST'],
|
||||
'/admin/media' => ['GET'],
|
||||
'/admin/media/delete/{id}' => ['POST'],
|
||||
'/admin/categories' => ['GET'],
|
||||
'/admin/categories/create' => ['POST'],
|
||||
'/admin/categories/delete/{id}' => ['POST'],
|
||||
'/admin/users' => ['GET'],
|
||||
'/admin/users/create' => ['GET', 'POST'],
|
||||
'/admin/users/role/{id}' => ['POST'],
|
||||
'/admin/users/delete/{id}' => ['POST'],
|
||||
];
|
||||
|
||||
ksort($expected);
|
||||
ksort($actual);
|
||||
|
||||
self::assertSame($expected, $actual);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user