testJsonSerializeWithPartialProperties()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\APL\Component;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\Entity;
8
use PHPUnit\Framework\TestCase;
9
10
class EntityTest extends TestCase
11
{
12
    public function testConstructorWithAllParameters(): void
13
    {
14
        $id = 'entity123';
15
        $type = 'person';
16
        $value = ['name' => 'John Doe', 'age' => 30];
17
18
        $entity = new Entity($id, $type, $value);
19
20
        $this->assertSame($id, $entity->id);
21
        $this->assertSame($type, $entity->type);
22
        $this->assertSame($value, $entity->value);
23
    }
24
25
    public function testConstructorWithDefaultParameters(): void
26
    {
27
        $entity = new Entity();
28
29
        $this->assertNull($entity->id);
30
        $this->assertNull($entity->type);
31
        $this->assertNull($entity->value);
32
    }
33
34
    public function testJsonSerializeWithAllProperties(): void
35
    {
36
        $id = 'test-entity';
37
        $type = 'product';
38
        $value = 'Test Product';
39
40
        $entity = new Entity($id, $type, $value);
41
        $result = $entity->jsonSerialize();
42
43
        $this->assertSame($id, $result['id']);
44
        $this->assertSame($type, $result['type']);
45
        $this->assertSame($value, $result['value']);
46
    }
47
48
    public function testJsonSerializeWithNullValues(): void
49
    {
50
        $entity = new Entity();
51
        $result = $entity->jsonSerialize();
52
53
        $this->assertArrayNotHasKey('id', $result);
54
        $this->assertArrayNotHasKey('type', $result);
55
        $this->assertArrayNotHasKey('value', $result);
56
        $this->assertEmpty($result);
57
    }
58
59
    public function testJsonSerializeWithDifferentValueTypes(): void
60
    {
61
        $testCases = [
62
            ['string value', 'string'],
63
            [123, 'integer'],
64
            [45.67, 'float'],
65
            [true, 'boolean'],
66
            [['nested' => 'array'], 'array'],
67
        ];
68
69
        foreach ($testCases as [$value, $description]) {
70
            $entity = new Entity('id', 'type', $value);
71
            $result = $entity->jsonSerialize();
72
73
            $this->assertSame($value, $result['value'], "Failed for $description");
74
        }
75
    }
76
77
    public function testJsonSerializeWithPartialProperties(): void
78
    {
79
        $entity = new Entity('only-id');
80
        $result = $entity->jsonSerialize();
81
82
        $this->assertSame('only-id', $result['id']);
83
        $this->assertArrayNotHasKey('type', $result);
84
        $this->assertArrayNotHasKey('value', $result);
85
    }
86
87
    public function testJsonSerializeWithZeroAndFalseValues(): void
88
    {
89
        $entity = new Entity('id', 'type', 0);
90
        $result = $entity->jsonSerialize();
91
92
        $this->assertSame(0, $result['value']);
93
94
        $entity2 = new Entity('id', 'type', false);
95
        $result2 = $entity2->jsonSerialize();
96
97
        $this->assertFalse($result2['value']);
98
    }
99
100
    public function testJsonSerializeWithEmptyStringValue(): void
101
    {
102
        $entity = new Entity('id', 'type', '');
103
        $result = $entity->jsonSerialize();
104
105
        $this->assertSame('', $result['value']);
106
    }
107
108
    public function testImplementsJsonSerializable(): void
109
    {
110
        $entity = new Entity();
111
112
        $this->assertInstanceOf(\JsonSerializable::class, $entity);
113
    }
114
}
115