Skip to content

Commit

Permalink
Constraint > Condition
Browse files Browse the repository at this point in the history
Or Condition
  • Loading branch information
bajb committed Jun 18, 2019
1 parent 4841319 commit a91aaa7
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 74 deletions.
34 changes: 34 additions & 0 deletions src/OrCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Packaged\Routing;

use Packaged\Context\Context;

class OrCondition implements Condition
{
public static function i()
{
return new static();
}

/** @var Condition[] */
protected $_conditions = [];

public function match(Context $context): bool
{
foreach($this->_conditions as $condition)
{
if($condition->match($context))
{
return true;
}
}
return false;
}

public function add(Condition $condition)
{
$this->_conditions[] = $condition;
return $this;
}

}
8 changes: 4 additions & 4 deletions src/RequestConstraint.php → src/RequestCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function rtrim;
use function strpos;

class RequestConstraint implements Condition, RouteCompleter
class RequestCondition implements Condition, RouteCompleter
{
const SCHEME = 'scheme';
const PORT = 'port';
Expand All @@ -38,7 +38,7 @@ class RequestConstraint implements Condition, RouteCompleter
protected $_routedPath;
protected $_extractedData = [];

protected $_constraints = [];
protected $_conditions = [];

public static function i()
{
Expand All @@ -52,7 +52,7 @@ public function path($path, $type = self::TYPE_MATCH)

protected function _add($on, $with, $type = self::TYPE_MATCH)
{
$this->_constraints[] = [$on, $with, $type];
$this->_conditions[] = [$on, $with, $type];
return $this;
}

Expand Down Expand Up @@ -114,7 +114,7 @@ public function hasQueryValue($key, $value, $matchType = self::TYPE_MATCH)
public function match(Context $context): bool
{
$this->_routedPath = $context->meta()->get(self::META_ROUTED_PATH, '/');
foreach($this->_constraints as [$matchOn, $matchWith, $matchType])
foreach($this->_conditions as [$matchOn, $matchWith, $matchType])
{
if($matchOn == self::PATH)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Packaged\Context\Context;

class RequestDataConstraint implements Condition
class RequestDataCondition implements Condition
{
protected $_values = [];

Expand Down
2 changes: 1 addition & 1 deletion src/RouteSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class RouteSelector implements Handler
*/
protected static function _route($path, $result)
{
return Route::with(RequestConstraint::i()->path($path))->setHandler($result);
return Route::with(RequestCondition::i()->path($path))->setHandler($result);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/ConditionSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Packaged\Context\Context;
use Packaged\Http\Request;
use Packaged\Routing\ConditionSet;
use Packaged\Routing\RequestConstraint;
use Packaged\Routing\RequestCondition;
use PHPUnit\Framework\TestCase;

class ConditionSetTest extends TestCase
Expand All @@ -15,13 +15,13 @@ public function testSet()
{
$ctx = new Context(new Request(['a' => 'bb']));

$condition = ConditionSet::i()->add(RequestConstraint::i()->hasQueryKey('a'));
$condition = ConditionSet::i()->add(RequestCondition::i()->hasQueryKey('a'));
$condition->match($ctx);
$this->assertTrue($condition->match($ctx));

$condition = ConditionSet::with(
RequestConstraint::i()->hasQueryKey('a'),
RequestConstraint::i()->hasQueryValue('a', 'bb')
RequestCondition::i()->hasQueryKey('a'),
RequestCondition::i()->hasQueryValue('a', 'bb')
);
$this->assertTrue($condition->match($ctx));
}
Expand Down
47 changes: 47 additions & 0 deletions tests/OrConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Packaged\Tests\Routing;

use Packaged\Context\Context;
use Packaged\Http\Request;
use Packaged\Routing\OrCondition;
use Packaged\Routing\RequestCondition;
use PHPUnit\Framework\TestCase;

class OrConditionTest extends TestCase
{

public function testMatch()
{
$request = Request::create('http://www.test.com:8080/path/one', 'POST');
$ctx = new Context($request);

$this->assertTrue(
OrCondition::i()
->add(RequestCondition::i()->scheme('https'))
->add(RequestCondition::i()->domain('test'))
->match($ctx)
);

$this->assertTrue(
OrCondition::i()
->add(RequestCondition::i()->domain('test'))
->add(RequestCondition::i()->scheme('https'))
->match($ctx)
);

$this->assertFalse(
OrCondition::i()
->add(RequestCondition::i()->scheme('https'))
->add(RequestCondition::i()->domain('testing'))
->match($ctx)
);

$this->assertTrue(
OrCondition::i()
->add(RequestCondition::i()->scheme('http'))
->add(RequestCondition::i()->rootDomain('test.com'))
->match($ctx)
);
}
}
78 changes: 39 additions & 39 deletions tests/RequestConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Packaged\Tests\Routing;

use Packaged\Context\Context;
use Packaged\Routing\RequestConstraint;
use Packaged\Routing\RequestCondition;
use Packaged\Http\Request;
use PHPUnit\Framework\TestCase;

Expand All @@ -14,76 +14,76 @@ public function testConstraints()
$request = Request::create('http://www.test.com:8080/path/one', 'POST');
$ctx = new Context($request);

$this->assertTrue(RequestConstraint::i()->match($ctx));
$this->assertTrue(RequestCondition::i()->match($ctx));

$this->assertTrue(RequestConstraint::i()->port(8080)->match($ctx));
$this->assertFalse(RequestConstraint::i()->port(9898)->match($ctx));
$this->assertTrue(RequestCondition::i()->port(8080)->match($ctx));
$this->assertFalse(RequestCondition::i()->port(9898)->match($ctx));

$this->assertTrue(RequestConstraint::i()->scheme('http')->match($ctx));
$this->assertFalse(RequestConstraint::i()->scheme('https')->match($ctx));
$this->assertTrue(RequestCondition::i()->scheme('http')->match($ctx));
$this->assertFalse(RequestCondition::i()->scheme('https')->match($ctx));

$this->assertTrue(RequestConstraint::i()->method('POST')->match($ctx));
$this->assertFalse(RequestConstraint::i()->method('GET')->match($ctx));
$this->assertTrue(RequestCondition::i()->method('POST')->match($ctx));
$this->assertFalse(RequestCondition::i()->method('GET')->match($ctx));

$this->assertTrue(RequestConstraint::i()->path('/path/one')->match($ctx));
$this->assertTrue(RequestConstraint::i()->path('/path')->match($ctx));
$this->assertTrue(RequestConstraint::i()->path('/path', RequestConstraint::TYPE_START)->match($ctx));
$this->assertFalse(RequestConstraint::i()->path('/Path', RequestConstraint::TYPE_START)->match($ctx));
$this->assertFalse(RequestConstraint::i()->path('/path', RequestConstraint::TYPE_EXACT)->match($ctx));
$this->assertFalse(RequestConstraint::i()->path('/abc')->match($ctx));
$this->assertTrue(RequestCondition::i()->path('/path/one')->match($ctx));
$this->assertTrue(RequestCondition::i()->path('/path')->match($ctx));
$this->assertTrue(RequestCondition::i()->path('/path', RequestCondition::TYPE_START)->match($ctx));
$this->assertFalse(RequestCondition::i()->path('/Path', RequestCondition::TYPE_START)->match($ctx));
$this->assertFalse(RequestCondition::i()->path('/path', RequestCondition::TYPE_EXACT)->match($ctx));
$this->assertFalse(RequestCondition::i()->path('/abc')->match($ctx));

$this->assertTrue(RequestConstraint::i()->domain('test')->match($ctx));
$this->assertTrue(RequestConstraint::i()->domain('test')->tld('com')->match($ctx));
$this->assertFalse(RequestConstraint::i()->domain('test')->tld('net')->match($ctx));
$this->assertFalse(RequestConstraint::i()->ajax()->match($ctx));
$this->assertTrue(RequestCondition::i()->domain('test')->match($ctx));
$this->assertTrue(RequestCondition::i()->domain('test')->tld('com')->match($ctx));
$this->assertFalse(RequestCondition::i()->domain('test')->tld('net')->match($ctx));
$this->assertFalse(RequestCondition::i()->ajax()->match($ctx));

$this->assertTrue(RequestConstraint::i()->subDomain('www')->domain('test')->tld('com')->match($ctx));
$this->assertFalse(RequestConstraint::i()->subDomain('my')->domain('test')->tld('com')->match($ctx));
$this->assertTrue(RequestCondition::i()->subDomain('www')->domain('test')->tld('com')->match($ctx));
$this->assertFalse(RequestCondition::i()->subDomain('my')->domain('test')->tld('com')->match($ctx));

$request = Request::create('HTTPS://www.tesT.com:9090/InV123');
$request->server->set('HTTPS', 'on');
$ctx = new Context($request);
$this->assertTrue(
RequestConstraint::i()->path('/INV{invoiceNumber@num}', RequestConstraint::TYPE_MATCH)->match($ctx)
RequestCondition::i()->path('/INV{invoiceNumber@num}', RequestCondition::TYPE_MATCH)->match($ctx)
);
$this->assertFalse(
RequestConstraint::i()->path('/INV{invoiceNumber@num}', RequestConstraint::TYPE_EXACT)->match($ctx)
RequestCondition::i()->path('/INV{invoiceNumber@num}', RequestCondition::TYPE_EXACT)->match($ctx)
);
$this->assertTrue(
RequestConstraint::i()->path('/INV', RequestConstraint::TYPE_START_CASEI)
->scheme('http', RequestConstraint::TYPE_START)
RequestCondition::i()->path('/INV', RequestCondition::TYPE_START_CASEI)
->scheme('http', RequestCondition::TYPE_START)
->match($ctx)
);
$this->assertTrue(
RequestConstraint::i()->path('/INV', RequestConstraint::TYPE_START_CASEI)
RequestCondition::i()->path('/INV', RequestCondition::TYPE_START_CASEI)
->port(9090)
->match($ctx)
);
$this->assertFalse(
RequestConstraint::i()->path('/INV', RequestConstraint::TYPE_START_CASEI)
->port("9090", RequestConstraint::TYPE_EXACT)
RequestCondition::i()->path('/INV', RequestCondition::TYPE_START_CASEI)
->port("9090", RequestCondition::TYPE_EXACT)
->match($ctx)
);

$request = Request::create('HTTPS://www.tesT.com:9090/?x=y&abc');
$request->server->set('HTTPS', 'on');
$ctx = new Context($request);
$this->assertTrue(
RequestConstraint::i()->path('/')->domain("tes", RequestConstraint::TYPE_START)->match($ctx)
RequestCondition::i()->path('/')->domain("tes", RequestCondition::TYPE_START)->match($ctx)
);
$this->assertFalse(
RequestConstraint::i()->path('/page')->domain("tes", RequestConstraint::TYPE_START)->match($ctx)
RequestCondition::i()->path('/page')->domain("tes", RequestCondition::TYPE_START)->match($ctx)
);
$this->assertFalse(
RequestConstraint::i()->path('')->domain("Ates", RequestConstraint::TYPE_START)->match($ctx)
RequestCondition::i()->path('')->domain("Ates", RequestCondition::TYPE_START)->match($ctx)
);
$this->assertTrue(
RequestConstraint::i()->path('')->domain("tes", RequestConstraint::TYPE_START)->match($ctx)
RequestCondition::i()->path('')->domain("tes", RequestCondition::TYPE_START)->match($ctx)
);
$this->assertTrue(RequestConstraint::i()->hasQueryKey("abc")->match($ctx));
$this->assertTrue(RequestConstraint::i()->hasQueryValue("x", "y")->match($ctx));
$this->assertTrue(RequestConstraint::i()->hostname("www.test.com")->match($ctx));
$this->assertTrue(RequestConstraint::i()->rootDomain("test.com")->match($ctx));
$this->assertTrue(RequestCondition::i()->hasQueryKey("abc")->match($ctx));
$this->assertTrue(RequestCondition::i()->hasQueryValue("x", "y")->match($ctx));
$this->assertTrue(RequestCondition::i()->hostname("www.test.com")->match($ctx));
$this->assertTrue(RequestCondition::i()->rootDomain("test.com")->match($ctx));
}

public function testRouteData()
Expand All @@ -94,7 +94,7 @@ public function testRouteData()
);
$ctx = new Context($request);

$constraint = RequestConstraint::i()->path(
$constraint = RequestCondition::i()->path(
'/{one}/{two@alpha}/{three}/{four@num}/{five@alphanum}/{six@alphanum}/{seven}/{remain@all}/end'
);
$this->assertTrue($constraint->match($ctx));
Expand All @@ -110,12 +110,12 @@ public function testRouteData()
$this->assertEquals("all/remain/path", $ctx->routeData()->get('remain'));
$this->assertEquals(
'/one/two/three/4/5/s1x/s3^eN!/all/remain/path/end',
$ctx->meta()->get(RequestConstraint::META_ROUTED_PATH)
$ctx->meta()->get(RequestCondition::META_ROUTED_PATH)
);

$request = Request::create('http://www.test.com:8080/INV123');
$ctx = new Context($request);
$constraint2 = RequestConstraint::i()->path('/INV{invoiceNumber@num}');
$constraint2 = RequestCondition::i()->path('/INV{invoiceNumber@num}');
$this->assertTrue($constraint2->match($ctx));
$constraint2->complete($ctx);
$this->assertEquals(123, $ctx->routeData()->get('invoiceNumber'));
Expand All @@ -128,6 +128,6 @@ public function testRegexInConstraintVariable()

$request = Request::create('http://www.test.com:8080/INV123');
$ctx = new Context($request);
RequestConstraint::i()->path('/{test#test}')->match($ctx);
RequestCondition::i()->path('/{test#test}')->match($ctx);
}
}
36 changes: 18 additions & 18 deletions tests/RequestDataContraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Packaged\Tests\Routing;

use Packaged\Context\Context;
use Packaged\Routing\RequestDataConstraint;
use Packaged\Routing\RequestDataCondition;
use Packaged\Http\Request;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,7 +26,7 @@ protected function _makeContext()
public function testInstance()
{
$c = $this->_makeContext();
$i = RequestDataConstraint::i();
$i = RequestDataCondition::i();

$this->assertTrue($i->match($c));
$i->post('post1', 'val1');
Expand All @@ -45,39 +45,39 @@ public function testInstance()

public function testServer()
{
$this->assertTrue(RequestDataConstraint::i()->server('HTTPS')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->server('HTTPS', 'on')->match($this->_makeContext()));
$this->assertFalse(RequestDataConstraint::i()->server('HTTP')->match($this->_makeContext()));
$this->assertFalse(RequestDataConstraint::i()->server('HTTPS', 'OFF')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->server('HTTPS')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->server('HTTPS', 'on')->match($this->_makeContext()));
$this->assertFalse(RequestDataCondition::i()->server('HTTP')->match($this->_makeContext()));
$this->assertFalse(RequestDataCondition::i()->server('HTTPS', 'OFF')->match($this->_makeContext()));
}

public function testCookie()
{
$this->assertFalse(RequestDataConstraint::i()->cookie('cookieN')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->cookie('cookie1')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->cookie('cookie1', 'val1')->match($this->_makeContext()));
$this->assertFalse(RequestDataCondition::i()->cookie('cookieN')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->cookie('cookie1')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->cookie('cookie1', 'val1')->match($this->_makeContext()));
$this->assertTrue(
RequestDataConstraint::i()->cookie('cookie1', 'val1')->cookie('cookie2', 'val2')->match($this->_makeContext())
RequestDataCondition::i()->cookie('cookie1', 'val1')->cookie('cookie2', 'val2')->match($this->_makeContext())
);
}

public function testPost()
{
$this->assertFalse(RequestDataConstraint::i()->post('postN')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->post('post1')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->post('post1', 'val1')->match($this->_makeContext()));
$this->assertFalse(RequestDataCondition::i()->post('postN')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->post('post1')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->post('post1', 'val1')->match($this->_makeContext()));
$this->assertTrue(
RequestDataConstraint::i()->post('post1', 'val1')->post('post2', 'val2')->match($this->_makeContext())
RequestDataCondition::i()->post('post1', 'val1')->post('post2', 'val2')->match($this->_makeContext())
);
}

public function testQuery()
{
$this->assertFalse(RequestDataConstraint::i()->query('queryN')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->query('query1')->match($this->_makeContext()));
$this->assertTrue(RequestDataConstraint::i()->query('query1', 'val1')->match($this->_makeContext()));
$this->assertFalse(RequestDataCondition::i()->query('queryN')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->query('query1')->match($this->_makeContext()));
$this->assertTrue(RequestDataCondition::i()->query('query1', 'val1')->match($this->_makeContext()));
$this->assertTrue(
RequestDataConstraint::i()->query('query1', 'val1')->query('query2', 'val2')->match($this->_makeContext())
RequestDataCondition::i()->query('query1', 'val1')->query('query2', 'val2')->match($this->_makeContext())
);
}
}
Loading

0 comments on commit a91aaa7

Please sign in to comment.