chore: complete update
This commit is contained in:
79
tests/Framework/StaticSite/RouteCollectorTest.php
Normal file
79
tests/Framework/StaticSite/RouteCollectorTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\StaticSite;
|
||||
|
||||
use App\Framework\Router\HttpRouter;
|
||||
use App\Framework\Router\Route;
|
||||
use App\Framework\Router\RouteCollection;
|
||||
use App\Framework\StaticSite\RouteCollector;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RouteCollectorTest extends TestCase
|
||||
{
|
||||
public function testCollectRoutes(): void
|
||||
{
|
||||
// Testdaten vorbereiten
|
||||
$routes = [
|
||||
new Route(path: '/', controller: 'TestController', action: 'index', methods: ['GET']),
|
||||
new Route(path: '/about', controller: 'TestController', action: 'about', methods: ['GET']),
|
||||
new Route(path: '/api/data', controller: 'ApiController', action: 'getData', methods: ['GET']),
|
||||
new Route(path: '/contact', controller: 'TestController', action: 'contact', methods: ['POST']),
|
||||
new Route(path: '/users/{id}', controller: 'UserController', action: 'show', methods: ['GET']),
|
||||
];
|
||||
|
||||
$routeCollection = $this->createMock(RouteCollection::class);
|
||||
$routeCollection->method('getAll')->willReturn($routes);
|
||||
|
||||
$router = $this->createMock(HttpRouter::class);
|
||||
$router->method('getRoutes')->willReturn($routeCollection);
|
||||
|
||||
// RouteCollector initialisieren
|
||||
$collector = new RouteCollector($router);
|
||||
|
||||
// Routen sammeln
|
||||
$collectedRoutes = $collector->collectRoutes();
|
||||
|
||||
// Prüfen, ob die richtigen Routen gesammelt wurden (nur GET-Methoden)
|
||||
$this->assertContains('/', $collectedRoutes);
|
||||
$this->assertContains('/about', $collectedRoutes);
|
||||
$this->assertContains('/api/data', $collectedRoutes);
|
||||
$this->assertContains('/users/{id}', $collectedRoutes);
|
||||
$this->assertNotContains('/contact', $collectedRoutes); // POST-Route sollte nicht enthalten sein
|
||||
}
|
||||
|
||||
public function testFilterRoutes(): void
|
||||
{
|
||||
// Testdaten vorbereiten
|
||||
$routes = [
|
||||
'/',
|
||||
'/about',
|
||||
'/api/data',
|
||||
'/api/users',
|
||||
'/admin/dashboard',
|
||||
'/users/{id}',
|
||||
'/products/{slug}',
|
||||
];
|
||||
|
||||
$excludePatterns = [
|
||||
'/\/api\/.*/', // API-Routen ausschließen
|
||||
'/\/admin\/.*/', // Admin-Routen ausschließen
|
||||
];
|
||||
|
||||
$router = $this->createMock(HttpRouter::class);
|
||||
$collector = new RouteCollector($router);
|
||||
|
||||
// Routen filtern
|
||||
$filteredRoutes = $collector->filterRoutes($routes, $excludePatterns);
|
||||
|
||||
// Prüfen, ob die richtigen Routen gefiltert wurden
|
||||
$this->assertContains('/', $filteredRoutes);
|
||||
$this->assertContains('/about', $filteredRoutes);
|
||||
$this->assertNotContains('/api/data', $filteredRoutes); // Sollte durch Pattern ausgeschlossen sein
|
||||
$this->assertNotContains('/api/users', $filteredRoutes); // Sollte durch Pattern ausgeschlossen sein
|
||||
$this->assertNotContains('/admin/dashboard', $filteredRoutes); // Sollte durch Pattern ausgeschlossen sein
|
||||
$this->assertNotContains('/users/{id}', $filteredRoutes); // Sollte durch Platzhalter ausgeschlossen sein
|
||||
$this->assertNotContains('/products/{slug}', $filteredRoutes); // Sollte durch Platzhalter ausgeschlossen sein
|
||||
}
|
||||
}
|
||||
102
tests/Framework/StaticSite/StaticPageCollectorTest.php
Normal file
102
tests/Framework/StaticSite/StaticPageCollectorTest.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\StaticSite;
|
||||
|
||||
use App\Framework\Attributes\StaticPage;
|
||||
use App\Framework\Router\HttpRouter;
|
||||
use App\Framework\Router\Route;
|
||||
use App\Framework\Router\RouteCollection;
|
||||
use App\Framework\StaticSite\StaticPageCollector;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StaticPageCollectorTest extends TestCase
|
||||
{
|
||||
public function testCollectStaticPages(): void
|
||||
{
|
||||
// Controller-Klasse mit StaticPage-Attribut simulieren
|
||||
$controllerClass = 'TestStaticPageController';
|
||||
$this->createTestControllerClass($controllerClass);
|
||||
|
||||
// Testdaten vorbereiten
|
||||
$routes = [
|
||||
new Route(path: '/', controller: $controllerClass, action: 'indexAction', methods: ['GET']),
|
||||
new Route(path: '/about', controller: $controllerClass, action: 'aboutAction', methods: ['GET']),
|
||||
new Route(path: '/contact', controller: $controllerClass, action: 'contactAction', methods: ['GET']),
|
||||
new Route(path: '/api/data', controller: 'ApiController', action: 'getData', methods: ['GET']),
|
||||
];
|
||||
|
||||
$routeCollection = $this->createMock(RouteCollection::class);
|
||||
$routeCollection->method('getAll')->willReturn($routes);
|
||||
|
||||
$router = $this->createMock(HttpRouter::class);
|
||||
$router->method('getRoutes')->willReturn($routeCollection);
|
||||
|
||||
// StaticPageCollector initialisieren
|
||||
$collector = new StaticPageCollector($router);
|
||||
|
||||
// Statische Seiten sammeln
|
||||
$staticPages = $collector->collectStaticPages();
|
||||
|
||||
// Prüfen, ob die richtigen Seiten gesammelt wurden
|
||||
$this->assertContains('/', $staticPages);
|
||||
$this->assertContains('/about', $staticPages);
|
||||
$this->assertNotContains('/contact', $staticPages); // Kein StaticPage-Attribut
|
||||
$this->assertNotContains('/api/data', $staticPages); // Anderer Controller ohne StaticPage-Attribut
|
||||
}
|
||||
|
||||
public function testCollectAllGetRoutes(): void
|
||||
{
|
||||
// Testdaten vorbereiten
|
||||
$routes = [
|
||||
new Route(path: '/', controller: 'TestController', action: 'index', methods: ['GET']),
|
||||
new Route(path: '/about', controller: 'TestController', action: 'about', methods: ['GET']),
|
||||
new Route(path: '/contact', controller: 'TestController', action: 'contact', methods: ['POST']),
|
||||
new Route(path: '/api/data', controller: 'ApiController', action: 'getData', methods: ['GET']),
|
||||
];
|
||||
|
||||
$routeCollection = $this->createMock(RouteCollection::class);
|
||||
$routeCollection->method('getAll')->willReturn($routes);
|
||||
|
||||
$router = $this->createMock(HttpRouter::class);
|
||||
$router->method('getRoutes')->willReturn($routeCollection);
|
||||
|
||||
// StaticPageCollector initialisieren
|
||||
$collector = new StaticPageCollector($router);
|
||||
|
||||
// Alle GET-Routen sammeln
|
||||
$allGetRoutes = $collector->collectAllGetRoutes();
|
||||
|
||||
// Prüfen, ob die richtigen Seiten gesammelt wurden
|
||||
$this->assertContains('/', $allGetRoutes);
|
||||
$this->assertContains('/about', $allGetRoutes);
|
||||
$this->assertContains('/api/data', $allGetRoutes);
|
||||
$this->assertNotContains('/contact', $allGetRoutes); // POST-Route sollte nicht enthalten sein
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt eine temporäre Controller-Klasse mit StaticPage-Attributen für Tests
|
||||
*/
|
||||
private function createTestControllerClass(string $className): void
|
||||
{
|
||||
if (class_exists($className)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$code = <<<EOT
|
||||
class {$className} {
|
||||
#[\App\Framework\Attributes\StaticPage]
|
||||
public function indexAction() {}
|
||||
|
||||
#[\App\Framework\Attributes\StaticPage(outputPath: 'custom-about')]
|
||||
public function aboutAction() {}
|
||||
|
||||
// Keine StaticPage-Annotation
|
||||
public function contactAction() {}
|
||||
}
|
||||
EOT;
|
||||
|
||||
eval($code);
|
||||
}
|
||||
}
|
||||
134
tests/Framework/StaticSite/StaticSiteGeneratorTest.php
Normal file
134
tests/Framework/StaticSite/StaticSiteGeneratorTest.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\StaticSite;
|
||||
|
||||
use App\Framework\Core\Application;
|
||||
use App\Framework\Http\Method;
|
||||
use App\Framework\Http\HttpResponse;
|
||||
use App\Framework\Http\Request;
|
||||
use App\Framework\Http\Response;
|
||||
use App\Framework\StaticSite\StaticSiteGenerator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StaticSiteGeneratorTest extends TestCase
|
||||
{
|
||||
private $outputDir;
|
||||
private $app;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Temporäres Verzeichnis für Tests erstellen
|
||||
$this->outputDir = sys_get_temp_dir() . '/static-site-test-' . uniqid();
|
||||
mkdir($this->outputDir, 0755, true);
|
||||
|
||||
// Mock für Application erstellen
|
||||
$this->app = $this->createMock(Application::class);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
// Temporäres Verzeichnis nach dem Test löschen
|
||||
$this->removeDirectory($this->outputDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hilfsfunktion zum rekursiven Löschen eines Verzeichnisses
|
||||
*/
|
||||
private function removeDirectory(string $dir): void
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$files = array_diff(scandir($dir), ['.', '..']);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$path = $dir . '/' . $file;
|
||||
is_dir($path) ? $this->removeDirectory($path) : unlink($path);
|
||||
}
|
||||
|
||||
rmdir($dir);
|
||||
}
|
||||
|
||||
public function testGenerateCreatesStaticFiles(): void
|
||||
{
|
||||
// Testdaten vorbereiten
|
||||
$routes = ['/', '/about', '/blog/post-1'];
|
||||
$responseBody = '<html><body>Test Content</body></html>';
|
||||
|
||||
// Mock für Response konfigurieren
|
||||
$response = $this->createMock(Response::class);
|
||||
$response->method('getBody')->willReturn($responseBody);
|
||||
|
||||
// App-Mock konfigurieren, um Response zurückzugeben
|
||||
$this->app->method('handleRequest')->willReturn($response);
|
||||
|
||||
// StaticSiteGenerator initialisieren
|
||||
$generator = new StaticSiteGenerator($this->app, $routes, $this->outputDir);
|
||||
|
||||
// Statische Seiten generieren
|
||||
$generator->generate();
|
||||
|
||||
// Prüfen, ob die erwarteten Dateien erstellt wurden
|
||||
$this->assertFileExists($this->outputDir . '/index.html');
|
||||
$this->assertFileExists($this->outputDir . '/about/index.html');
|
||||
$this->assertFileExists($this->outputDir . '/blog/post-1/index.html');
|
||||
|
||||
// Prüfen, ob der Inhalt korrekt ist
|
||||
$this->assertEquals($responseBody, file_get_contents($this->outputDir . '/index.html'));
|
||||
}
|
||||
|
||||
public function testGenerateHandlesExceptions(): void
|
||||
{
|
||||
// Testdaten vorbereiten
|
||||
$routes = ['/error-page'];
|
||||
|
||||
// App-Mock konfigurieren, um eine Exception zu werfen
|
||||
$this->app->method('handleRequest')->willThrowException(new \Exception('Test Exception'));
|
||||
|
||||
// StaticSiteGenerator initialisieren
|
||||
$generator = new StaticSiteGenerator($this->app, $routes, $this->outputDir);
|
||||
|
||||
// Output-Buffer verwenden, um die Echo-Ausgaben zu erfassen
|
||||
ob_start();
|
||||
$generator->generate();
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Prüfen, ob die Fehlermeldung ausgegeben wurde
|
||||
$this->assertStringContainsString('Fehler beim Generieren von /error-page', $output);
|
||||
|
||||
// Prüfen, ob keine Datei erstellt wurde
|
||||
$this->assertFileDoesNotExist($this->outputDir . '/error-page/index.html');
|
||||
}
|
||||
|
||||
public function testGetFilePathForRoute(): void
|
||||
{
|
||||
// StaticSiteGenerator mit Reflection für private Methoden testen
|
||||
$generator = new StaticSiteGenerator($this->app, [], $this->outputDir);
|
||||
$reflection = new \ReflectionClass($generator);
|
||||
$method = $reflection->getMethod('getFilePathForRoute');
|
||||
$method->setAccessible(true);
|
||||
|
||||
// Verschiedene Routentypen testen
|
||||
$this->assertEquals(
|
||||
$this->outputDir . '/index.html',
|
||||
$method->invoke($generator, '/')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$this->outputDir . '/about/index.html',
|
||||
$method->invoke($generator, '/about')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$this->outputDir . '/api/data.json',
|
||||
$method->invoke($generator, '/api/data.json')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user