Skip to content

Commit ec7c7a7

Browse files
committed
Add more tests for http_build_query()
Some with unusual types like resource and null A lot more tests for objects
1 parent c177ea9 commit ec7c7a7

9 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
http_build_query() function with object
3+
--FILE--
4+
<?php
5+
class KeyVal {
6+
public $public = "input";
7+
protected $protected = "hello";
8+
private $private = "world";
9+
}
10+
11+
$o = new KeyVal();
12+
13+
// Percent encoded "public=input"
14+
var_dump(http_build_query($o));
15+
?>
16+
--EXPECT--
17+
string(12) "public=input"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
http_build_query() function with empty object
3+
--FILE--
4+
<?php
5+
class EmptyObj {}
6+
$o = new EmptyObj();
7+
8+
var_dump(http_build_query($o));
9+
?>
10+
--EXPECT--
11+
string(0) ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
http_build_query() function with object that is just stringable (GH-10229)
3+
--FILE--
4+
<?php
5+
class StringableObject {
6+
public function __toString() : string {
7+
return "Stringable";
8+
}
9+
}
10+
11+
$o = new StringableObject();
12+
13+
var_dump(http_build_query(['hello', $o]));
14+
var_dump(http_build_query($o));
15+
var_dump(http_build_query(['hello', $o], numeric_prefix: 'prefix_'));
16+
var_dump(http_build_query($o, numeric_prefix: 'prefix_'));
17+
?>
18+
--EXPECT--
19+
string(7) "0=hello"
20+
string(0) ""
21+
string(14) "prefix_0=hello"
22+
string(0) ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
http_build_query() function with recursif object
3+
--FILE--
4+
<?php
5+
class KeyValStringable {
6+
public $public = "input";
7+
protected $protected = "hello";
8+
private $private = "world";
9+
10+
public function __toString(): string {
11+
return "Stringable";
12+
}
13+
}
14+
15+
$o = new KeyValStringable();
16+
17+
var_dump(http_build_query($o));
18+
?>
19+
--EXPECT--
20+
string(12) "public=input"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
http_build_query() function with nested object
3+
--FILE--
4+
<?php
5+
class KeyVal {
6+
public $public = "input";
7+
protected $protected = "hello";
8+
private $private = "world";
9+
}
10+
11+
$o = new KeyVal();
12+
$nested = new KeyVal();
13+
14+
$o->public = $nested;
15+
16+
// Percent encoded "public[public]=input"
17+
var_dump(http_build_query($o));
18+
?>
19+
--EXPECT--
20+
string(24) "public%5Bpublic%5D=input"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
http_build_query() function with recursif object
3+
--FILE--
4+
<?php
5+
class KeyVal {
6+
public $public = "input";
7+
protected $protected = "hello";
8+
private $private = "world";
9+
}
10+
11+
$o = new KeyVal();
12+
$o->public = $o;
13+
14+
var_dump(http_build_query($o));
15+
?>
16+
--EXPECT--
17+
string(0) ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
http_build_query() function with null in array
3+
--FILE--
4+
<?php
5+
var_dump(http_build_query([null]));
6+
?>
7+
--EXPECT--
8+
string(0) ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
http_build_query() function with reference in array
3+
--FILE--
4+
<?php
5+
$v = 'value';
6+
$ref = &$v;
7+
8+
var_dump(http_build_query([$ref]));
9+
?>
10+
--EXPECT--
11+
string(7) "0=value"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
http_build_query() function with resource in array
3+
--FILE--
4+
<?php
5+
var_dump(http_build_query([STDIN]));
6+
?>
7+
--EXPECT--
8+
string(0) ""

0 commit comments

Comments
 (0)