22/**
33 * Class QueryTest
44 *
5+ * @link https://github.com/guzzle/psr7/blob/c0dcda9f54d145bd4d062a6d15f54931a67732f9/tests/QueryTest.php
6+ *
57 * @created 27.03.2021
68 * @author smiley <smiley@chillerlan.net>
79 * @copyright 2021 smiley
1214
1315use chillerlan \HTTP \Psr7 \Query ;
1416use PHPUnit \Framework \TestCase ;
17+ use const PHP_QUERY_RFC1738 , PHP_QUERY_RFC3986 ;
1518
1619/**
1720 *
@@ -58,26 +61,11 @@ public function queryParamDataProvider():array{
5861 * @param bool $remove_empty
5962 */
6063 public function testCleanQueryParams (int $ bool_cast , bool $ remove_empty , array $ expected ):void {
61- $ data = ['whatever ' => null , 'nope ' => '' , 'true ' => true , 'false ' => false , 'array ' => ['value ' => false ]];
64+ $ data = ['whatever ' => null , 'nope ' => ' ' , 'true ' => true , 'false ' => false , 'array ' => ['value ' => false ]];
6265
6366 $ this ::assertSame ($ expected , Query::cleanParams ($ data , $ bool_cast , $ remove_empty ));
6467 }
6568
66- public function testBuildHttpQuery ():void {
67-
68- $ data = ['foo ' => 'bar ' , 'whatever? ' => 'nope! ' ];
69-
70- $ this ::assertSame ('' , Query::build ([]));
71- $ this ::assertSame ('foo=bar&whatever%3F=nope%21 ' , Query::build ($ data ));
72- $ this ::assertSame ('foo=bar&whatever?=nope! ' , Query::build ($ data , false ));
73- $ this ::assertSame ('foo=bar, whatever?=nope! ' , Query::build ($ data , false , ', ' ));
74- $ this ::assertSame ('foo="bar", whatever?="nope!" ' , Query::build ($ data , false , ', ' , '" ' ));
75-
76- $ data ['florps ' ] = ['nope ' , 'nope ' , 'nah ' ];
77- $ this ::assertSame ('florps="nah", florps="nope", florps="nope", foo="bar", whatever?="nope!" ' , Query::build ($ data , false , ', ' , '" ' ));
78- }
79-
80-
8169 public function mergeQueryDataProvider ():array {
8270 $ uri = 'http://localhost/whatever/ ' ;
8371 $ params = ['foo ' => 'bar ' ];
@@ -102,5 +90,91 @@ public function testMergeQuery(string $uri, array $params, string $expected):voi
10290 $ this ::assertSame ($ expected , $ merged );
10391 }
10492
93+ public function testBuildQuery ():void {
94+ $ data = ['foo ' => 'bar ' , 'whatever? ' => 'nope! ' ];
95+
96+ $ this ::assertSame ('foo=bar&whatever%3F=nope%21 ' , Query::build ($ data ));
97+ $ this ::assertSame ('foo=bar&whatever?=nope! ' , Query::build ($ data , Query::NO_ENCODING ));
98+ $ this ::assertSame ('foo=bar, whatever?=nope! ' , Query::build ($ data , Query::NO_ENCODING , ', ' ));
99+ $ this ::assertSame ('foo="bar", whatever?="nope!" ' , Query::build ($ data , Query::NO_ENCODING , ', ' , '" ' ));
100+
101+ $ data ['florps ' ] = ['nope ' , 'nope ' , 'nah ' ];
102+ $ this ::assertSame (
103+ 'florps="nah", florps="nope", florps="nope", foo="bar", whatever?="nope!" ' ,
104+ Query::build ($ data , Query::NO_ENCODING , ', ' , '" ' )
105+ );
106+ }
107+
108+ public function testBuildQuerySort ():void {
109+ $ this ::assertSame ('a=2&b=1&b=2&b=3&c=1&d=4 ' , Query::build (['c ' => 1 , 'a ' => 2 , 'b ' => [3 , 1 , 2 ], 'd ' => 4 ]));
110+ }
111+
112+ public function parseQueryProvider ():array {
113+ return [
114+ 'Does not need to parse when the string is empty ' => ['' , []],
115+ 'Can parse mult-values items ' => ['q=a&q=b ' , ['q ' => ['a ' , 'b ' ]]],
116+ 'Can parse multi-valued items that use numeric indices ' => ['q[0]=a&q[1]=b ' , ['q[0] ' => 'a ' , 'q[1] ' => 'b ' ]],
117+ 'Can parse duplicates and does not include numeric indices ' => ['q[]=a&q[]=b ' , ['q[] ' => ['a ' , 'b ' ]]],
118+ 'Ensures that the value of "q" is an array ' => ['q[]=a ' , ['q[] ' => 'a ' ]],
119+ 'Does not modify "." to "_" like parse_str() ' => ['q.a=a&q.b=b ' , ['q.a ' => 'a ' , 'q.b ' => 'b ' ]],
120+ 'Can decode %20 to " " ' => ['q%20a=a%20b ' , ['q a ' => 'a b ' ]],
121+ 'Can parse strings with no values by assigning each to null ' => ['a&q ' , ['a ' => null , 'q ' => null ]],
122+ 'Does not strip trailing equal signs ' => ['data=abc= ' , ['data ' => 'abc= ' ]],
123+ 'Can store duplicates without affecting other values ' => ['?µ=c&foo=a&foo=b ' , ['?µ ' => 'c ' , 'foo ' => ['a ' , 'b ' ]]],
124+ 'Sets value to null when no "=" is present ' => ['foo ' , ['foo ' => null ]],
125+ 'Preserves "0" keys ' => ['0 ' , ['0 ' => null ]],
126+ 'Sets the value to an empty string when "=" is present ' => ['0= ' , ['0 ' => '' ]],
127+ 'Preserves falsey keys 1 ' => ['var=0 ' , ['var ' => '0 ' ]],
128+ 'Preserves falsey keys 2 ' => ['a[b][c]=1&a[b][c]=2 ' , ['a[b][c] ' => ['1 ' , '2 ' ]]],
129+ 'Preserves falsey keys 3 ' => ['a[b]=c&a[d]=e ' , ['a[b] ' => 'c ' , 'a[d] ' => 'e ' ]],
130+ 'Can parse multi-values items ' => ['q=a&q=b&q=c ' , ['q ' => ['a ' , 'b ' , 'c ' ]]],
131+ ];
132+ }
133+
134+ /**
135+ * @dataProvider parseQueryProvider
136+ */
137+ public function testParsesQueries (string $ input , array $ output ):void {
138+ $ this ::assertSame ($ output , Query::parse ($ input ));
139+ }
140+
141+ /**
142+ * @dataProvider parseQueryProvider
143+ */
144+ public function testParsesAndBuildsQueries (string $ input ): void {
145+ $ result = Query::parse ($ input , Query::NO_ENCODING );
146+
147+ $ this ::assertSame ($ input , Query::build ($ result , Query::NO_ENCODING ));
148+ }
149+
150+ public function testDoesNotDecode ():void {
151+ $ this ::assertSame (['foo%20 ' => 'bar ' ], Query::parse ('foo%20=bar ' , Query::NO_ENCODING ));
152+ }
153+
154+ public function testEncodesWithRfc1738 ():void {
155+ $ this ::assertSame ('foo+bar=baz%2B ' , Query::build (['foo bar ' => 'baz+ ' ], PHP_QUERY_RFC1738 ));
156+ }
157+
158+ public function testEncodesWithRfc3986 ():void {
159+ $ this ::assertSame ('foo%20bar=baz%2B ' , Query::build (['foo bar ' => 'baz+ ' ], PHP_QUERY_RFC3986 ));
160+ }
161+
162+ public function testDoesNotEncode ():void {
163+ $ this ::assertSame ('foo bar=baz+ ' , Query::build (['foo bar ' => 'baz+ ' ], Query::NO_ENCODING ));
164+ }
165+
166+ public function testCanControlDecodingType ():void {
167+ $ this ::assertSame ('foo+bar ' , Query::parse ('var=foo+bar ' , PHP_QUERY_RFC3986 )['var ' ]);
168+ $ this ::assertSame ('foo bar ' , Query::parse ('var=foo+bar ' , PHP_QUERY_RFC1738 )['var ' ]);
169+ }
170+
171+ public function testBuildBooleans ():void {
172+ $ this ::assertSame ('false=0&true=1 ' , Query::build (['true ' => true , 'false ' => false ]));
173+
174+ $ this ::assertSame (
175+ 'bar=0&bar=false&foo=1&foo=true ' ,
176+ Query::build (['foo ' => [true , 'true ' ], 'bar ' => [false , 'false ' ]], PHP_QUERY_RFC1738 )
177+ );
178+ }
105179
106180}
0 commit comments