From a91aaa73d696bde10241cfa381dad51009925123 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Tue, 18 Jun 2019 12:08:25 +0100 Subject: [PATCH] Constraint > Condition Or Condition --- src/OrCondition.php | 34 ++++++++ ...estConstraint.php => RequestCondition.php} | 8 +- ...onstraint.php => RequestDataCondition.php} | 2 +- src/RouteSelector.php | 2 +- tests/ConditionSetTest.php | 8 +- tests/OrConditionTest.php | 47 +++++++++++ tests/RequestConstraintTest.php | 78 +++++++++---------- tests/RequestDataContraintTest.php | 36 ++++----- tests/RouteTest.php | 14 ++-- 9 files changed, 155 insertions(+), 74 deletions(-) create mode 100644 src/OrCondition.php rename src/{RequestConstraint.php => RequestCondition.php} (97%) rename src/{RequestDataConstraint.php => RequestDataCondition.php} (97%) create mode 100644 tests/OrConditionTest.php diff --git a/src/OrCondition.php b/src/OrCondition.php new file mode 100644 index 0000000..6b3ae47 --- /dev/null +++ b/src/OrCondition.php @@ -0,0 +1,34 @@ +_conditions as $condition) + { + if($condition->match($context)) + { + return true; + } + } + return false; + } + + public function add(Condition $condition) + { + $this->_conditions[] = $condition; + return $this; + } + +} diff --git a/src/RequestConstraint.php b/src/RequestCondition.php similarity index 97% rename from src/RequestConstraint.php rename to src/RequestCondition.php index 888d0cb..35aa50b 100644 --- a/src/RequestConstraint.php +++ b/src/RequestCondition.php @@ -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'; @@ -38,7 +38,7 @@ class RequestConstraint implements Condition, RouteCompleter protected $_routedPath; protected $_extractedData = []; - protected $_constraints = []; + protected $_conditions = []; public static function i() { @@ -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; } @@ -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) { diff --git a/src/RequestDataConstraint.php b/src/RequestDataCondition.php similarity index 97% rename from src/RequestDataConstraint.php rename to src/RequestDataCondition.php index 4f1dd44..8a7a87a 100644 --- a/src/RequestDataConstraint.php +++ b/src/RequestDataCondition.php @@ -3,7 +3,7 @@ use Packaged\Context\Context; -class RequestDataConstraint implements Condition +class RequestDataCondition implements Condition { protected $_values = []; diff --git a/src/RouteSelector.php b/src/RouteSelector.php index c69704a..e9dbf91 100644 --- a/src/RouteSelector.php +++ b/src/RouteSelector.php @@ -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); } /** diff --git a/tests/ConditionSetTest.php b/tests/ConditionSetTest.php index 916dd8c..ca54ea6 100644 --- a/tests/ConditionSetTest.php +++ b/tests/ConditionSetTest.php @@ -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 @@ -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)); } diff --git a/tests/OrConditionTest.php b/tests/OrConditionTest.php new file mode 100644 index 0000000..2a9e588 --- /dev/null +++ b/tests/OrConditionTest.php @@ -0,0 +1,47 @@ +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) + ); + } +} diff --git a/tests/RequestConstraintTest.php b/tests/RequestConstraintTest.php index e3d8df9..e20cea8 100644 --- a/tests/RequestConstraintTest.php +++ b/tests/RequestConstraintTest.php @@ -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; @@ -14,54 +14,54 @@ 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) ); @@ -69,21 +69,21 @@ public function testConstraints() $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() @@ -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)); @@ -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')); @@ -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); } } diff --git a/tests/RequestDataContraintTest.php b/tests/RequestDataContraintTest.php index 7e1080e..3db6d31 100644 --- a/tests/RequestDataContraintTest.php +++ b/tests/RequestDataContraintTest.php @@ -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; @@ -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'); @@ -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()) ); } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 041cc4f..1ed9ee6 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -5,7 +5,7 @@ use Packaged\Context\Context; use Packaged\Http\Request; use Packaged\Routing\Handler\FuncHandler; -use Packaged\Routing\RequestConstraint; +use Packaged\Routing\RequestCondition; use Packaged\Routing\Route; use PHPUnit\Framework\TestCase; @@ -15,7 +15,7 @@ public function testRoutePart() { [$route, $ctx] = $this->_getRoute(); - $route->add(RequestConstraint::i()->path('route')); + $route->add(RequestCondition::i()->path('route')); $this->assertTrue($route->match($ctx)); } @@ -23,7 +23,7 @@ public function testRouteRootRoute() { [$route, $ctx] = $this->_getRoute(); - $route->add(RequestConstraint::i()->path('/route')); + $route->add(RequestCondition::i()->path('/route')); $this->assertTrue($route->match($ctx)); } @@ -31,14 +31,14 @@ public function testRouteRoot() { [$route, $ctx] = $this->_getRoute(); - $route->add(RequestConstraint::i()->path('/')); + $route->add(RequestCondition::i()->path('/')); $this->assertTrue($route->match($ctx)); } public function testRoutePort() { [$route, $ctx] = $this->_getRoute(); - $route->add(RequestConstraint::i()->port('8484')); + $route->add(RequestCondition::i()->port('8484')); $this->assertFalse($route->match($ctx)); } @@ -49,14 +49,14 @@ public function testRouteExtra() $route->setHandler($handler); $ctx = new Context(Request::create('/route_extra')); - $route->add(RequestConstraint::i()->path('/route')); + $route->add(RequestCondition::i()->path('/route')); $this->assertFalse($route->match($ctx)); } public function testComplete() { $ctx = new Context(Request::create('/route_extra')); - $route = Route::i()->add(RequestConstraint::i()->path('{pathname}')); + $route = Route::i()->add(RequestCondition::i()->path('{pathname}')); $route->match($ctx); $route->complete($ctx); $this->assertEquals('route_extra', $ctx->routeData()->get('pathname'));