Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
stubbles\peer\Uri::addParam() now accepts objects with __toString() m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
mikey179 committed Mar 9, 2015
1 parent 5467e1b commit 6ea79c0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
8 changes: 6 additions & 2 deletions src/main/php/peer/QueryString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . ']');
}
Expand Down Expand Up @@ -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;
Expand Down
115 changes: 69 additions & 46 deletions src/test/php/peer/QueryStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

/**
Expand All @@ -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'));
}
Expand All @@ -77,18 +82,17 @@ public function prefilledHasParametersFromInitialQueryString()
*/
public function buildEmptQueryStringReturnsEmptyString()
{
$this->assertEquals('',
$this->emptyQueryString->build()
);
$this->assertEquals('', $this->emptyQueryString->build());
}

/**
* @test
*/
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()
);
}

Expand Down Expand Up @@ -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')
);
}

Expand All @@ -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')
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand All @@ -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()
);
}

Expand Down

0 comments on commit 6ea79c0

Please sign in to comment.