Passed
Pull Request — master (#97)
by Maximilian
04:17
created

StyleValueTest::testJsonSerializeWithoutWhen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
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\Document;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\StyleValue;
8
use PHPUnit\Framework\TestCase;
9
10
class StyleValueTest extends TestCase
11
{
12
    public function testConstructorWithAllParameters(): void
13
    {
14
        $when = 'viewport.width > 100';
15
        $properties = ['color' => 'red', 'fontSize' => '16dp'];
16
17
        $styleValue = new StyleValue($when, $properties);
18
19
        $this->assertSame($when, $styleValue->when);
20
        $this->assertSame($properties, $styleValue->properties);
21
    }
22
23
    public function testConstructorWithDefaultParameters(): void
24
    {
25
        $styleValue = new StyleValue();
26
27
        $this->assertNull($styleValue->when);
28
        $this->assertSame([], $styleValue->properties);
29
    }
30
31
    public function testSetMethodWithRegularProperty(): void
32
    {
33
        $styleValue = new StyleValue();
34
        $result = $styleValue->set('color', 'blue');
35
36
        $this->assertSame($styleValue, $result);
37
        $this->assertSame('blue', $styleValue->properties['color']);
38
    }
39
40
    public function testSetMethodWithWhenProperty(): void
41
    {
42
        $styleValue = new StyleValue();
43
        $result = $styleValue->set('when', 'condition');
44
45
        $this->assertSame($styleValue, $result);
46
        $this->assertSame('condition', $styleValue->when);
47
        $this->assertArrayNotHasKey('when', $styleValue->properties);
48
    }
49
50
    public function testSetMethodWithNonStringWhenValue(): void
51
    {
52
        $styleValue = new StyleValue();
53
        $styleValue->set('when', 123);
54
55
        $this->assertNull($styleValue->when);
56
    }
57
58
    public function testSetMethodChaining(): void
59
    {
60
        $styleValue = new StyleValue();
61
        $result = $styleValue
62
            ->set('color', 'red')
63
            ->set('fontSize', '20dp')
64
            ->set('when', 'true');
65
66
        $this->assertSame($styleValue, $result);
67
        $this->assertSame('red', $styleValue->properties['color']);
68
        $this->assertSame('20dp', $styleValue->properties['fontSize']);
69
        $this->assertSame('true', $styleValue->when);
70
    }
71
72
    public function testJsonSerializeWithWhenAndProperties(): void
73
    {
74
        $styleValue = new StyleValue('condition', ['color' => 'green', 'opacity' => 0.5]);
75
        $result = $styleValue->jsonSerialize();
76
77
        $this->assertArrayHasKey('when', $result);
78
        $this->assertArrayHasKey('color', $result);
79
        $this->assertArrayHasKey('opacity', $result);
80
        $this->assertSame('condition', $result['when']);
81
        $this->assertSame('green', $result['color']);
82
        $this->assertSame(0.5, $result['opacity']);
83
    }
84
85
    public function testJsonSerializeWhenAppearsFirst(): void
86
    {
87
        $styleValue = new StyleValue('test', ['color' => 'blue', 'fontSize' => '14dp']);
88
        $result = $styleValue->jsonSerialize();
89
90
        $keys = array_keys($result);
91
        $this->assertSame('when', $keys[0]);
92
        $this->assertContains('color', $keys);
93
        $this->assertContains('fontSize', $keys);
94
    }
95
96
    public function testJsonSerializeWithoutWhen(): void
97
    {
98
        $styleValue = new StyleValue(null, ['color' => 'yellow', 'padding' => '10dp']);
99
        $result = $styleValue->jsonSerialize();
100
101
        $this->assertArrayNotHasKey('when', $result);
102
        $this->assertArrayHasKey('color', $result);
103
        $this->assertArrayHasKey('padding', $result);
104
        $this->assertSame('yellow', $result['color']);
105
        $this->assertSame('10dp', $result['padding']);
106
    }
107
108
    public function testJsonSerializeWithEmptyWhen(): void
109
    {
110
        $styleValue = new StyleValue('', ['color' => 'purple']);
111
        $result = $styleValue->jsonSerialize();
112
113
        $this->assertArrayNotHasKey('when', $result);
114
        $this->assertArrayHasKey('color', $result);
115
    }
116
117
    public function testJsonSerializeWithOnlyProperties(): void
118
    {
119
        $properties = ['margin' => '5dp', 'backgroundColor' => '#fff'];
120
        $styleValue = new StyleValue(null, $properties);
121
        $result = $styleValue->jsonSerialize();
122
123
        $this->assertSame($properties, $result);
124
    }
125
126
    public function testJsonSerializeWithEmptyProperties(): void
127
    {
128
        $styleValue = new StyleValue('condition', []);
129
        $result = $styleValue->jsonSerialize();
130
131
        $this->assertCount(1, $result);
132
        $this->assertArrayHasKey('when', $result);
133
        $this->assertSame('condition', $result['when']);
134
    }
135
136
    public function testJsonSerializeWithMixedPropertyTypes(): void
137
    {
138
        $properties = [
139
            'string' => 'value',
140
            'number' => 42,
141
            'float' => 3.14,
142
            'boolean' => true,
143
            'array' => ['nested', 'array'],
144
        ];
145
        $styleValue = new StyleValue(null, $properties);
146
        $result = $styleValue->jsonSerialize();
147
148
        $this->assertSame('value', $result['string']);
149
        $this->assertSame(42, $result['number']);
150
        $this->assertSame(3.14, $result['float']);
151
        $this->assertTrue($result['boolean']);
152
        $this->assertSame(['nested', 'array'], $result['array']);
153
    }
154
155
    public function testSetOverwritesExistingProperty(): void
156
    {
157
        $styleValue = new StyleValue(null, ['color' => 'red']);
158
        $styleValue->set('color', 'blue');
159
160
        $this->assertSame('blue', $styleValue->properties['color']);
161
    }
162
163
    public function testSetOverwritesExistingWhen(): void
164
    {
165
        $styleValue = new StyleValue('original');
166
        $styleValue->set('when', 'updated');
167
168
        $this->assertSame('updated', $styleValue->when);
169
    }
170
171
    public function testImplementsJsonSerializable(): void
172
    {
173
        $styleValue = new StyleValue();
174
175
        $this->assertInstanceOf(\JsonSerializable::class, $styleValue);
176
    }
177
}
178