Open Journal Systems  3.3.0
QueryStringTest.php
1 <?php
2 
3 namespace Guzzle\Tests\Http;
4 
8 
10 {
12  protected $q;
13 
14  public function setup()
15  {
16  $this->q = new QueryString();
17  }
18 
19  public function testGetFieldSeparator()
20  {
21  $this->assertEquals('&', $this->q->getFieldSeparator());
22  }
23 
24  public function testGetValueSeparator()
25  {
26  $this->assertEquals('=', $this->q->getValueSeparator());
27  }
28 
29  public function testIsUrlEncoding()
30  {
31  $this->assertEquals('RFC 3986', $this->q->getUrlEncoding());
32  $this->assertTrue($this->q->isUrlEncoding());
33  $this->assertEquals('foo%20bar', $this->q->encodeValue('foo bar'));
34 
35  $this->q->useUrlEncoding(QueryString::FORM_URLENCODED);
36  $this->assertTrue($this->q->isUrlEncoding());
37  $this->assertEquals(QueryString::FORM_URLENCODED, $this->q->getUrlEncoding());
38  $this->assertEquals('foo+bar', $this->q->encodeValue('foo bar'));
39 
40  $this->assertSame($this->q, $this->q->useUrlEncoding(false));
41  $this->assertFalse($this->q->isUrlEncoding());
42  $this->assertFalse($this->q->isUrlEncoding());
43  }
44 
45  public function testSetFieldSeparator()
46  {
47  $this->assertEquals($this->q, $this->q->setFieldSeparator('/'));
48  $this->assertEquals('/', $this->q->getFieldSeparator());
49  }
50 
51  public function testSetValueSeparator()
52  {
53  $this->assertEquals($this->q, $this->q->setValueSeparator('/'));
54  $this->assertEquals('/', $this->q->getValueSeparator());
55  }
56 
57  public function testUrlEncode()
58  {
59  $params = array(
60  'test' => 'value',
61  'test 2' => 'this is a test?',
62  'test3' => array('v1', 'v2', 'v3'),
63  'ሴ' => 'bar'
64  );
65  $encoded = array(
66  'test' => 'value',
67  'test%202' => rawurlencode('this is a test?'),
68  'test3%5B0%5D' => 'v1',
69  'test3%5B1%5D' => 'v2',
70  'test3%5B2%5D' => 'v3',
71  '%E1%88%B4' => 'bar'
72  );
73  $this->q->replace($params);
74  $this->assertEquals($encoded, $this->q->urlEncode());
75 
76  // Disable encoding
77  $testData = array('test 2' => 'this is a test');
78  $this->q->replace($testData);
79  $this->q->useUrlEncoding(false);
80  $this->assertEquals($testData, $this->q->urlEncode());
81  }
82 
83  public function testToString()
84  {
85  // Check with no parameters
86  $this->assertEquals('', $this->q->__toString());
87 
88  $params = array(
89  'test' => 'value',
90  'test 2' => 'this is a test?',
91  'test3' => array('v1', 'v2', 'v3'),
92  'test4' => null,
93  );
94  $this->q->replace($params);
95  $this->assertEquals('test=value&test%202=this%20is%20a%20test%3F&test3%5B0%5D=v1&test3%5B1%5D=v2&test3%5B2%5D=v3&test4', $this->q->__toString());
96  $this->q->useUrlEncoding(false);
97  $this->assertEquals('test=value&test 2=this is a test?&test3[0]=v1&test3[1]=v2&test3[2]=v3&test4', $this->q->__toString());
98 
99  // Use an alternative aggregator
100  $this->q->setAggregator(new CommaAggregator());
101  $this->assertEquals('test=value&test 2=this is a test?&test3=v1,v2,v3&test4', $this->q->__toString());
102  }
103 
104  public function testAllowsMultipleValuesPerKey()
105  {
106  $q = new QueryString();
107  $q->add('facet', 'size');
108  $q->add('facet', 'width');
109  $q->add('facet.field', 'foo');
110  // Use the duplicate aggregator
111  $q->setAggregator(new DuplicateAggregator());
112  $this->assertEquals('facet=size&facet=width&facet.field=foo', $q->__toString());
113  }
114 
115  public function testAllowsNestedQueryData()
116  {
117  $this->q->replace(array(
118  'test' => 'value',
119  't' => array(
120  'v1' => 'a',
121  'v2' => 'b',
122  'v3' => array(
123  'v4' => 'c',
124  'v5' => 'd',
125  )
126  )
127  ));
128 
129  $this->q->useUrlEncoding(false);
130  $this->assertEquals('test=value&t[v1]=a&t[v2]=b&t[v3][v4]=c&t[v3][v5]=d', $this->q->__toString());
131  }
132 
133  public function parseQueryProvider()
134  {
135  return array(
136  // Ensure that multiple query string values are allowed per value
137  array('q=a&q=b', array('q' => array('a', 'b'))),
138  // Ensure that PHP array style query string values are parsed
139  array('q[]=a&q[]=b', array('q' => array('a', 'b'))),
140  // Ensure that a single PHP array style query string value is parsed into an array
141  array('q[]=a', array('q' => array('a'))),
142  // Ensure that decimals are allowed in query strings
143  array('q.a=a&q.b=b', array(
144  'q.a' => 'a',
145  'q.b' => 'b'
146  )),
147  // Ensure that query string values are percent decoded
148  array('q%20a=a%20b', array('q a' => 'a b')),
149  // Ensure null values can be added
150  array('q&a', array('q' => false, 'a' => false)),
151  );
152  }
153 
157  public function testParsesQueryStrings($query, $data)
158  {
159  $query = QueryString::fromString($query);
160  $this->assertEquals($data, $query->getAll());
161  }
162 
164  {
165  $query = QueryString::fromString('foo=a&foo=b&?µ=c');
166  $this->assertEquals(array('a', 'b'), $query->get('foo'));
167  $this->assertEquals('c', $query->get('?µ'));
168  }
169 
170  public function testAllowsBlankQueryStringValues()
171  {
172  $query = QueryString::fromString('foo');
173  $this->assertEquals('foo', (string) $query);
174  $query->set('foo', QueryString::BLANK);
175  $this->assertEquals('foo', (string) $query);
176  }
177 
178  public function testAllowsFalsyQueryStringValues()
179  {
180  $query = QueryString::fromString('0');
181  $this->assertEquals('0', (string) $query);
182  $query->set('0', QueryString::BLANK);
183  $this->assertSame('0', (string) $query);
184  }
185 
186  public function testFromStringIgnoresQuestionMark()
187  {
188  $query = QueryString::fromString('foo=baz&bar=boo');
189  $this->assertEquals('foo=baz&bar=boo', (string) $query);
190  }
191 
192  public function testConvertsPlusSymbolsToSpaces()
193  {
194  $query = QueryString::fromString('var=foo+bar');
195  $this->assertEquals('foo bar', $query->get('var'));
196  }
197 
198  public function testFromStringDoesntMangleZeroes()
199  {
200  $query = QueryString::fromString('var=0');
201  $this->assertSame('0', $query->get('var'));
202  }
203 
204  public function testAllowsZeroValues()
205  {
206  $query = new QueryString(array(
207  'foo' => 0,
208  'baz' => '0',
209  'bar' => null,
210  'boo' => false,
211  'bam' => ''
212  ));
213  $this->assertEquals('foo=0&baz=0&bar&boo&bam=', (string) $query);
214  }
215 
217  {
218  $query = QueryString::fromString('data=mF0b3IiLCJUZWFtIERldiJdfX0=');
219  $this->assertEquals('mF0b3IiLCJUZWFtIERldiJdfX0=', $query->get('data'));
220  }
221 
223  {
224  $query = QueryString::fromString('test=a&test=b');
225  $this->assertEquals('test=a&test=b', (string) $query);
226  }
227 
229  {
230  $query = QueryString::fromString('test[]=a&test[]=b');
231  $this->assertEquals('test%5B0%5D=a&test%5B1%5D=b', (string) $query);
232  }
233 }
Guzzle\Tests\Http\QueryStringTest\$q
$q
Definition: QueryStringTest.php:15
Guzzle\Tests\Http\QueryStringTest\testConvertsPlusSymbolsToSpaces
testConvertsPlusSymbolsToSpaces()
Definition: QueryStringTest.php:195
Guzzle\Tests\Http\QueryStringTest\testGuessesIfDuplicateAggregatorShouldBeUsedAndChecksForPhpStyle
testGuessesIfDuplicateAggregatorShouldBeUsedAndChecksForPhpStyle()
Definition: QueryStringTest.php:231
Guzzle\Tests\Http\QueryStringTest\testProperlyDealsWithDuplicateQueryStringValues
testProperlyDealsWithDuplicateQueryStringValues()
Definition: QueryStringTest.php:166
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Http\QueryStringTest\parseQueryProvider
parseQueryProvider()
Definition: QueryStringTest.php:136
Guzzle\Http\QueryString
Definition: QueryString.php:14
Guzzle\Tests\Http\QueryStringTest\testIsUrlEncoding
testIsUrlEncoding()
Definition: QueryStringTest.php:32
Guzzle\Tests\Http\QueryStringTest\testAllowsZeroValues
testAllowsZeroValues()
Definition: QueryStringTest.php:207
Guzzle\Tests\Http\QueryStringTest\testFromStringDoesntStripTrailingEquals
testFromStringDoesntStripTrailingEquals()
Definition: QueryStringTest.php:219
Guzzle\Http\QueryString\BLANK
const BLANK
Definition: QueryString.php:24
Guzzle\Tests\Http\QueryStringTest\testAllowsNestedQueryData
testAllowsNestedQueryData()
Definition: QueryStringTest.php:118
Guzzle\Tests\Http\QueryStringTest
Definition: QueryStringTest.php:9
Guzzle\Http\QueryString\fromString
static fromString($query)
Definition: QueryString.php:59
Guzzle\Tests\Http\QueryStringTest\testGetValueSeparator
testGetValueSeparator()
Definition: QueryStringTest.php:27
Guzzle\Http\QueryString\FORM_URLENCODED
const FORM_URLENCODED
Definition: QueryString.php:21
Guzzle\Tests\Http\QueryStringTest\setup
setup()
Definition: QueryStringTest.php:17
Guzzle\Tests\Http\QueryStringTest\testUrlEncode
testUrlEncode()
Definition: QueryStringTest.php:60
Guzzle\Http\QueryAggregator\DuplicateAggregator
Definition: DuplicateAggregator.php:12
Guzzle\Tests\Http\QueryStringTest\testGetFieldSeparator
testGetFieldSeparator()
Definition: QueryStringTest.php:22
Guzzle\Tests\Http\QueryStringTest\testGuessesIfDuplicateAggregatorShouldBeUsed
testGuessesIfDuplicateAggregatorShouldBeUsed()
Definition: QueryStringTest.php:225
Guzzle\Tests\Http\QueryStringTest\testAllowsFalsyQueryStringValues
testAllowsFalsyQueryStringValues()
Definition: QueryStringTest.php:181
Guzzle\Tests\Http\QueryStringTest\testSetValueSeparator
testSetValueSeparator()
Definition: QueryStringTest.php:54
Guzzle\Tests\Http\QueryStringTest\testAllowsMultipleValuesPerKey
testAllowsMultipleValuesPerKey()
Definition: QueryStringTest.php:107
Guzzle\Tests\Http\QueryStringTest\testFromStringIgnoresQuestionMark
testFromStringIgnoresQuestionMark()
Definition: QueryStringTest.php:189
Guzzle\Tests\Http\QueryStringTest\testParsesQueryStrings
testParsesQueryStrings($query, $data)
Definition: QueryStringTest.php:160
Guzzle\Tests\Http\QueryStringTest\testAllowsBlankQueryStringValues
testAllowsBlankQueryStringValues()
Definition: QueryStringTest.php:173
Guzzle\Tests\Http\QueryStringTest\testToString
testToString()
Definition: QueryStringTest.php:86
Guzzle\Http\QueryAggregator\CommaAggregator
Definition: CommaAggregator.php:10
Guzzle\Tests\Http\QueryStringTest\testSetFieldSeparator
testSetFieldSeparator()
Definition: QueryStringTest.php:48
Guzzle\Tests\Http\QueryStringTest\testFromStringDoesntMangleZeroes
testFromStringDoesntMangleZeroes()
Definition: QueryStringTest.php:201
Guzzle\Tests\Http
Definition: AbstractEntityBodyDecoratorTest.php:3