-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathNestedUnionArrayTest.php
More file actions
89 lines (80 loc) · 3.27 KB
/
Copy pathNestedUnionArrayTest.php
File metadata and controls
89 lines (80 loc) · 3.27 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
<?php
namespace Intercom\Tests\Core\Json;
use DateTime;
use PHPUnit\Framework\TestCase;
use Intercom\Core\Json\JsonProperty;
use Intercom\Core\Json\JsonSerializableType;
use Intercom\Core\Types\ArrayType;
use Intercom\Core\Types\Constant;
use Intercom\Core\Types\Union;
class UnionObject extends JsonSerializableType
{
/**
* @var string $nestedProperty
*/
#[JsonProperty('nested_property')]
public string $nestedProperty;
/**
* @param array{
* nestedProperty: string,
* } $values
*/
public function __construct(
array $values,
) {
$this->nestedProperty = $values['nestedProperty'];
}
}
class NestedUnionArray extends JsonSerializableType
{
/**
* @var array<int, array<int, UnionObject|null|string>> $nestedArray
*/
#[ArrayType(['integer' => ['integer' => new Union(UnionObject::class, 'null', 'date')]])]
#[JsonProperty('nested_array')]
public array $nestedArray;
/**
* @param array{
* nestedArray: array<int, array<int, UnionObject|null|string>>,
* } $values
*/
public function __construct(
array $values,
) {
$this->nestedArray = $values['nestedArray'];
}
}
class NestedUnionArrayTest extends TestCase
{
public function testNestedUnionArray(): void
{
$expectedJson = json_encode(
[
'nested_array' => [
1 => [
1 => ['nested_property' => 'Nested One'],
2 => null,
4 => '2023-01-02'
],
2 => [
5 => ['nested_property' => 'Nested Two'],
7 => '2023-02-02'
]
]
],
JSON_THROW_ON_ERROR
);
$object = NestedUnionArray::fromJson($expectedJson);
$this->assertInstanceOf(UnionObject::class, $object->nestedArray[1][1], 'nested_array[1][1] should be an instance of Object.');
$this->assertEquals('Nested One', $object->nestedArray[1][1]->nestedProperty, 'nested_array[1][1]->nestedProperty should match the original data.');
$this->assertNull($object->nestedArray[1][2], 'nested_array[1][2] should be null.');
$this->assertInstanceOf(DateTime::class, $object->nestedArray[1][4], 'nested_array[1][4] should be a DateTime instance.');
$this->assertEquals('2023-01-02T00:00:00+00:00', $object->nestedArray[1][4]->format(Constant::DateTimeFormat), 'nested_array[1][4] should have the correct datetime.');
$this->assertInstanceOf(UnionObject::class, $object->nestedArray[2][5], 'nested_array[2][5] should be an instance of Object.');
$this->assertEquals('Nested Two', $object->nestedArray[2][5]->nestedProperty, 'nested_array[2][5]->nestedProperty should match the original data.');
$this->assertInstanceOf(DateTime::class, $object->nestedArray[2][7], 'nested_array[1][4] should be a DateTime instance.');
$this->assertEquals('2023-02-02', $object->nestedArray[2][7]->format('Y-m-d'), 'nested_array[1][4] should have the correct date.');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for nested_array.');
}
}