-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathTraitTest.php
More file actions
60 lines (51 loc) · 1.56 KB
/
Copy pathTraitTest.php
File metadata and controls
60 lines (51 loc) · 1.56 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
<?php
namespace Intercom\Tests\Core\Json;
use PHPUnit\Framework\TestCase;
use Intercom\Core\Json\JsonProperty;
use Intercom\Core\Json\JsonSerializableType;
trait IntegerPropertyTrait
{
/**
* @var int $integerProperty
*/
#[JsonProperty('integer_property')]
public int $integerProperty;
}
class TypeWithTrait extends JsonSerializableType
{
use IntegerPropertyTrait;
/**
* @var string $stringProperty
*/
#[JsonProperty('string_property')]
public string $stringProperty;
/**
* @param array{
* integerProperty: int,
* stringProperty: string,
* } $values
*/
public function __construct(array $values)
{
$this->integerProperty = $values['integerProperty'];
$this->stringProperty = $values['stringProperty'];
}
}
class TraitTest extends TestCase
{
public function testTraitPropertyAndString(): void
{
$expectedJson = json_encode(
[
'integer_property' => 42,
'string_property' => 'Hello, World!',
],
JSON_THROW_ON_ERROR
);
$object = TypeWithTrait::fromJson($expectedJson);
$this->assertEquals(42, $object->integerProperty, 'integer_property should be 42.');
$this->assertEquals('Hello, World!', $object->stringProperty, 'string_property should be "Hello, World!".');
$actualJson = $object->toJson();
$this->assertJsonStringEqualsJsonString($expectedJson, $actualJson, 'Serialized JSON does not match original JSON for ScalarTypesTestWithTrait.');
}
}