testExtendsTouchableComponent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
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\APLBaseComponent;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\TouchableComponent;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\TouchWrapperComponent;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
12
use PHPUnit\Framework\TestCase;
13
14
class TouchWrapperComponentTest extends TestCase
15
{
16
    public function testConstructorWithAllParameters(): void
17
    {
18
        $item = $this->createMock(APLBaseComponent::class);
19
        $items = [
20
            $this->createMock(APLBaseComponent::class),
21
            $this->createMock(APLBaseComponent::class),
22
        ];
23
24
        $component = new TouchWrapperComponent($item, $items);
25
26
        $this->assertSame($item, $component->item);
27
        $this->assertSame($items, $component->items);
28
    }
29
30
    public function testConstructorWithDefaultParameters(): void
31
    {
32
        $component = new TouchWrapperComponent();
33
34
        $this->assertNull($component->item);
35
        $this->assertNull($component->items);
36
    }
37
38
    public function testJsonSerializeWithAllProperties(): void
39
    {
40
        $item = $this->createMock(APLBaseComponent::class);
41
        $items = [
42
            $this->createMock(APLBaseComponent::class),
43
            $this->createMock(APLBaseComponent::class),
44
        ];
45
46
        $component = new TouchWrapperComponent($item, $items);
47
        $result = $component->jsonSerialize();
48
49
        $this->assertSame(APLComponentType::TOUCH_WRAPPER->value, $result['type']);
50
        $this->assertSame($item, $result['item']);
51
        $this->assertSame($items, $result['items']);
52
    }
53
54
    public function testJsonSerializeWithOnlyItem(): void
55
    {
56
        $item = $this->createMock(APLBaseComponent::class);
57
        $component = new TouchWrapperComponent($item);
58
        $result = $component->jsonSerialize();
59
60
        $this->assertSame(APLComponentType::TOUCH_WRAPPER->value, $result['type']);
61
        $this->assertSame($item, $result['item']);
62
        $this->assertArrayNotHasKey('items', $result);
63
    }
64
65
    public function testJsonSerializeWithOnlyItems(): void
66
    {
67
        $items = [$this->createMock(APLBaseComponent::class)];
68
        $component = new TouchWrapperComponent(null, $items);
69
        $result = $component->jsonSerialize();
70
71
        $this->assertSame(APLComponentType::TOUCH_WRAPPER->value, $result['type']);
72
        $this->assertArrayNotHasKey('item', $result);
73
        $this->assertSame($items, $result['items']);
74
    }
75
76
    public function testJsonSerializeWithEmptyItems(): void
77
    {
78
        $component = new TouchWrapperComponent(null, []);
79
        $result = $component->jsonSerialize();
80
81
        $this->assertArrayNotHasKey('items', $result);
82
    }
83
84
    public function testJsonSerializeWithNullValues(): void
85
    {
86
        $component = new TouchWrapperComponent();
87
        $result = $component->jsonSerialize();
88
89
        $this->assertSame(APLComponentType::TOUCH_WRAPPER->value, $result['type']);
90
        $this->assertArrayNotHasKey('item', $result);
91
        $this->assertArrayNotHasKey('items', $result);
92
    }
93
94
    public function testTypeConstant(): void
95
    {
96
        $this->assertSame(APLComponentType::TOUCH_WRAPPER, TouchWrapperComponent::TYPE);
97
    }
98
99
    public function testExtendsTouchableComponent(): void
100
    {
101
        $component = new TouchWrapperComponent();
102
103
        $this->assertInstanceOf(TouchableComponent::class, $component);
104
    }
105
106
    public function testImplementsJsonSerializable(): void
107
    {
108
        $component = new TouchWrapperComponent();
109
110
        $this->assertInstanceOf(\JsonSerializable::class, $component);
111
    }
112
113
    public function testJsonSerializeIncludesTouchableProperties(): void
114
    {
115
        $component = new TouchWrapperComponent();
116
117
        // Candidate event / gesture properties typically exposed by a touchable component.
118
        $candidateEventProps = [
119
            'onPress', 'onLongPress', 'onDown', 'onUp', 'onCancel', 'onMove',
120
            'onFocus', 'onBlur', 'gestures', 'onSwipe', 'onScroll',
121
        ];
122
123
        $expected = [];
124
125
        foreach ($candidateEventProps as $prop) {
126
            if (property_exists($component, $prop)) {
127
                // For command arrays supply one mock command; for gestures supply a simple array element.
128
                if (str_starts_with($prop, 'on')) {
129
                    $expected[$prop] = [$this->createMock(AbstractStandardCommand::class)];
130
                } elseif ($prop === 'gestures') {
131
                    $expected[$prop] = [['type' => 'testGesture']];
132
                } else {
133
                    $expected[$prop] = ['value'];
134
                }
135
                $component->$prop = $expected[$prop];
136
            }
137
        }
138
139
        $json = $component->jsonSerialize();
140
141
        // Base type assertion still valid.
142
        $this->assertSame(APLComponentType::TOUCH_WRAPPER->value, $json['type']);
143
144
        // Verify every property we actually set (and that exists) was serialized.
145
        foreach ($expected as $prop => $value) {
146
            $this->assertArrayHasKey($prop, $json, "Property '$prop' not serialized.");
147
            $this->assertSame($value, $json[$prop], "Serialized value mismatch for '$prop'.");
148
        }
149
    }
150
}
151