scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests;
4  
5 use GuzzleHttp\Query;
6 use GuzzleHttp\QueryParser;
7  
8 class QueryParserTest extends \PHPUnit_Framework_TestCase
9 {
10 public function parseQueryProvider()
11 {
12 return [
13 // Does not need to parse when the string is empty
14 ['', []],
15 // Can parse mult-values items
16 ['q=a&q=b', ['q' => ['a', 'b']]],
17 // Can parse multi-valued items that use numeric indices
18 ['q[0]=a&q[1]=b', ['q' => ['a', 'b']]],
19 // Can parse duplicates and does not include numeric indices
20 ['q[]=a&q[]=b', ['q' => ['a', 'b']]],
21 // Ensures that the value of "q" is an array even though one value
22 ['q[]=a', ['q' => ['a']]],
23 // Does not modify "." to "_" like PHP's parse_str()
24 ['q.a=a&q.b=b', ['q.a' => 'a', 'q.b' => 'b']],
25 // Can decode %20 to " "
26 ['q%20a=a%20b', ['q a' => 'a b']],
27 // Can parse funky strings with no values by assigning each to null
28 ['q&a', ['q' => null, 'a' => null]],
29 // Does not strip trailing equal signs
30 ['data=abc=', ['data' => 'abc=']],
31 // Can store duplicates without affecting other values
32 ['foo=a&foo=b&?µ=c', ['foo' => ['a', 'b'], '?µ' => 'c']],
33 // Sets value to null when no "=" is present
34 ['foo', ['foo' => null]],
35 // Preserves "0" keys.
36 ['0', ['0' => null]],
37 // Sets the value to an empty string when "=" is present
38 ['0=', ['0' => '']],
39 // Preserves falsey keys
40 ['var=0', ['var' => '0']],
41 // Can deeply nest and store duplicate PHP values
42 ['a[b][c]=1&a[b][c]=2', [
43 'a' => ['b' => ['c' => ['1', '2']]]
44 ]],
45 // Can parse PHP style arrays
46 ['a[b]=c&a[d]=e', ['a' => ['b' => 'c', 'd' => 'e']]],
47 // Ensure it doesn't leave things behind with repeated values
48 // Can parse mult-values items
49 ['q=a&q=b&q=c', ['q' => ['a', 'b', 'c']]],
50 ];
51 }
52  
53 /**
54 * @dataProvider parseQueryProvider
55 */
56 public function testParsesQueries($input, $output)
57 {
58 $query = Query::fromString($input);
59 $this->assertEquals($output, $query->toArray());
60 // Normalize the input and output
61 $query->setEncodingType(false);
62 $this->assertEquals(rawurldecode($input), (string) $query);
63 }
64  
65 public function testConvertsPlusSymbolsToSpacesByDefault()
66 {
67 $query = Query::fromString('var=foo+bar', true);
68 $this->assertEquals('foo bar', $query->get('var'));
69 }
70  
71 public function testCanControlDecodingType()
72 {
73 $qp = new QueryParser();
74 $q = new Query();
75 $qp->parseInto($q, 'var=foo+bar', Query::RFC3986);
76 $this->assertEquals('foo+bar', $q->get('var'));
77 $qp->parseInto($q, 'var=foo+bar', Query::RFC1738);
78 $this->assertEquals('foo bar', $q->get('var'));
79 }
80 }