|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Reactor; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use ReflectionException; |
|
16
|
|
|
use Spiral\Reactor\Exception\SerializeException; |
|
17
|
|
|
use Spiral\Reactor\Partial\Source; |
|
18
|
|
|
use Spiral\Reactor\Serializer; |
|
19
|
|
|
use Spiral\Reactor\Traits\SerializerTrait; |
|
20
|
|
|
use Spiral\Tests\Reactor\Fixture; |
|
21
|
|
|
|
|
22
|
|
|
class SerializerTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
//To cover this weird trait as well |
|
25
|
|
|
use SerializerTrait; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setSerializer(new Serializer()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testSetGet(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->setSerializer($s = new Serializer()); |
|
35
|
|
|
$this->assertSame($s, $this->getSerializer()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws ReflectionException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testEmptyArray(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->assertSame('[]', $this->getSerializer()->serialize([])); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws ReflectionException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testArrayOfArray(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->assertEquals($this->replace('[ |
|
52
|
|
|
\'hello\' => [ |
|
53
|
|
|
\'name\' => 123 |
|
54
|
|
|
] |
|
55
|
|
|
]'), $this->serialized([ |
|
56
|
|
|
'hello' => ['name' => 123], |
|
57
|
|
|
])); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $value |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
private function replace(string $value): string |
|
65
|
|
|
{ |
|
66
|
|
|
return preg_replace('/\s+/', '', $value); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $value |
|
71
|
|
|
* @return string |
|
72
|
|
|
* @throws ReflectionException |
|
73
|
|
|
*/ |
|
74
|
|
|
private function serialized($value): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->replace($this->getSerializer()->serialize($value)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws ReflectionException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testArrayOfArray2(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertEquals($this->replace('[ |
|
85
|
|
|
\'hello\' => [ |
|
86
|
|
|
\'name\' => 123, |
|
87
|
|
|
\'sub\' => magic |
|
88
|
|
|
] |
|
89
|
|
|
]'), $this->serialized([ |
|
90
|
|
|
'hello' => ['name' => 123, 'sub' => new Source(['magic'])], |
|
91
|
|
|
])); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws ReflectionException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testClassNames(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertEquals($this->replace('[ |
|
100
|
|
|
\'hello\' => [ |
|
101
|
|
|
\'name\' => 123, |
|
102
|
|
|
\'sub\' => \Spiral\Reactor\Serializer::class |
|
103
|
|
|
] |
|
104
|
|
|
]'), $this->serialized([ |
|
105
|
|
|
'hello' => ['name' => 123, 'sub' => Serializer::class], |
|
106
|
|
|
])); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @throws ReflectionException |
|
111
|
|
|
* |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testSerializeResource(): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->expectException(SerializeException::class); |
|
116
|
|
|
|
|
117
|
|
|
$this->getSerializer()->serialize(STDOUT); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @throws ReflectionException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testSerializeObject(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->expectException(SerializeException::class); |
|
126
|
|
|
|
|
127
|
|
|
$this->getSerializer()->serialize(new Fixture\SerializedObject()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @throws ReflectionException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function testSerializeStateObject(): void |
|
134
|
|
|
{ |
|
135
|
|
|
$this->assertEquals( |
|
136
|
|
|
$this->replace('\\Spiral\Tests\Reactor\Fixture\SerializedStateObject::__set_state(array())'), |
|
137
|
|
|
$this->serialized(new Fixture\SerializedStateObject()) |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|