1
by Jacek Nykis
Initial commit |
1 |
<?php
|
2 |
||
3 |
namespace GuzzleHttp\Tests; |
|
4 |
||
5 |
use GuzzleHttp\Query; |
|
6 |
||
7 |
class QueryTest extends \PHPUnit_Framework_TestCase |
|
8 |
{
|
|
9 |
public function testCanCastToString() |
|
10 |
{
|
|
11 |
$q = new Query(['foo' => 'baz', 'bar' => 'bam boozle']); |
|
12 |
$this->assertEquals('foo=baz&bar=bam%20boozle', (string) $q); |
|
13 |
}
|
|
14 |
||
15 |
public function testCanDisableUrlEncoding() |
|
16 |
{
|
|
17 |
$q = new Query(['bar' => 'bam boozle']); |
|
18 |
$q->setEncodingType(false); |
|
19 |
$this->assertEquals('bar=bam boozle', (string) $q); |
|
20 |
}
|
|
21 |
||
22 |
public function testCanSpecifyRfc1783UrlEncodingType() |
|
23 |
{
|
|
24 |
$q = new Query(['bar abc' => 'bam boozle']); |
|
25 |
$q->setEncodingType(Query::RFC1738); |
|
26 |
$this->assertEquals('bar+abc=bam+boozle', (string) $q); |
|
27 |
}
|
|
28 |
||
29 |
public function testCanSpecifyRfc3986UrlEncodingType() |
|
30 |
{
|
|
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); |
|
34 |
}
|
|
35 |
||
36 |
/**
|
|
37 |
* @expectedException \InvalidArgumentException
|
|
38 |
*/
|
|
39 |
public function testValidatesEncodingType() |
|
40 |
{
|
|
41 |
(new Query(['bar' => 'bam boozle']))->setEncodingType('foo'); |
|
42 |
}
|
|
43 |
||
44 |
public function testAggregatesMultipleValues() |
|
45 |
{
|
|
46 |
$q = new Query(['foo' => ['bar', 'baz']]); |
|
47 |
$this->assertEquals('foo%5B0%5D=bar&foo%5B1%5D=baz', (string) $q); |
|
48 |
}
|
|
49 |
||
50 |
public function testCanSetAggregator() |
|
51 |
{
|
|
52 |
$q = new Query(['foo' => ['bar', 'baz']]); |
|
53 |
$q->setAggregator(function (array $data) { |
|
54 |
return ['foo' => ['barANDbaz']]; |
|
55 |
});
|
|
56 |
$this->assertEquals('foo=barANDbaz', (string) $q); |
|
57 |
}
|
|
58 |
||
59 |
public function testAllowsMultipleValuesPerKey() |
|
60 |
{
|
|
61 |
$q = new Query(); |
|
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); |
|
68 |
}
|
|
69 |
||
70 |
public function testAllowsZeroValues() |
|
71 |
{
|
|
72 |
$query = new Query(array( |
|
73 |
'foo' => 0, |
|
74 |
'baz' => '0', |
|
75 |
'bar' => null, |
|
76 |
'boo' => false |
|
77 |
));
|
|
78 |
$this->assertEquals('foo=0&baz=0&bar&boo=', (string) $query); |
|
79 |
}
|
|
80 |
||
81 |
private $encodeData = [ |
|
82 |
't' => [ |
|
83 |
'v1' => ['a', '1'], |
|
84 |
'v2' => 'b', |
|
85 |
'v3' => ['v4' => 'c', 'v5' => 'd'] |
|
86 |
]
|
|
87 |
];
|
|
88 |
||
89 |
public function testEncodesDuplicateAggregator() |
|
90 |
{
|
|
91 |
$agg = Query::duplicateAggregator(); |
|
92 |
$result = $agg($this->encodeData); |
|
93 |
$this->assertEquals(array( |
|
94 |
't[v1]' => ['a', '1'], |
|
95 |
't[v2]' => ['b'], |
|
96 |
't[v3][v4]' => ['c'], |
|
97 |
't[v3][v5]' => ['d'], |
|
98 |
), $result); |
|
99 |
}
|
|
100 |
||
101 |
public function testDuplicateEncodesNoNumericIndices() |
|
102 |
{
|
|
103 |
$agg = Query::duplicateAggregator(); |
|
104 |
$result = $agg($this->encodeData); |
|
105 |
$this->assertEquals(array( |
|
106 |
't[v1]' => ['a', '1'], |
|
107 |
't[v2]' => ['b'], |
|
108 |
't[v3][v4]' => ['c'], |
|
109 |
't[v3][v5]' => ['d'], |
|
110 |
), $result); |
|
111 |
}
|
|
112 |
||
113 |
public function testEncodesPhpAggregator() |
|
114 |
{
|
|
115 |
$agg = Query::phpAggregator(); |
|
116 |
$result = $agg($this->encodeData); |
|
117 |
$this->assertEquals(array( |
|
118 |
't[v1][0]' => ['a'], |
|
119 |
't[v1][1]' => ['1'], |
|
120 |
't[v2]' => ['b'], |
|
121 |
't[v3][v4]' => ['c'], |
|
122 |
't[v3][v5]' => ['d'], |
|
123 |
), $result); |
|
124 |
}
|
|
125 |
||
126 |
public function testPhpEncodesNoNumericIndices() |
|
127 |
{
|
|
128 |
$agg = Query::phpAggregator(false); |
|
129 |
$result = $agg($this->encodeData); |
|
130 |
$this->assertEquals(array( |
|
131 |
't[v1][]' => ['a', '1'], |
|
132 |
't[v2]' => ['b'], |
|
133 |
't[v3][v4]' => ['c'], |
|
134 |
't[v3][v5]' => ['d'], |
|
135 |
), $result); |
|
136 |
}
|
|
137 |
||
138 |
public function testCanDisableUrlEncodingDecoding() |
|
139 |
{
|
|
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); |
|
143 |
}
|
|
144 |
||
145 |
public function testCanChangeUrlEncodingDecodingToRfc1738() |
|
146 |
{
|
|
147 |
$q = Query::fromString('foo=bar+baz', Query::RFC1738); |
|
148 |
$this->assertEquals('bar baz', $q['foo']); |
|
149 |
$this->assertEquals('foo=bar+baz', (string) $q); |
|
150 |
}
|
|
151 |
||
152 |
public function testCanChangeUrlEncodingDecodingToRfc3986() |
|
153 |
{
|
|
154 |
$q = Query::fromString('foo=bar%20baz', Query::RFC3986); |
|
155 |
$this->assertEquals('bar baz', $q['foo']); |
|
156 |
$this->assertEquals('foo=bar%20baz', (string) $q); |
|
157 |
}
|
|
158 |
}
|