From f69c488b533dc7a55ccb86f4774c064bd8d92a51 Mon Sep 17 00:00:00 2001 From: Enea Dhack Date: Fri, 23 Aug 2024 14:25:40 -0500 Subject: [PATCH] test: compiler tests --- tests/Compiler/CompilerTestCase.php | 60 +++++++++++++++++++ .../Json/HostedJsonRoutesCompilerTest.php | 43 +++++++++++++ tests/Compiler/Json/JsonCompilerTestCase.php | 47 +++++++++++++++ .../Json/LargeJsonRoutesCompilerTest.php | 45 ++++++++++++++ .../Json/PrefixedJsonRoutesCompilerTest.php | 43 +++++++++++++ .../Json/ShortJsonRoutesCompilerTest.php | 41 +++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 tests/Compiler/CompilerTestCase.php create mode 100644 tests/Compiler/Json/HostedJsonRoutesCompilerTest.php create mode 100644 tests/Compiler/Json/JsonCompilerTestCase.php create mode 100644 tests/Compiler/Json/LargeJsonRoutesCompilerTest.php create mode 100644 tests/Compiler/Json/PrefixedJsonRoutesCompilerTest.php create mode 100644 tests/Compiler/Json/ShortJsonRoutesCompilerTest.php diff --git a/tests/Compiler/CompilerTestCase.php b/tests/Compiler/CompilerTestCase.php new file mode 100644 index 0000000..b8ce496 --- /dev/null +++ b/tests/Compiler/CompilerTestCase.php @@ -0,0 +1,60 @@ + + */ + +declare(strict_types=1); + +namespace Vaened\Laroute\Tests\Compiler; + +use Vaened\Laroute\Compilers\Compiler; +use Vaened\Laroute\FileRouteBuilder; +use Vaened\Laroute\Items\File; +use Vaened\Laroute\Normalizers\MultipleFilesNormalizer; +use Vaened\Laroute\Tests\TestCase; +use Vaened\Support\Types\ArrayList; + +use function file_get_contents; +use function json_decode; + +abstract class CompilerTestCase extends TestCase +{ + private ArrayList $files; + + private static array $expectedRoutes; + + abstract protected static function compiler(): Compiler; + + abstract protected static function expectedRoutesFromPath(): string; + + protected function setUp(): void + { + parent::setUp(); + + $filesNormalizer = static::create(MultipleFilesNormalizer::class); + $fileRouteBuilder = static::create(FileRouteBuilder::class); + $this->files = $filesNormalizer->normalize($fileRouteBuilder->files()); + + self::$expectedRoutes = json_decode(file_get_contents(static::expectedRoutesFromPath()), true); + } + + protected function files(): ArrayList + { + return $this->files; + } + + protected static function getStoreRoutes(): array + { + return self::$expectedRoutes['store']; + } + + protected static function getAdminRoutes(): array + { + return self::$expectedRoutes['admin']; + } + + protected static function module(string $module): callable + { + return static fn(File $file) => $file->name() === $module; + } +} \ No newline at end of file diff --git a/tests/Compiler/Json/HostedJsonRoutesCompilerTest.php b/tests/Compiler/Json/HostedJsonRoutesCompilerTest.php new file mode 100644 index 0000000..0add7bb --- /dev/null +++ b/tests/Compiler/Json/HostedJsonRoutesCompilerTest.php @@ -0,0 +1,43 @@ + + */ + +declare(strict_types=1); + +namespace Vaened\Laroute\Tests\Compiler\Json; + +use PHPUnit\Framework\Attributes\Test; + +final class HostedJsonRoutesCompilerTest extends JsonCompilerTestCase +{ + #[Test] + public function compile_hosted_routes(): void + { + $this->assertStoreRoutes(self::getStoreRoutes()); + $this->assertAdminRoutes(self::getAdminRoutes()); + } + + protected static function modules(): array + { + return [ + [ + 'match' => '/api/store', + 'name' => 'store', + 'rootUrl' => 'https://store.aplication.com', + 'absolute' => true, + ], + [ + 'match' => '/api/admin', + 'name' => 'admin', + 'rootUrl' => 'https://admin.aplication.com', + 'absolute' => true, + ], + ]; + } + + protected static function expectedRoutesFromPath(): string + { + return __DIR__ . '/../../routes/hosted-routes.json'; + } +} \ No newline at end of file diff --git a/tests/Compiler/Json/JsonCompilerTestCase.php b/tests/Compiler/Json/JsonCompilerTestCase.php new file mode 100644 index 0000000..3be3e23 --- /dev/null +++ b/tests/Compiler/Json/JsonCompilerTestCase.php @@ -0,0 +1,47 @@ + + */ + +declare(strict_types=1); + +namespace Vaened\Laroute\Tests\Compiler\Json; + +use Vaened\Laroute\Compilers\Compiler; +use Vaened\Laroute\Compilers\JsonFileCompiler; +use Vaened\Laroute\Tests\Compiler\CompilerTestCase; +use Vaened\Laroute\Utils; + +abstract class JsonCompilerTestCase extends CompilerTestCase +{ + protected function assertStoreRoutes(array $routes): void + { + $store = $this->files()->pick(self::module('store')); + + $this->assertEquals( + Utils::jsonEncode($routes), + $this->compiler()->compile($store) + ); + } + + protected function assertAdminRoutes(array $routes): void + { + $admin = $this->files()->pick(self::module('admin')); + $this->assertEquals( + Utils::jsonEncode($routes), + $this->compiler()->compile($admin) + ); + } + + protected static function compiler(): Compiler + { + return self::create(JsonFileCompiler::class); + } + + protected static function config(): array + { + return [ + 'output' => 'json', + ]; + } +} \ No newline at end of file diff --git a/tests/Compiler/Json/LargeJsonRoutesCompilerTest.php b/tests/Compiler/Json/LargeJsonRoutesCompilerTest.php new file mode 100644 index 0000000..e918ef2 --- /dev/null +++ b/tests/Compiler/Json/LargeJsonRoutesCompilerTest.php @@ -0,0 +1,45 @@ + + */ + +declare(strict_types=1); + +namespace Vaened\Laroute\Tests\Compiler\Json; + +use PHPUnit\Framework\Attributes\Test; + +final class LargeJsonRoutesCompilerTest extends JsonCompilerTestCase +{ + #[Test] + public function compile_large_routes(): void + { + $this->assertStoreRoutes(self::getStoreRoutes()); + $this->assertAdminRoutes(self::getAdminRoutes()); + } + + protected static function modules(): array + { + return [ + [ + 'match' => '/api/store', + 'name' => 'store', + 'rootUrl' => 'https://store.aplication.com', + 'prefix' => 'store-name', + 'absolute' => true, + ], + [ + 'match' => '/api/admin', + 'name' => 'admin', + 'rootUrl' => 'https://admin.aplication.com', + 'prefix' => 'dashboard', + 'absolute' => true, + ], + ]; + } + + protected static function expectedRoutesFromPath(): string + { + return __DIR__ . '/../../routes/large-routes.json'; + } +} \ No newline at end of file diff --git a/tests/Compiler/Json/PrefixedJsonRoutesCompilerTest.php b/tests/Compiler/Json/PrefixedJsonRoutesCompilerTest.php new file mode 100644 index 0000000..41fb7d4 --- /dev/null +++ b/tests/Compiler/Json/PrefixedJsonRoutesCompilerTest.php @@ -0,0 +1,43 @@ + + */ + +declare(strict_types=1); + +namespace Vaened\Laroute\Tests\Compiler\Json; + +use PHPUnit\Framework\Attributes\Test; + +final class PrefixedJsonRoutesCompilerTest extends JsonCompilerTestCase +{ + #[Test] + public function compile_prefixed_routes(): void + { + $this->assertStoreRoutes(self::getStoreRoutes()); + $this->assertAdminRoutes(self::getAdminRoutes()); + } + + protected static function modules(): array + { + return [ + [ + 'match' => '/api/store', + 'name' => 'store', + 'prefix' => 'store-name', + 'absolute' => false, + ], + [ + 'match' => '/api/admin', + 'name' => 'admin', + 'prefix' => 'dashboard', + 'absolute' => false, + ], + ]; + } + + protected static function expectedRoutesFromPath(): string + { + return __DIR__ . '/../../routes/prefixed-routes.json'; + } +} \ No newline at end of file diff --git a/tests/Compiler/Json/ShortJsonRoutesCompilerTest.php b/tests/Compiler/Json/ShortJsonRoutesCompilerTest.php new file mode 100644 index 0000000..fa27780 --- /dev/null +++ b/tests/Compiler/Json/ShortJsonRoutesCompilerTest.php @@ -0,0 +1,41 @@ + + */ + +declare(strict_types=1); + +namespace Vaened\Laroute\Tests\Compiler\Json; + +use PHPUnit\Framework\Attributes\Test; + +final class ShortJsonRoutesCompilerTest extends JsonCompilerTestCase +{ + #[Test] + public function compile_short_routes(): void + { + $this->assertStoreRoutes(self::getStoreRoutes()); + $this->assertAdminRoutes(self::getAdminRoutes()); + } + + protected static function modules(): array + { + return [ + [ + 'match' => '/api/store', + 'name' => 'store', + 'absolute' => false, + ], + [ + 'match' => '/api/admin', + 'name' => 'admin', + 'absolute' => false, + ], + ]; + } + + protected static function expectedRoutesFromPath(): string + { + return __DIR__ . '/../../routes/short-routes.json'; + } +} \ No newline at end of file