Skip to content

Commit 39abf54

Browse files
committed
Add json encode recursion tests
1 parent 49c1cdb commit 39abf54

6 files changed

+210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
json_encode() Recursion test with just JsonSerializable
3+
--FILE--
4+
<?php
5+
6+
class SerializingTest implements JsonSerializable
7+
{
8+
public $a = 1;
9+
10+
private $b = 'hide';
11+
12+
protected $c = 'protect';
13+
14+
public function jsonSerialize(): mixed
15+
{
16+
$result = json_encode($this);
17+
var_dump($result);
18+
return $this;
19+
}
20+
}
21+
22+
var_dump(json_encode(new SerializingTest()));
23+
?>
24+
--EXPECT--
25+
bool(false)
26+
string(7) "{"a":1}"
27+
string(7) "{"a":1}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
json_encode() Recursion test with JsonSerializable and var_dump simple
3+
--FILE--
4+
<?php
5+
6+
class SerializingTest implements JsonSerializable
7+
{
8+
public $a = 1;
9+
10+
public function jsonSerialize(): mixed
11+
{
12+
var_dump($this);
13+
return $this;
14+
}
15+
}
16+
17+
var_dump(json_encode(new SerializingTest()));
18+
19+
?>
20+
--EXPECT--
21+
object(SerializingTest)#1 (1) {
22+
["a"]=>
23+
int(1)
24+
}
25+
string(7) "{"a":1}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
json_encode() Recursion test with JsonSerializable and __debugInfo
3+
--FILE--
4+
<?php
5+
6+
class SerializingTest implements JsonSerializable
7+
{
8+
public $a = 1;
9+
10+
public function __debugInfo()
11+
{
12+
return [ 'result' => json_encode($this) ];
13+
}
14+
15+
public function jsonSerialize(): mixed
16+
{
17+
var_dump($this);
18+
return $this;
19+
}
20+
}
21+
22+
var_dump(json_encode(new SerializingTest()));
23+
echo "---------\n";
24+
var_dump(new SerializingTest());
25+
26+
?>
27+
--EXPECT--
28+
*RECURSION*
29+
object(SerializingTest)#1 (1) {
30+
["result"]=>
31+
string(7) "{"a":1}"
32+
}
33+
string(7) "{"a":1}"
34+
---------
35+
*RECURSION*
36+
object(SerializingTest)#1 (1) {
37+
["result"]=>
38+
string(7) "{"a":1}"
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
json_encode() Recursion test with JsonSerializable, __debugInfo and var_export
3+
--FILE--
4+
<?php
5+
6+
class SerializingTest implements JsonSerializable
7+
{
8+
public $a = 1;
9+
10+
public function __debugInfo()
11+
{
12+
return [ 'result' => var_export($this, true) ];
13+
}
14+
15+
public function jsonSerialize(): mixed
16+
{
17+
var_dump($this);
18+
return $this;
19+
}
20+
}
21+
22+
var_dump(json_encode(new SerializingTest()));
23+
echo "---------\n";
24+
var_dump(new SerializingTest());
25+
26+
?>
27+
--EXPECT--
28+
object(SerializingTest)#1 (1) {
29+
["result"]=>
30+
string(52) "\SerializingTest::__set_state(array(
31+
'a' => 1,
32+
))"
33+
}
34+
string(7) "{"a":1}"
35+
---------
36+
object(SerializingTest)#1 (1) {
37+
["result"]=>
38+
string(52) "\SerializingTest::__set_state(array(
39+
'a' => 1,
40+
))"
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
json_encode() Recursion test with JsonSerializable, __debugInfo and print_r
3+
--FILE--
4+
<?php
5+
6+
class SerializingTest implements JsonSerializable
7+
{
8+
public $a = 1;
9+
10+
public function __debugInfo()
11+
{
12+
return [ 'result' => $this->a ];
13+
}
14+
15+
public function jsonSerialize(): mixed
16+
{
17+
print_r($this);
18+
return $this;
19+
}
20+
}
21+
22+
var_dump(json_encode(new SerializingTest()));
23+
echo "---------\n";
24+
var_dump(new SerializingTest());
25+
26+
?>
27+
--EXPECT--
28+
SerializingTest Object
29+
(
30+
[result] => 1
31+
)
32+
string(7) "{"a":1}"
33+
---------
34+
object(SerializingTest)#1 (1) {
35+
["result"]=>
36+
int(1)
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
json_encode() Recursion test with JsonSerializable and serialize
3+
--FILE--
4+
<?php
5+
6+
class JsonEncodeFirstTest implements JsonSerializable
7+
{
8+
public $a = 1;
9+
10+
public function __serialize()
11+
{
12+
return [ 'result' => $this->a ];
13+
}
14+
15+
public function jsonSerialize(): mixed
16+
{
17+
return [ 'serialize' => serialize($this) ];
18+
}
19+
}
20+
21+
class SerializeFirstTest implements JsonSerializable
22+
{
23+
public $a = 1;
24+
25+
public function __serialize()
26+
{
27+
return [ 'result' => json_encode($this) ];
28+
}
29+
30+
public function jsonSerialize(): mixed
31+
{
32+
return [ 'json' => serialize($this) ];
33+
}
34+
}
35+
36+
var_dump(json_encode(new JsonEncodeFirstTest()));
37+
var_dump(serialize(new SerializeFirstTest()));
38+
?>
39+
--EXPECT--
40+
string(68) "{"serialize":"O:19:\"JsonEncodeFirstTest\":1:{s:6:\"result\";i:1;}"}"
41+
string(194) "O:18:"SerializeFirstTest":1:{s:6:"result";s:142:"{"json":"O:18:\"SerializeFirstTest\":1:{s:6:\"result\";s:62:\"{\"json\":\"O:18:\\\"SerializeFirstTest\\\":1:{s:6:\\\"result\\\";b:0;}\"}\";}"}";}"

0 commit comments

Comments
 (0)