3
namespace GuzzleHttp\Tests;
7
class QueryTest extends \PHPUnit_Framework_TestCase
9
public function testCanCastToString()
11
$q = new Query(['foo' => 'baz', 'bar' => 'bam boozle']);
12
$this->assertEquals('foo=baz&bar=bam%20boozle', (string) $q);
15
public function testCanDisableUrlEncoding()
17
$q = new Query(['bar' => 'bam boozle']);
18
$q->setEncodingType(false);
19
$this->assertEquals('bar=bam boozle', (string) $q);
22
public function testCanSpecifyRfc1783UrlEncodingType()
24
$q = new Query(['bar abc' => 'bam boozle']);
25
$q->setEncodingType(Query::RFC1738);
26
$this->assertEquals('bar+abc=bam+boozle', (string) $q);
29
public function testCanSpecifyRfc3986UrlEncodingType()
31
$q = new Query(['bar abc' => 'bam boozle', 'ሴ' => 'hi']);
32
$q->setEncodingType(Query::RFC3986);
33
$this->assertEquals('bar%20abc=bam%20boozle&%E1%88%B4=hi', (string) $q);
37
* @expectedException \InvalidArgumentException
39
public function testValidatesEncodingType()
41
(new Query(['bar' => 'bam boozle']))->setEncodingType('foo');
44
public function testAggregatesMultipleValues()
46
$q = new Query(['foo' => ['bar', 'baz']]);
47
$this->assertEquals('foo%5B0%5D=bar&foo%5B1%5D=baz', (string) $q);
50
public function testCanSetAggregator()
52
$q = new Query(['foo' => ['bar', 'baz']]);
53
$q->setAggregator(function (array $data) {
54
return ['foo' => ['barANDbaz']];
56
$this->assertEquals('foo=barANDbaz', (string) $q);
59
public function testAllowsMultipleValuesPerKey()
62
$q->add('facet', 'size');
63
$q->add('facet', 'width');
64
$q->add('facet.field', 'foo');
65
// Use the duplicate aggregator
66
$q->setAggregator($q::duplicateAggregator());
67
$this->assertEquals('facet=size&facet=width&facet.field=foo', (string) $q);
70
public function testAllowsZeroValues()
72
$query = new Query(array(
78
$this->assertEquals('foo=0&baz=0&bar&boo=', (string) $query);
81
private $encodeData = [
85
'v3' => ['v4' => 'c', 'v5' => 'd']
89
public function testEncodesDuplicateAggregator()
91
$agg = Query::duplicateAggregator();
92
$result = $agg($this->encodeData);
93
$this->assertEquals(array(
94
't[v1]' => ['a', '1'],
101
public function testDuplicateEncodesNoNumericIndices()
103
$agg = Query::duplicateAggregator();
104
$result = $agg($this->encodeData);
105
$this->assertEquals(array(
106
't[v1]' => ['a', '1'],
108
't[v3][v4]' => ['c'],
109
't[v3][v5]' => ['d'],
113
public function testEncodesPhpAggregator()
115
$agg = Query::phpAggregator();
116
$result = $agg($this->encodeData);
117
$this->assertEquals(array(
121
't[v3][v4]' => ['c'],
122
't[v3][v5]' => ['d'],
126
public function testPhpEncodesNoNumericIndices()
128
$agg = Query::phpAggregator(false);
129
$result = $agg($this->encodeData);
130
$this->assertEquals(array(
131
't[v1][]' => ['a', '1'],
133
't[v3][v4]' => ['c'],
134
't[v3][v5]' => ['d'],
138
public function testCanDisableUrlEncodingDecoding()
140
$q = Query::fromString('foo=bar+baz boo%20', false);
141
$this->assertEquals('bar+baz boo%20', $q['foo']);
142
$this->assertEquals('foo=bar+baz boo%20', (string) $q);
145
public function testCanChangeUrlEncodingDecodingToRfc1738()
147
$q = Query::fromString('foo=bar+baz', Query::RFC1738);
148
$this->assertEquals('bar baz', $q['foo']);
149
$this->assertEquals('foo=bar+baz', (string) $q);
152
public function testCanChangeUrlEncodingDecodingToRfc3986()
154
$q = Query::fromString('foo=bar%20baz', Query::RFC3986);
155
$this->assertEquals('bar baz', $q['foo']);
156
$this->assertEquals('foo=bar%20baz', (string) $q);