Skip to content

Commit

Permalink
Route complete callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jun 18, 2019
1 parent a91aaa7 commit c60d030
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/RouteCompleteCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Packaged\Routing;

use Packaged\Context\Context;
use function is_callable;

class RouteCompleteCallback implements RouteCompleter, Condition
{
protected $_callback;
protected $_condition;

public function __construct(callable $callback = null)
{
if($callback !== null)
{
$this->setCallback($callback);
}
}

public static function i(callable $callback = null)
{
return new static($callback);
}

public function match(Context $context): bool
{
if($this->_condition instanceof Condition)
{
return $this->_condition->match($context);
}
return true;
}

public function setCondition(Condition $condition)
{
$this->_condition = $condition;
return $this;
}

public function setCallback(callable $callback)
{
$this->_callback = $callback;
return $this;
}

public function complete(Context $context)
{
$func = $this->_callback;
if(is_callable($func))
{
return $func($context);
}
return null;
}

}
73 changes: 73 additions & 0 deletions tests/RouteCompleteCallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Packaged\Tests\Routing;

use Exception;
use Packaged\Context\Context;
use Packaged\Http\Request;
use Packaged\Http\Response;
use Packaged\Routing\Handler\FuncHandler;
use Packaged\Routing\RequestCondition;
use Packaged\Routing\Route;
use Packaged\Routing\RouteCompleteCallback;
use Packaged\Tests\Routing\Supporting\TestSingleRouteSelector;
use PHPUnit\Framework\TestCase;

class RouteCompleteCallbackTest extends TestCase
{
public function testComplete()
{
$handler = new FuncHandler(
function () {
return Response::create('OK');
}
);

$request = Request::create('http://www.test.com:8080/');
$ctx = new Context($request);

(new TestSingleRouteSelector(
Route::with(
RouteCompleteCallback::i(
function (Context $ctx) {
$ctx->meta()->set('callback', 1);
}
)
)->setHandler($handler)
))->handle($ctx);

$this->assertEquals(1, $ctx->meta()->get('callback'));

try
{
(new TestSingleRouteSelector(
Route::with(
RouteCompleteCallback::i(
function (Context $ctx) {
$ctx->meta()->set('callback', 2);
}
)->setCondition(RequestCondition::i()->domain('invalid'))
)->setHandler($handler)
))->handle($ctx);
}
catch(Exception $e)
{
}
$this->assertEquals(1, $ctx->meta()->get('callback'));

(new TestSingleRouteSelector(
Route::with(
RouteCompleteCallback::i(
function (Context $ctx) {
$ctx->meta()->set('callback', 3);
}
)->setCondition(RequestCondition::i()->domain('test'))
)->setHandler($handler)
))->handle($ctx);

$this->assertEquals(3, $ctx->meta()->get('callback'));

(new TestSingleRouteSelector(Route::with(RouteCompleteCallback::i())->setHandler($handler)))->handle($ctx);
$this->assertEquals(3, $ctx->meta()->get('callback'));
}
}
6 changes: 6 additions & 0 deletions tests/Supporting/TestSingleRouteSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Packaged\Http\Response;
use Packaged\Routing\Handler\FuncHandler;
use Packaged\Routing\Handler\Handler;
use Packaged\Routing\Route;
use Packaged\Routing\RouteSelector;

class TestSingleRouteSelector extends RouteSelector
Expand All @@ -19,6 +20,11 @@ public function __construct($route = null)

protected function _generateRoutes()
{
if($this->_route instanceof Route)
{
yield $this->_route;
return null;
}
return $this->_route;
}

Expand Down

0 comments on commit c60d030

Please sign in to comment.