-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |