2013-09-19 23:15:56 -07:00
|
|
|
<?php
|
|
|
|
|
2014-02-16 21:02:54 -08:00
|
|
|
namespace GuzzleHttp\Tests;
|
2013-09-19 23:15:56 -07:00
|
|
|
|
2014-02-16 21:02:54 -08:00
|
|
|
use GuzzleHttp\Query;
|
2013-09-19 23:15:56 -07:00
|
|
|
|
2014-02-16 21:02:54 -08:00
|
|
|
class QueryTest extends \PHPUnit_Framework_TestCase
|
2013-09-19 23:15:56 -07:00
|
|
|
{
|
|
|
|
public function testCanCastToString()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query(['foo' => 'baz', 'bar' => 'bam boozle']);
|
2013-09-19 23:15:56 -07:00
|
|
|
$this->assertEquals('foo=baz&bar=bam%20boozle', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanDisableUrlEncoding()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query(['bar' => 'bam boozle']);
|
2013-09-19 23:15:56 -07:00
|
|
|
$q->setEncodingType(false);
|
|
|
|
$this->assertEquals('bar=bam boozle', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanSpecifyRfc1783UrlEncodingType()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query(['bar abc' => 'bam boozle']);
|
|
|
|
$q->setEncodingType(Query::RFC1738);
|
2013-09-19 23:15:56 -07:00
|
|
|
$this->assertEquals('bar+abc=bam+boozle', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanSpecifyRfc3986UrlEncodingType()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query(['bar abc' => 'bam boozle', 'ሴ' => 'hi']);
|
|
|
|
$q->setEncodingType(Query::RFC3986);
|
2013-09-19 23:15:56 -07:00
|
|
|
$this->assertEquals('bar%20abc=bam%20boozle&%E1%88%B4=hi', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testValidatesEncodingType()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
(new Query(['bar' => 'bam boozle']))->setEncodingType('foo');
|
2013-09-19 23:15:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAggregatesMultipleValues()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query(['foo' => ['bar', 'baz']]);
|
2013-09-19 23:15:56 -07:00
|
|
|
$this->assertEquals('foo%5B0%5D=bar&foo%5B1%5D=baz', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanSetAggregator()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query(['foo' => ['bar', 'baz']]);
|
2014-02-16 23:20:17 -08:00
|
|
|
$q->setAggregator(function (array $data) {
|
|
|
|
return ['foo' => ['barANDbaz']];
|
|
|
|
});
|
2013-09-19 23:15:56 -07:00
|
|
|
$this->assertEquals('foo=barANDbaz', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsMultipleValuesPerKey()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$q = new Query();
|
2013-09-19 23:15:56 -07:00
|
|
|
$q->add('facet', 'size');
|
|
|
|
$q->add('facet', 'width');
|
|
|
|
$q->add('facet.field', 'foo');
|
|
|
|
// Use the duplicate aggregator
|
2014-02-16 23:20:17 -08:00
|
|
|
$q->setAggregator($q::duplicateAggregator());
|
2013-09-19 23:15:56 -07:00
|
|
|
$this->assertEquals('facet=size&facet=width&facet.field=foo', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAllowsZeroValues()
|
|
|
|
{
|
2014-02-16 21:02:54 -08:00
|
|
|
$query = new Query(array(
|
2013-09-19 23:15:56 -07:00
|
|
|
'foo' => 0,
|
|
|
|
'baz' => '0',
|
|
|
|
'bar' => null,
|
|
|
|
'boo' => false
|
|
|
|
));
|
|
|
|
$this->assertEquals('foo=0&baz=0&bar&boo=', (string) $query);
|
|
|
|
}
|
|
|
|
|
2014-02-16 23:20:17 -08:00
|
|
|
private $encodeData = [
|
|
|
|
't' => [
|
|
|
|
'v1' => ['a', '1'],
|
|
|
|
'v2' => 'b',
|
|
|
|
'v3' => ['v4' => 'c', 'v5' => 'd']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
public function testEncodesDuplicateAggregator()
|
|
|
|
{
|
|
|
|
$agg = Query::duplicateAggregator();
|
|
|
|
$result = $agg($this->encodeData);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
't[v1]' => ['a', '1'],
|
|
|
|
't[v2]' => ['b'],
|
|
|
|
't[v3][v4]' => ['c'],
|
|
|
|
't[v3][v5]' => ['d'],
|
|
|
|
), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDuplicateEncodesNoNumericIndices()
|
|
|
|
{
|
|
|
|
$agg = Query::duplicateAggregator();
|
|
|
|
$result = $agg($this->encodeData);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
't[v1]' => ['a', '1'],
|
|
|
|
't[v2]' => ['b'],
|
|
|
|
't[v3][v4]' => ['c'],
|
|
|
|
't[v3][v5]' => ['d'],
|
|
|
|
), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncodesPhpAggregator()
|
|
|
|
{
|
|
|
|
$agg = Query::phpAggregator();
|
|
|
|
$result = $agg($this->encodeData);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
't[v1][0]' => ['a'],
|
|
|
|
't[v1][1]' => ['1'],
|
|
|
|
't[v2]' => ['b'],
|
|
|
|
't[v3][v4]' => ['c'],
|
|
|
|
't[v3][v5]' => ['d'],
|
|
|
|
), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPhpEncodesNoNumericIndices()
|
|
|
|
{
|
|
|
|
$agg = Query::phpAggregator(false);
|
|
|
|
$result = $agg($this->encodeData);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
't[v1][]' => ['a', '1'],
|
|
|
|
't[v2]' => ['b'],
|
|
|
|
't[v3][v4]' => ['c'],
|
|
|
|
't[v3][v5]' => ['d'],
|
|
|
|
), $result);
|
|
|
|
}
|
2014-07-11 17:25:13 -07:00
|
|
|
|
|
|
|
public function testCanDisableUrlEncodingDecoding()
|
|
|
|
{
|
|
|
|
$q = Query::fromString('foo=bar+baz boo%20', false);
|
|
|
|
$this->assertEquals('bar+baz boo%20', $q['foo']);
|
|
|
|
$this->assertEquals('foo=bar+baz boo%20', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanChangeUrlEncodingDecodingToRfc1738()
|
|
|
|
{
|
|
|
|
$q = Query::fromString('foo=bar+baz', Query::RFC1738);
|
|
|
|
$this->assertEquals('bar baz', $q['foo']);
|
|
|
|
$this->assertEquals('foo=bar+baz', (string) $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanChangeUrlEncodingDecodingToRfc3986()
|
|
|
|
{
|
|
|
|
$q = Query::fromString('foo=bar%20baz', Query::RFC3986);
|
|
|
|
$this->assertEquals('bar baz', $q['foo']);
|
|
|
|
$this->assertEquals('foo=bar%20baz', (string) $q);
|
|
|
|
}
|
2013-09-19 23:15:56 -07:00
|
|
|
}
|