Skip to content

Commit

Permalink
F/controller routes provider (#22)
Browse files Browse the repository at this point in the history
* added ControllerRoutesProvider
  • Loading branch information
Mararok authored May 23, 2018
1 parent be8bcf2 commit 54460d0
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/SAREhub/Microt/App/Controller/ControllerRoutesProvider.php
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);
}
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]);
}
}

0 comments on commit 54460d0

Please sign in to comment.