Skip to content

Commit

Permalink
+ Added tests for single part sets
Browse files Browse the repository at this point in the history
+ Added tests for query params sets
+ Added single query param (un)setting
- Fixed query parts assignment
  • Loading branch information
AnrDaemon committed Nov 19, 2019
1 parent 267026e commit 78602b1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 17 deletions.
8 changes: 5 additions & 3 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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

------------------------------------------------------------------------
14 changes: 10 additions & 4 deletions src/Net/Url.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/** URL handling class.
*
* @version SVN: $Id: Url.php 1035 2019-10-21 10:48:04Z anrdaemon $
* @version SVN: $Id: Url.php 1040 2019-11-19 23:23:52Z anrdaemon $
*/

namespace AnrDaemon\Net;
Expand Down Expand Up @@ -322,13 +322,19 @@ function(&$part, $key)
*
* @see \AnrDaemon\Net\Url::setParts for more information.
*
* @param array $params A set of name-value pairs to replace.
* @param array|string $params A set of name-value pairs to replace OR a name of the query parameter to (un)set.
* @param ?mixed $value A value of query parameter to replace, if first argument is a parameter name.
* @return \AnrDaemon\Net\Url A new class instance with corresponding query manes replaced.
*/
public function setQueryParams(array $params)
public function setQueryParams($params, $value = null)
{
if(!is_array($params))
{
$params = [$params => $value];
}

/** Compose new query block */
$query = $params + $this->params["query"];
$query = $params + (array)$this->params["query"];

return $this->setParts("query", $query);
}
Expand Down
2 changes: 1 addition & 1 deletion src/classloader.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/** Universal stackable classloader.
*
* @version SVN: $Id: classloader.php 1030 2019-09-02 07:00:58Z anrdaemon $
* @version SVN: $Id: classloader.php 1040 2019-11-19 23:23:52Z anrdaemon $
*/

namespace AnrDaemon;
Expand Down
90 changes: 81 additions & 9 deletions test/Net/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,27 @@ public function overridePartsProvider()
foreach($parts as $name => $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",
Expand All @@ -305,27 +310,27 @@ public function overridePartsProvider()
];

$data["with unset part"] = [
[
[[
"scheme" => "http",
"host" => "localhost",
"path" => null,
],
]],
$this->_normalized([
"scheme" => "http",
"host" => "localhost",
]),
];

$data["with unset query param"] = [
[
[[
"scheme" => "http",
"host" => "localhost",
"path" => "/",
"query" => [
"x" => null,
"y" => "",
],
],
]],
$this->_normalized([
"scheme" => "http",
"host" => "localhost",
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 78602b1

Please sign in to comment.