Skip to content

Commit

Permalink
test: compiler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaened committed Aug 23, 2024
1 parent 0b1a419 commit f69c488
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/Compiler/CompilerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @author enea dhack <enea.so@live.com>
*/

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;
}
}
43 changes: 43 additions & 0 deletions tests/Compiler/Json/HostedJsonRoutesCompilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author enea dhack <enea.so@live.com>
*/

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';
}
}
47 changes: 47 additions & 0 deletions tests/Compiler/Json/JsonCompilerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @author enea dhack <enea.so@live.com>
*/

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',
];
}
}
45 changes: 45 additions & 0 deletions tests/Compiler/Json/LargeJsonRoutesCompilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @author enea dhack <enea.so@live.com>
*/

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';
}
}
43 changes: 43 additions & 0 deletions tests/Compiler/Json/PrefixedJsonRoutesCompilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author enea dhack <enea.so@live.com>
*/

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';
}
}
41 changes: 41 additions & 0 deletions tests/Compiler/Json/ShortJsonRoutesCompilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @author enea dhack <enea.so@live.com>
*/

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';
}
}

0 comments on commit f69c488

Please sign in to comment.