-
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.
* added ControllerRoutesProvider
- Loading branch information
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/SAREhub/Microt/App/Controller/ControllerRoutesProvider.php
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,44 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Microt\App\Controller; | ||
|
||
|
||
use SAREhub\Commons\Misc\InvokableProvider; | ||
|
||
abstract class ControllerRoutesProvider extends InvokableProvider | ||
{ | ||
/** | ||
* @var callable[] | ||
*/ | ||
private $middlewares; | ||
|
||
public function __construct(array $middlewares = []) | ||
{ | ||
$this->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); | ||
} |
79 changes: 79 additions & 0 deletions
79
tests/unit/SAREhub/Microt/App/Controller/ControllerRoutesProviderTest.php
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,79 @@ | ||
<?php | ||
|
||
namespace SAREhub\Microt\App\Controller; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
|
||
class ExampleControllerRoutesProvider extends ControllerRoutesProvider | ||
{ | ||
|
||
public $actionRoute; | ||
|
||
public function __construct(array $middlewares = []) | ||
{ | ||
parent::__construct($middlewares); | ||
$this->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]); | ||
} | ||
} |