-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathSerializeTest.php
More file actions
97 lines (90 loc) · 3.14 KB
/
SerializeTest.php
File metadata and controls
97 lines (90 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
declare(strict_types=1);
namespace Tests\Unit\Serializers;
use Tests\Fixtures\TestEnum;
use Tests\TestCase;
use Throwable;
use Workflow\Serializers\Base64;
use Workflow\Serializers\Serializer;
use Workflow\Serializers\Y;
final class SerializeTest extends TestCase
{
/**
* @dataProvider dataProvider
*/
public function testSerialize($data): void
{
$this->testSerializeUnserialize($data, Y::class, Y::class);
$this->testSerializeUnserialize($data, Base64::class, Base64::class);
$this->testSerializeUnserialize($data, Y::class, Base64::class);
$this->testSerializeUnserialize($data, Base64::class, Y::class);
}
public static function dataProvider(): array
{
return [
'array []' => [[]],
'array [[]]' => [[[]]],
'array assoc' => [
[
'key' => 'value',
],
],
'bool true' => [true],
'bool false' => [false],
'enum' => [TestEnum::First],
'enum[]' => [[TestEnum::First]],
'int(PHP_INT_MIN)' => [PHP_INT_MIN],
'int(PHP_INT_MAX)' => [PHP_INT_MAX],
'int(-1)' => [-1],
'int(0)' => [0],
'int(1)' => [1],
'exception' => [new \Exception('test')],
'float PHP_FLOAT_EPSILON' => [PHP_FLOAT_EPSILON],
'float PHP_FLOAT_MIN' => [PHP_FLOAT_MIN],
'float -PHP_FLOAT_MIN' => [-PHP_FLOAT_MIN],
'float PHP_FLOAT_MAX' => [PHP_FLOAT_MAX],
'float(-1.123456789)' => [-1.123456789],
'float(0.0)' => [0.0],
'float(1.123456789)' => [1.123456789],
'null' => [null],
'string empty' => [''],
'string foo' => ['foo'],
'string bytes' => [random_bytes(4096)],
];
}
public function testSerializableReturnsFalseForClosure(): void
{
$this->assertFalse(Serializer::serializable(static function () {
return 'test';
}));
}
private function testSerializeUnserialize($data, $serializer, $unserializer): void
{
config([
'workflows.serializer' => $serializer,
]);
$serialized = Serializer::serialize($data);
config([
'workflows.serializer' => $unserializer,
]);
$unserialized = Serializer::unserialize($serialized);
if (is_object($data)) {
if ($data instanceof Throwable) {
$this->assertEquals([
'class' => get_class($data),
'message' => $data->getMessage(),
'code' => $data->getCode(),
'line' => $data->getLine(),
'file' => $data->getFile(),
'trace' => collect($data->getTrace())
->filter(static fn ($trace) => Serializer::serializable($trace))
->toArray(),
], $unserialized);
} else {
$this->assertEqualsCanonicalizing($data, $unserialized);
}
} else {
$this->assertSame($data, $unserialized);
}
}
}