Add more tests for http_build_query()

Some with unusual types like resource and null
A lot more tests for objects
This commit is contained in:
George Peter Banyard 2023-01-05 14:09:25 +00:00
parent c177ea91d4
commit ec7c7a7550
No known key found for this signature in database
GPG key ID: 3306078E3194AEBD
9 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,17 @@
--TEST--
http_build_query() function with object
--FILE--
<?php
class KeyVal {
public $public = "input";
protected $protected = "hello";
private $private = "world";
}
$o = new KeyVal();
// Percent encoded "public=input"
var_dump(http_build_query($o));
?>
--EXPECT--
string(12) "public=input"

View file

@ -0,0 +1,11 @@
--TEST--
http_build_query() function with empty object
--FILE--
<?php
class EmptyObj {}
$o = new EmptyObj();
var_dump(http_build_query($o));
?>
--EXPECT--
string(0) ""

View file

@ -0,0 +1,22 @@
--TEST--
http_build_query() function with object that is just stringable (GH-10229)
--FILE--
<?php
class StringableObject {
public function __toString() : string {
return "Stringable";
}
}
$o = new StringableObject();
var_dump(http_build_query(['hello', $o]));
var_dump(http_build_query($o));
var_dump(http_build_query(['hello', $o], numeric_prefix: 'prefix_'));
var_dump(http_build_query($o, numeric_prefix: 'prefix_'));
?>
--EXPECT--
string(7) "0=hello"
string(0) ""
string(14) "prefix_0=hello"
string(0) ""

View file

@ -0,0 +1,20 @@
--TEST--
http_build_query() function with recursif object
--FILE--
<?php
class KeyValStringable {
public $public = "input";
protected $protected = "hello";
private $private = "world";
public function __toString(): string {
return "Stringable";
}
}
$o = new KeyValStringable();
var_dump(http_build_query($o));
?>
--EXPECT--
string(12) "public=input"

View file

@ -0,0 +1,20 @@
--TEST--
http_build_query() function with nested object
--FILE--
<?php
class KeyVal {
public $public = "input";
protected $protected = "hello";
private $private = "world";
}
$o = new KeyVal();
$nested = new KeyVal();
$o->public = $nested;
// Percent encoded "public[public]=input"
var_dump(http_build_query($o));
?>
--EXPECT--
string(24) "public%5Bpublic%5D=input"

View file

@ -0,0 +1,17 @@
--TEST--
http_build_query() function with recursif object
--FILE--
<?php
class KeyVal {
public $public = "input";
protected $protected = "hello";
private $private = "world";
}
$o = new KeyVal();
$o->public = $o;
var_dump(http_build_query($o));
?>
--EXPECT--
string(0) ""

View file

@ -0,0 +1,8 @@
--TEST--
http_build_query() function with null in array
--FILE--
<?php
var_dump(http_build_query([null]));
?>
--EXPECT--
string(0) ""

View file

@ -0,0 +1,11 @@
--TEST--
http_build_query() function with reference in array
--FILE--
<?php
$v = 'value';
$ref = &$v;
var_dump(http_build_query([$ref]));
?>
--EXPECT--
string(7) "0=value"

View file

@ -0,0 +1,8 @@
--TEST--
http_build_query() function with resource in array
--FILE--
<?php
var_dump(http_build_query([STDIN]));
?>
--EXPECT--
string(0) ""