-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathUnionArrayTest.php
More file actions
57 lines (50 loc) · 1.78 KB
/
Copy pathUnionArrayTest.php
File metadata and controls
57 lines (50 loc) · 1.78 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
<?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\Union;
class UnionArray extends JsonSerializableType
{
/**
* @var array<int, datetime|string|null> $mixedDates
*/
#[ArrayType(['integer' => new Union('datetime', 'string', 'null')])]
#[JsonProperty('mixed_dates')]
public array $mixedDates;
/**
* @param array{
* mixedDates: array<int, datetime|string|null>,
* } $values
*/
public function __construct(
array $values,
) {
$this->mixedDates = $values['mixedDates'];
}
}
class UnionArrayTest extends TestCase
{
public function testUnionArray(): void
{
$expectedJson = json_encode(
[
'mixed_dates' => [
1 => '2023-01-01T12:00:00Z',
2 => null,
3 => 'Some String'
]
],
JSON_THROW_ON_ERROR
);
$object = UnionArray::fromJson($expectedJson);
$this->assertInstanceOf(DateTime::class, $object->mixedDates[1], 'mixed_dates[1] should be a DateTime instance.');
$this->assertEquals('2023-01-01 12:00:00', $object->mixedDates[1]->format('Y-m-d H:i:s'), 'mixed_dates[1] should have the correct datetime.');
$this->assertNull($object->mixedDates[2], 'mixed_dates[2] should be null.');
$this->assertEquals('Some String', $object->mixedDates[3], 'mixed_dates[3] should be "Some String".');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for mixed_dates.');
}
}