-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathUnionPropertyTest.php
More file actions
115 lines (96 loc) · 3.91 KB
/
Copy pathUnionPropertyTest.php
File metadata and controls
115 lines (96 loc) · 3.91 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Intercom\Tests\Core\Json;
use PHPUnit\Framework\TestCase;
use Intercom\Core\Json\JsonProperty;
use Intercom\Core\Json\JsonSerializableType;
use Intercom\Core\Types\Union;
class UnionProperty extends JsonSerializableType
{
#[Union(new Union('string', 'integer'), 'null', ['integer' => 'integer'], UnionProperty::class)]
#[JsonProperty('complexUnion')]
public mixed $complexUnion;
/**
* @param array{
* complexUnion: string|int|null|array<int, int>|UnionProperty
* } $values
*/
public function __construct(
array $values,
) {
$this->complexUnion = $values['complexUnion'];
}
}
class UnionPropertyTest extends TestCase
{
public function testWithMapOfIntToInt(): void
{
$expectedJson = json_encode(
[
'complexUnion' => [1 => 100, 2 => 200]
],
JSON_THROW_ON_ERROR
);
$object = UnionProperty::fromJson($expectedJson);
$this->assertIsArray($object->complexUnion, 'complexUnion should be an array.');
$this->assertEquals([1 => 100, 2 => 200], $object->complexUnion, 'complexUnion should match the original map of int => int.');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.');
}
public function testWithNestedUnionPropertyType(): void
{
$expectedJson = json_encode(
[
'complexUnion' => new UnionProperty(
[
'complexUnion' => 'Nested String'
]
)
],
JSON_THROW_ON_ERROR
);
$object = UnionProperty::fromJson($expectedJson);
$this->assertInstanceOf(UnionProperty::class, $object->complexUnion, 'complexUnion should be an instance of UnionPropertyType.');
$this->assertEquals('Nested String', $object->complexUnion->complexUnion, 'Nested complexUnion should match the original value.');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.');
}
public function testWithNull(): void
{
$expectedJson = json_encode(
[],
JSON_THROW_ON_ERROR
);
$object = UnionProperty::fromJson($expectedJson);
$this->assertNull($object->complexUnion, 'complexUnion should be null.');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.');
}
public function testWithInteger(): void
{
$expectedJson = json_encode(
[
'complexUnion' => 42
],
JSON_THROW_ON_ERROR
);
$object = UnionProperty::fromJson($expectedJson);
$this->assertIsInt($object->complexUnion, 'complexUnion should be an integer.');
$this->assertEquals(42, $object->complexUnion, 'complexUnion should match the original integer.');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.');
}
public function testWithString(): void
{
$expectedJson = json_encode(
[
'complexUnion' => 'Some String'
],
JSON_THROW_ON_ERROR
);
$object = UnionProperty::fromJson($expectedJson);
$this->assertIsString($object->complexUnion, 'complexUnion should be a string.');
$this->assertEquals('Some String', $object->complexUnion, 'complexUnion should match the original string.');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match the original JSON.');
}
}