diff --git a/src/SAREhub/Microt/App/Controller/ControllerRoutesProvider.php b/src/SAREhub/Microt/App/Controller/ControllerRoutesProvider.php new file mode 100644 index 0000000..6285503 --- /dev/null +++ b/src/SAREhub/Microt/App/Controller/ControllerRoutesProvider.php @@ -0,0 +1,44 @@ +middlewares = $middlewares; + } + + /** + * @return ControllerActionRoutes + */ + public function get() + { + $routes = ControllerActionRoutes::create($this->getBaseUri(), $this->getControllerClass()); + $this->injectMiddlewares($routes); + $this->injectRoutes($routes); + return $routes; + } + + protected abstract function getBaseUri(): string; + + protected abstract function getControllerClass(): string; + + private function injectMiddlewares(ControllerActionRoutes $routes) + { + foreach ($this->middlewares as $middleware) { + $routes->addMiddleware($middleware); + } + } + + protected abstract function injectRoutes(ControllerActionRoutes $routes); +} \ No newline at end of file diff --git a/tests/unit/SAREhub/Microt/App/Controller/ControllerRoutesProviderTest.php b/tests/unit/SAREhub/Microt/App/Controller/ControllerRoutesProviderTest.php new file mode 100644 index 0000000..3769cd1 --- /dev/null +++ b/tests/unit/SAREhub/Microt/App/Controller/ControllerRoutesProviderTest.php @@ -0,0 +1,79 @@ +actionRoute = ControllerActionRoute::get("test", "test"); + } + + + protected function getBaseUri(): string + { + return "test_base_uri"; + } + + protected function getControllerClass(): string + { + return "test_controller_class"; + } + + protected function injectRoutes(ControllerActionRoutes $routes) + { + $routes->addRoute($this->actionRoute); + } +} + +class ControllerRoutesProviderTest extends TestCase +{ + + public function testGetThenRoutesHasBaseUri() + { + $provider = new ExampleControllerRoutesProvider(); + + $routes = $provider->get(); + + $this->assertEquals("test_base_uri", $routes->getBaseUri()); + } + + public function testGetThenRoutesHasControllerClass() + { + $middleware = function () { + }; + $provider = new ExampleControllerRoutesProvider([$middleware]); + + $routes = $provider->get(); + + $this->assertEquals("test_controller_class", $routes->getController()); + } + + public function testGetThenRoutesHasMiddlewares() + { + $middleware = function () { + + }; + $provider = new ExampleControllerRoutesProvider([$middleware]); + + $routes = $provider->get(); + + $this->assertEquals([$middleware], $routes->getMiddlewares()); + } + + public function testGetThenRoutesHasActionRoutes() + { + $provider = new ExampleControllerRoutesProvider(); + + $routes = $provider->get(); + + $this->assertSame($provider->actionRoute, $routes->getRoutes()[0]); + } +}