Skip to content

Commit

Permalink
Func Condition
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Dec 11, 2019
1 parent b0cf46c commit 4f7ea93
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FuncCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Packaged\Routing;

use Packaged\Context\Context;

class FuncCondition implements Condition
{
/** @var callable */
protected $_func;

public function __construct(callable $func)
{
$this->_func = $func;
}

public function match(Context $context): bool
{
return ($this->_func)($context);
}
}
35 changes: 35 additions & 0 deletions tests/FuncConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Packaged\Tests\Routing;

use Packaged\Context\Context;
use Packaged\Http\Request;
use Packaged\Routing\FuncCondition;
use PHPUnit\Framework\TestCase;

class FuncConditionTest extends TestCase
{
protected function _makeContext()
{
$ctx = new Context(
Request::create(
'https://localhost/?query1=val1&query2=val2&query3=val3',
'POST',
['post1' => 'val1', 'post2' => 'val2', 'post3' => 'val3'],
['cookie1' => 'val1', 'cookie2' => 'val2'],
[],
['HTTPS' => 'on']
)
);

$ctx->meta()->set('a', 'b');
return $ctx;
}

public function testInstance()
{
$c = $this->_makeContext();
self::assertTrue((new FuncCondition(function (Context $ctx) { return $ctx->meta()->has('a'); }))->match($c));
self::assertFalse((new FuncCondition(function (Context $ctx) { return $ctx->meta()->has('b'); }))->match($c));
}
}

0 comments on commit 4f7ea93

Please sign in to comment.