|
| 1 | +<?php |
| 2 | +namespace GT\WebEngine\Test\Dispatch; |
| 3 | + |
| 4 | +use GT\WebEngine\Dispatch\RouterFactory; |
| 5 | +use Gt\Routing\BaseRouter; |
| 6 | +use Gt\Routing\RouterConfig; |
| 7 | +use Gt\ServiceContainer\Container; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class RouterFactoryTest extends TestCase { |
| 11 | + private string $tmpDir; |
| 12 | + |
| 13 | + protected function setUp():void { |
| 14 | + parent::setUp(); |
| 15 | + $this->tmpDir = sys_get_temp_dir() . "/phpgt-webengine-test--Dispatch-RouterFactory-" . uniqid(); |
| 16 | + if(!is_dir($this->tmpDir)) { |
| 17 | + mkdir($this->tmpDir, recursive: true); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + protected function tearDown():void { |
| 22 | + foreach(scandir($this->tmpDir) ?: [] as $f) { |
| 23 | + if($f === '.' || $f === '..') { continue; } |
| 24 | + @unlink($this->tmpDir . DIRECTORY_SEPARATOR . $f); |
| 25 | + } |
| 26 | + @rmdir($this->tmpDir); |
| 27 | + parent::tearDown(); |
| 28 | + } |
| 29 | + |
| 30 | + private function writeRouterFile(string $namespace, string $className, string $filePath):void { |
| 31 | + $template = <<<'PHP' |
| 32 | + <?php |
| 33 | + namespace %NAMESPACE%; |
| 34 | + use Gt\Routing\BaseRouter; |
| 35 | + use Gt\Routing\RouterConfig; |
| 36 | + use Gt\ServiceContainer\Container; |
| 37 | + class %CLASS% extends BaseRouter { |
| 38 | + public ?RouterConfig $receivedConfig = null; |
| 39 | + public ?Container $receivedContainer = null; |
| 40 | + public function __construct(RouterConfig $config) { |
| 41 | + parent::__construct($config); |
| 42 | + $this->receivedConfig = $config; |
| 43 | + } |
| 44 | + public function setContainer(Container $container):void { |
| 45 | + parent::setContainer($container); |
| 46 | + $this->receivedContainer = $container; |
| 47 | + } |
| 48 | + } |
| 49 | + PHP; |
| 50 | + $code = strtr($template, [ |
| 51 | + '%NAMESPACE%' => $namespace, |
| 52 | + '%CLASS%' => $className, |
| 53 | + ]); |
| 54 | + file_put_contents($filePath, $code); |
| 55 | + } |
| 56 | + |
| 57 | + /** @noinspection PhpUndefinedFieldInspection */ |
| 58 | + public function testCreate_usesAppRouterWhenFileExists():void { |
| 59 | + $appNs = 'MyApp\\Routing'; |
| 60 | + $appClass = 'AppRouter'; |
| 61 | + $appFile = $this->tmpDir . '/AppRouter.php'; |
| 62 | + $this->writeRouterFile($appNs, $appClass, $appFile); |
| 63 | + |
| 64 | + $defaultNs = 'Default\\Routing'; |
| 65 | + $defaultClass = 'DefaultRouter'; |
| 66 | + $defaultFile = $this->tmpDir . '/DefaultRouter.php'; |
| 67 | + $this->writeRouterFile($defaultNs, $defaultClass, $defaultFile); |
| 68 | + |
| 69 | + $factory = new RouterFactory(); |
| 70 | + $container = new Container(); |
| 71 | + $router = $factory->create( |
| 72 | + $container, |
| 73 | + $appNs, |
| 74 | + $appFile, |
| 75 | + $appClass, |
| 76 | + $defaultFile, |
| 77 | + "\\$defaultNs\\$defaultClass", |
| 78 | + 303, |
| 79 | + 'text/html' |
| 80 | + ); |
| 81 | + |
| 82 | + self::assertInstanceOf(BaseRouter::class, $router); |
| 83 | + self::assertSame("$appNs\\$appClass", ltrim($router::class, '\\')); |
| 84 | + self::assertSame(303, $router->receivedConfig->redirectResponseCode); |
| 85 | + self::assertSame('text/html', $router->receivedConfig->defaultContentType); |
| 86 | + self::assertSame($container, $router->receivedContainer); |
| 87 | + } |
| 88 | + |
| 89 | + /** @noinspection PhpUndefinedFieldInspection */ |
| 90 | + public function testCreate_fallsBackToDefaultWhenAppFileMissing():void { |
| 91 | + $appNs = 'MyMissing\\Routing'; |
| 92 | + $appClass = 'MissingRouter'; |
| 93 | + $appFile = $this->tmpDir . '/MissingRouter.php'; // not created |
| 94 | + |
| 95 | + $defaultNs = 'Default\\Routing'; |
| 96 | + $defaultClass = 'DefaultRouter'; |
| 97 | + $defaultFile = $this->tmpDir . '/DefaultRouter.php'; |
| 98 | + $this->writeRouterFile($defaultNs, $defaultClass, $defaultFile); |
| 99 | + |
| 100 | + $factory = new RouterFactory(); |
| 101 | + $container = new Container(); |
| 102 | + $router = $factory->create( |
| 103 | + $container, |
| 104 | + $appNs, |
| 105 | + $appFile, |
| 106 | + $appClass, |
| 107 | + $defaultFile, |
| 108 | + "\\$defaultNs\\$defaultClass", |
| 109 | + 308, |
| 110 | + 'application/json' |
| 111 | + ); |
| 112 | + |
| 113 | + self::assertSame("$defaultNs\\$defaultClass", ltrim($router::class, '\\')); |
| 114 | + self::assertSame(308, $router->receivedConfig->redirectResponseCode); |
| 115 | + self::assertSame('application/json', $router->receivedConfig->defaultContentType); |
| 116 | + self::assertSame($container, $router->receivedContainer); |
| 117 | + } |
| 118 | +} |
0 commit comments