From 78602b122e37de274dab9cbe3fcd0a9653ac87bd Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Wed, 20 Nov 2019 02:27:24 +0300 Subject: [PATCH] + Added tests for single part sets + Added tests for query params sets + Added single query param (un)setting - Fixed query parts assignment --- CHANGES.txt | 8 ++-- src/Net/Url.php | 14 +++++-- src/classloader.php | 2 +- test/Net/UrlTest.php | 90 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 97 insertions(+), 17 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index bae0c54..e9199cd 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,7 +1,9 @@ ------------------------------------------------------------------------ -r1035 | anrdaemon | 2019-10-21 13:48:04 +0300 (Mon, 21 Oct 2019) | 3 lines +r1038 | anrdaemon | 2019-11-20 02:14:53 +0300 (Wed, 20 Nov 2019) | 4 lines -+ Added Url::setQueryParams() method as a wrapper around Url::setParts(). -* Made unsetting query params possible. ++ Added tests for single part sets ++ Added tests for query params sets ++ Added single query param (un)setting +- Fixed query parts assignment ------------------------------------------------------------------------ diff --git a/src/Net/Url.php b/src/Net/Url.php index faad3a5..2183846 100644 --- a/src/Net/Url.php +++ b/src/Net/Url.php @@ -1,7 +1,7 @@ $value]; + } + /** Compose new query block */ - $query = $params + $this->params["query"]; + $query = $params + (array)$this->params["query"]; return $this->setParts("query", $query); } diff --git a/src/classloader.php b/src/classloader.php index d06b828..80cac2b 100644 --- a/src/classloader.php +++ b/src/classloader.php @@ -1,7 +1,7 @@ $value) { $data[$name] = [ - [$name => $value], + [[$name => $value]], + $this->_normalized([$name => $value]), + ]; + + $data["$name(single set)"] = [ + [$name, $value], $this->_normalized([$name => $value]), ]; } $data["query(as string)"] = [ - ["query" => "query=string"], + [["query" => "query=string"]], $this->_normalized(["query" => ["query" => "string"]]), ]; $data["in a batch"] = [ - [ + [[ "scheme" => "http", "host" => "localhost", "path" => "/", - ], + ]], $this->_normalized([ "scheme" => "http", "host" => "localhost", @@ -305,11 +310,11 @@ public function overridePartsProvider() ]; $data["with unset part"] = [ - [ + [[ "scheme" => "http", "host" => "localhost", "path" => null, - ], + ]], $this->_normalized([ "scheme" => "http", "host" => "localhost", @@ -317,7 +322,7 @@ public function overridePartsProvider() ]; $data["with unset query param"] = [ - [ + [[ "scheme" => "http", "host" => "localhost", "path" => "/", @@ -325,7 +330,7 @@ public function overridePartsProvider() "x" => null, "y" => "", ], - ], + ]], $this->_normalized([ "scheme" => "http", "host" => "localhost", @@ -339,6 +344,62 @@ public function overridePartsProvider() return $data; } + public function overrideQueryElementsProvider() + { + $parts = [ + "host" => "localhost", + "path" => "/", + "query" => ["x" => "y"], + ]; + + $data["(simple set)"] = [ + [["a" => "b"]], + $this->_normalized(["query" => ["a" => "b", "x" => "y"]] + $parts), + ]; + + $data["(parameter unset)"] = [ + [["x" => null]], + $this->_normalized(["query" => null] + $parts), + ]; + + $data["(parameter replace)"] = [ + [["x" => "z"]], + $this->_normalized(["query" => ["x" => "z"]] + $parts), + ]; + + $data["(batch operaton)"] = [ + [["a" => "b", "x" => null]], + $this->_normalized(["query" => ["a" => "b"]] + $parts), + ]; + + $data["(empty call)"] = [ + [[]], + $this->_normalized($parts), + ]; + + $data["(single set)"] = [ + ["a", "b"], + $this->_normalized(["query" => ["a" => "b", "x" => "y"]] + $parts), + ]; + + $data["(single replace)"] = [ + ["x", "z"], + $this->_normalized(["query" => ["x" => "z"]] + $parts), + ]; + + $data["(single unset)"] = [ + ["x", null], + $this->_normalized(["query" => null] + $parts), + ]; + + $data["(single empty call)"] = [ + [null], + $this->_normalized($parts), + ]; + + return $data; + } + /** Provide a list of well-known schemes and ports */ public function standardSchemesProvider() @@ -497,7 +558,18 @@ public function testParseQueryString($url, $parts) */ public function testSetUrlPart($overrides, $parts) { - $this->assertEquals($parts, static::$url->setParts($overrides)); + $this->assertEquals($parts, static::$url->setParts(...$overrides)); + } + + /** Test setting URL query parts + * + * @dataProvider overrideQueryElementsProvider + * @depends testCreateEntityFromUri + */ + public function testSetQueryElements($overrides, $parts) + { + $url = static::$url->parse("//localhost/?x=y"); + $this->assertEquals($parts, $url->setQueryParams(...$overrides)); } /** Test scheme-port normalization for well-known protocols