From 6ea79c0be795ba55cf4b6f3abbb282ea3d655f3b Mon Sep 17 00:00:00 2001 From: mikey179 Date: Mon, 9 Mar 2015 11:40:51 +0100 Subject: [PATCH] stubbles\peer\Uri::addParam() now accepts objects with __toString() method --- CHANGELOG.md | 6 ++ src/main/php/peer/QueryString.php | 8 +- src/test/php/peer/QueryStringTest.php | 115 +++++++++++++++----------- 3 files changed, 81 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e22bd59..17ba991 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +5.3.1 (2015-03-09) +------------------ + + * `stubbles\peer\Uri::addParam()` now accepts objects with `__toString()` method + + 5.3.0 (2015-03-05) ------------------ diff --git a/src/main/php/peer/QueryString.php b/src/main/php/peer/QueryString.php index 655f252..8605f1c 100644 --- a/src/main/php/peer/QueryString.php +++ b/src/main/php/peer/QueryString.php @@ -111,7 +111,7 @@ protected function buildQuery($name, $value, $postfix= '') if (is_array($value)) { foreach ($value as $k => $v) { if (is_int($k)) { - $query .= $this->buildQuery(null, $v, $postfix . $name.'[]'); + $query .= $this->buildQuery(null, $v, $postfix . $name .'[]'); } else { $query .= $this->buildQuery(null, $v, $postfix . $name . '[' . $k . ']'); } @@ -150,7 +150,11 @@ public function hasParams() public function addParam($name, $value) { if (!is_array($value) && !is_scalar($value) && null !== $value) { - throw new IllegalArgumentException('Argument 2 passed to ' . __METHOD__ . '() must be an instance of string, array or any other scalar value.'); + if (is_object($value) && method_exists($value, '__toString')) { + $value = (string) $value; + } else { + throw new IllegalArgumentException('Argument 2 passed to ' . __METHOD__ . '() must be an a string, array, object with __toString() method or any other scalar value.'); + } } $this->parameters[$name] = $value; diff --git a/src/test/php/peer/QueryStringTest.php b/src/test/php/peer/QueryStringTest.php index 1a9a6c3..a476f01 100644 --- a/src/test/php/peer/QueryStringTest.php +++ b/src/test/php/peer/QueryStringTest.php @@ -34,7 +34,9 @@ class QueryStringTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->emptyQueryString = new QueryString(); - $this->prefilledQueryString = new QueryString('foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=&set'); + $this->prefilledQueryString = new QueryString( + 'foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=&set' + ); } /** @@ -60,14 +62,17 @@ public function emptyHasNoParametersByDefault() public function prefilledHasParametersFromInitialQueryString() { $this->assertTrue($this->prefilledQueryString->hasParams()); - $this->assertEquals('bar', - $this->prefilledQueryString->param('foo.hm') + $this->assertEquals( + 'bar', + $this->prefilledQueryString->param('foo.hm') ); - $this->assertEquals(['dummy' => 'blubb', 'more'], - $this->prefilledQueryString->param('baz') + $this->assertEquals( + ['dummy' => 'blubb', 'more'], + $this->prefilledQueryString->param('baz') ); - $this->assertEquals('', - $this->prefilledQueryString->param('empty') + $this->assertEquals( + '', + $this->prefilledQueryString->param('empty') ); $this->assertNull($this->prefilledQueryString->param('set')); } @@ -77,9 +82,7 @@ public function prefilledHasParametersFromInitialQueryString() */ public function buildEmptQueryStringReturnsEmptyString() { - $this->assertEquals('', - $this->emptyQueryString->build() - ); + $this->assertEquals('', $this->emptyQueryString->build()); } /** @@ -87,8 +90,9 @@ public function buildEmptQueryStringReturnsEmptyString() */ public function buildNonEmptQueryStringReturnsString() { - $this->assertEquals('foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=&set', - $this->prefilledQueryString->build() + $this->assertEquals( + 'foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=&set', + $this->prefilledQueryString->build() ); } @@ -137,8 +141,9 @@ public function getNonExistingParamReturnsNullByDefault() */ public function getNonExistingParamReturnsDefaultValue() { - $this->assertEquals('example', - $this->emptyQueryString->param('doesNotExist', 'example') + $this->assertEquals( + 'example', + $this->emptyQueryString->param('doesNotExist', 'example') ); } @@ -147,8 +152,9 @@ public function getNonExistingParamReturnsDefaultValue() */ public function getExistingParamReturnsValue() { - $this->assertEquals('bar', - $this->prefilledQueryString->param('foo.hm') + $this->assertEquals( + 'bar', + $this->prefilledQueryString->param('foo.hm') ); } @@ -157,9 +163,9 @@ public function getExistingParamReturnsValue() */ public function removeNonExistingParamDoesNothing() { - $this->assertEquals('foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=&set', - $this->prefilledQueryString->removeParam('doesNotExist') - ->build() + $this->assertEquals( + 'foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=&set', + $this->prefilledQueryString->removeParam('doesNotExist')->build() ); } @@ -168,9 +174,9 @@ public function removeNonExistingParamDoesNothing() */ public function removeExistingEmptyParam() { - $this->assertEquals('foo.hm=bar&baz[dummy]=blubb&baz[]=more&set', - $this->prefilledQueryString->removeParam('empty') - ->build() + $this->assertEquals( + 'foo.hm=bar&baz[dummy]=blubb&baz[]=more&set', + $this->prefilledQueryString->removeParam('empty')->build() ); } @@ -179,9 +185,9 @@ public function removeExistingEmptyParam() */ public function removeExistingNullValueParam() { - $this->assertEquals('foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=', - $this->prefilledQueryString->removeParam('set') - ->build() + $this->assertEquals( + 'foo.hm=bar&baz[dummy]=blubb&baz[]=more&empty=', + $this->prefilledQueryString->removeParam('set')->build() ); } @@ -190,9 +196,9 @@ public function removeExistingNullValueParam() */ public function removeExistingArrayParam() { - $this->assertEquals('foo.hm=bar&empty=&set', - $this->prefilledQueryString->removeParam('baz') - ->build() + $this->assertEquals( + 'foo.hm=bar&empty=&set', + $this->prefilledQueryString->removeParam('baz')->build() ); } @@ -205,14 +211,29 @@ public function addIllegalParamThrowsIllegalArgumentException() $this->emptyQueryString->addParam('some', new \stdClass()); } + /** + * @test + * @since 5.3.1 + */ + public function allowsToAddObjectWithToStringMethodAsParam() + { + $this->assertEquals( + 'some=127.0.0.1', + $this->emptyQueryString->addParam( + 'some', + new IpAddress('127.0.0.1') + )->build() + ); + } + /** * @test */ public function addNullValueAddsParamNameOnly() { - $this->assertEquals('some', - $this->emptyQueryString->addParam('some', null) - ->build() + $this->assertEquals( + 'some', + $this->emptyQueryString->addParam('some', null)->build() ); } @@ -221,9 +242,9 @@ public function addNullValueAddsParamNameOnly() */ public function addEmptyValueAddsParamNameAndEqualsign() { - $this->assertEquals('some=', - $this->emptyQueryString->addParam('some', '') - ->build() + $this->assertEquals( + 'some=', + $this->emptyQueryString->addParam('some', '')->build() ); } @@ -232,9 +253,9 @@ public function addEmptyValueAddsParamNameAndEqualsign() */ public function addValueAddsParamNameWithValue() { - $this->assertEquals('some=bar', - $this->emptyQueryString->addParam('some', 'bar') - ->build() + $this->assertEquals( + 'some=bar', + $this->emptyQueryString->addParam('some', 'bar')->build() ); } @@ -243,9 +264,11 @@ public function addValueAddsParamNameWithValue() */ public function addArrayAddsParam() { - $this->assertEquals('some[foo]=bar&some[]=baz', - $this->emptyQueryString->addParam('some', ['foo' => 'bar', 'baz']) - ->build() + $this->assertEquals( + 'some[foo]=bar&some[]=baz', + $this->emptyQueryString->addParam( + 'some', ['foo' => 'bar', 'baz'] + )->build() ); } @@ -254,9 +277,9 @@ public function addArrayAddsParam() */ public function addFalseValueTranslatesFalseTo0() { - $this->assertEquals('some=0', - $this->emptyQueryString->addParam('some', false) - ->build() + $this->assertEquals( + 'some=0', + $this->emptyQueryString->addParam('some', false)->build() ); } @@ -265,9 +288,9 @@ public function addFalseValueTranslatesFalseTo0() */ public function addTrueValueTranslatesFalseTo1() { - $this->assertEquals('some=1', - $this->emptyQueryString->addParam('some', true) - ->build() + $this->assertEquals( + 'some=1', + $this->emptyQueryString->addParam('some', true)->build() ); }