From 0326c5abbe1d7b6d69f0bbc93439d223ad737ad3 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Tue, 18 Jun 2019 12:56:15 +0100 Subject: [PATCH] Upgrade to packaged/routing 2.0 --- composer.json | 2 +- src/Routing/Router.php | 4 +- tests/Routing/RequestConditionTest.php | 133 +++++++++++++++++++++ tests/Routing/RequestConstraintTest.php | 133 --------------------- tests/Routing/RequestDataContraintTest.php | 36 +++--- tests/Routing/RouteTest.php | 12 +- 6 files changed, 160 insertions(+), 160 deletions(-) create mode 100644 tests/Routing/RequestConditionTest.php delete mode 100644 tests/Routing/RequestConstraintTest.php diff --git a/composer.json b/composer.json index 16c9888..4be40a8 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "packaged/figlet": "~0.0", "packaged/helpers": "~2.0", "packaged/http": "~1.3", - "packaged/routing": "~1.0", + "packaged/routing": "~2.0", "packaged/ui": "~1.0", "packaged/docblock": "~1.0", "symfony/console": "~4.2", diff --git a/src/Routing/Router.php b/src/Routing/Router.php index fd140d9..3dbf715 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -6,7 +6,7 @@ use Packaged\Routing\ConditionHandler; use Packaged\Routing\Handler\FuncHandler; use Packaged\Routing\Handler\Handler; -use Packaged\Routing\RequestConstraint; +use Packaged\Routing\RequestCondition; use Packaged\Routing\Route; class Router extends RouteProcessor @@ -35,7 +35,7 @@ public function onPathFunc($path, callable $handleFunc): Condition public function onPath($path, Handler $handler): Condition { - $condition = RequestConstraint::i()->path($path); + $condition = RequestCondition::i()->path($path); $this->addCondition(Route::with($condition)->setHandler($handler)); return $condition; } diff --git a/tests/Routing/RequestConditionTest.php b/tests/Routing/RequestConditionTest.php new file mode 100644 index 0000000..daeac59 --- /dev/null +++ b/tests/Routing/RequestConditionTest.php @@ -0,0 +1,133 @@ +assertTrue(RequestCondition::i()->match($ctx)); + + $this->assertTrue(RequestCondition::i()->port(8080)->match($ctx)); + $this->assertFalse(RequestCondition::i()->port(9898)->match($ctx)); + + $this->assertTrue(RequestCondition::i()->scheme('http')->match($ctx)); + $this->assertFalse(RequestCondition::i()->scheme('https')->match($ctx)); + + $this->assertTrue(RequestCondition::i()->method('POST')->match($ctx)); + $this->assertFalse(RequestCondition::i()->method('GET')->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(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(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( + RequestCondition::i()->path('/INV{invoiceNumber@num}', RequestCondition::TYPE_MATCH)->match($ctx) + ); + $this->assertFalse( + RequestCondition::i()->path('/INV{invoiceNumber@num}', RequestCondition::TYPE_EXACT)->match($ctx) + ); + $this->assertTrue( + RequestCondition::i()->path('/INV', RequestCondition::TYPE_START_CASEI) + ->scheme('http', RequestCondition::TYPE_START) + ->match($ctx) + ); + $this->assertTrue( + RequestCondition::i()->path('/INV', RequestCondition::TYPE_START_CASEI) + ->port(9090) + ->match($ctx) + ); + $this->assertFalse( + 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( + RequestCondition::i()->path('/')->domain("tes", RequestCondition::TYPE_START)->match($ctx) + ); + $this->assertFalse( + RequestCondition::i()->path('/page')->domain("tes", RequestCondition::TYPE_START)->match($ctx) + ); + $this->assertFalse( + RequestCondition::i()->path('')->domain("Ates", RequestCondition::TYPE_START)->match($ctx) + ); + $this->assertTrue( + RequestCondition::i()->path('')->domain("tes", RequestCondition::TYPE_START)->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() + { + $request = Request::create( + 'http://www.test.com:8080/one/two/three/4/5/s1x/s3^eN!/all/remain/path/end/finished', + 'POST' + ); + $ctx = new Context($request); + + $constraint = RequestCondition::i()->path( + '/{one}/{two@alpha}/{three}/{four@num}/{five@alphanum}/{six@alphanum}/{seven}/{remain@all}/end' + ); + $this->assertTrue($constraint->match($ctx)); + $constraint->complete($ctx); + + $this->assertEquals("one", $ctx->routeData()->get('one')); + $this->assertEquals("two", $ctx->routeData()->get('two')); + $this->assertEquals("three", $ctx->routeData()->get('three')); + $this->assertEquals("4", $ctx->routeData()->get('four')); + $this->assertEquals("5", $ctx->routeData()->get('five')); + $this->assertEquals("s1x", $ctx->routeData()->get('six')); + $this->assertEquals("s3^eN!", $ctx->routeData()->get('seven')); + $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(RequestCondition::META_ROUTED_PATH) + ); + + $request = Request::create('http://www.test.com:8080/INV123'); + $ctx = new Context($request); + $constraint2 = RequestCondition::i()->path('/INV{invoiceNumber@num}'); + $this->assertTrue($constraint2->match($ctx)); + $constraint2->complete($ctx); + $this->assertEquals(123, $ctx->routeData()->get('invoiceNumber')); + } + + public function testRegexInConstraintVariable() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid regex passed to path #^/{test#test}(?=/|$)#ui'); + + $request = Request::create('http://www.test.com:8080/INV123'); + $ctx = new Context($request); + RequestCondition::i()->path('/{test#test}')->match($ctx); + } +} diff --git a/tests/Routing/RequestConstraintTest.php b/tests/Routing/RequestConstraintTest.php deleted file mode 100644 index 1f0d7b5..0000000 --- a/tests/Routing/RequestConstraintTest.php +++ /dev/null @@ -1,133 +0,0 @@ -assertTrue(RequestConstraint::i()->match($ctx)); - - $this->assertTrue(RequestConstraint::i()->port(8080)->match($ctx)); - $this->assertFalse(RequestConstraint::i()->port(9898)->match($ctx)); - - $this->assertTrue(RequestConstraint::i()->scheme('http')->match($ctx)); - $this->assertFalse(RequestConstraint::i()->scheme('https')->match($ctx)); - - $this->assertTrue(RequestConstraint::i()->method('POST')->match($ctx)); - $this->assertFalse(RequestConstraint::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(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(RequestConstraint::i()->subDomain('www')->domain('test')->tld('com')->match($ctx)); - $this->assertFalse(RequestConstraint::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) - ); - $this->assertFalse( - RequestConstraint::i()->path('/INV{invoiceNumber@num}', RequestConstraint::TYPE_EXACT)->match($ctx) - ); - $this->assertTrue( - RequestConstraint::i()->path('/INV', RequestConstraint::TYPE_START_CASEI) - ->scheme('http', RequestConstraint::TYPE_START) - ->match($ctx) - ); - $this->assertTrue( - RequestConstraint::i()->path('/INV', RequestConstraint::TYPE_START_CASEI) - ->port(9090) - ->match($ctx) - ); - $this->assertFalse( - RequestConstraint::i()->path('/INV', RequestConstraint::TYPE_START_CASEI) - ->port("9090", RequestConstraint::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) - ); - $this->assertFalse( - RequestConstraint::i()->path('/page')->domain("tes", RequestConstraint::TYPE_START)->match($ctx) - ); - $this->assertFalse( - RequestConstraint::i()->path('')->domain("Ates", RequestConstraint::TYPE_START)->match($ctx) - ); - $this->assertTrue( - RequestConstraint::i()->path('')->domain("tes", RequestConstraint::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)); - } - - public function testRouteData() - { - $request = Request::create( - 'http://www.test.com:8080/one/two/three/4/5/s1x/s3^eN!/all/remain/path/end/finished', - 'POST' - ); - $ctx = new Context($request); - - $constraint = RequestConstraint::i()->path( - '/{one}/{two@alpha}/{three}/{four@num}/{five@alphanum}/{six@alphanum}/{seven}/{remain@all}/end' - ); - $this->assertTrue($constraint->match($ctx)); - $constraint->complete($ctx); - - $this->assertEquals("one", $ctx->routeData()->get('one')); - $this->assertEquals("two", $ctx->routeData()->get('two')); - $this->assertEquals("three", $ctx->routeData()->get('three')); - $this->assertEquals("4", $ctx->routeData()->get('four')); - $this->assertEquals("5", $ctx->routeData()->get('five')); - $this->assertEquals("s1x", $ctx->routeData()->get('six')); - $this->assertEquals("s3^eN!", $ctx->routeData()->get('seven')); - $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) - ); - - $request = Request::create('http://www.test.com:8080/INV123'); - $ctx = new Context($request); - $constraint2 = RequestConstraint::i()->path('/INV{invoiceNumber@num}'); - $this->assertTrue($constraint2->match($ctx)); - $constraint2->complete($ctx); - $this->assertEquals(123, $ctx->routeData()->get('invoiceNumber')); - } - - public function testRegexInConstraintVariable() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Invalid regex passed to path #^/{test#test}(?=/|$)#ui'); - - $request = Request::create('http://www.test.com:8080/INV123'); - $ctx = new Context($request); - RequestConstraint::i()->path('/{test#test}')->match($ctx); - } -} diff --git a/tests/Routing/RequestDataContraintTest.php b/tests/Routing/RequestDataContraintTest.php index c73edf2..ffaa56c 100644 --- a/tests/Routing/RequestDataContraintTest.php +++ b/tests/Routing/RequestDataContraintTest.php @@ -3,8 +3,8 @@ namespace Cubex\Tests\Routing; use Packaged\Context\Context; -use Packaged\Routing\RequestDataConstraint; use Packaged\Http\Request; +use Packaged\Routing\RequestDataCondition; use PHPUnit\Framework\TestCase; class RequestDataContraintTest extends 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/Routing/RouteTest.php b/tests/Routing/RouteTest.php index 11409ff..08027b0 100644 --- a/tests/Routing/RouteTest.php +++ b/tests/Routing/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,7 +49,7 @@ 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)); }