Passed
Push — master ( a53d55...43aca1 )
by Maximilian
03:50
created

EditTextComponentTest::testTypeConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\EditTextComponent;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\FontStyle;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\FontWeight;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\KeyboardType;
12
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\SecureInputType;
13
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\SubmitKeyType;
14
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
15
use PHPUnit\Framework\TestCase;
16
17
class EditTextComponentTest extends TestCase
18
{
19
    public function testConstructorWithAllParameters(): void
20
    {
21
        $borderColor = '#ff0000';
22
        $fontStyle = FontStyle::ITALIC;
23
        $keyboardType = KeyboardType::EMAIL_ADDRESS;
24
        $onTextChange = [$this->createMock(AbstractStandardCommand::class)];
25
        $secureInput = SecureInputType::DECIMAL_PAD;
26
27
        $component = new EditTextComponent(
28
            borderColor: $borderColor,
29
            fontStyle: $fontStyle,
30
            keyboardType: $keyboardType,
31
            onTextChange: $onTextChange,
32
            secureInput: $secureInput
33
        );
34
35
        $this->assertSame($borderColor, $component->borderColor);
36
        $this->assertSame($fontStyle, $component->fontStyle);
37
        $this->assertSame($keyboardType, $component->keyboardType);
38
        $this->assertSame($onTextChange, $component->onTextChange);
39
        $this->assertSame($secureInput, $component->secureInput);
40
    }
41
42
    public function testConstructorWithDefaultParameters(): void
43
    {
44
        $component = new EditTextComponent();
45
46
        $this->assertNull($component->borderColor);
47
        $this->assertSame('0', $component->borderWidth);
48
        $this->assertSame('sans-serif', $component->fontFamily);
49
        $this->assertSame('40dp', $component->fontSize);
50
        $this->assertSame('', $component->hint);
51
        $this->assertSame(0, $component->maxLength);
52
        $this->assertFalse($component->selectOnFocus);
53
        $this->assertSame(8, $component->size);
54
        $this->assertSame('', $component->text);
55
    }
56
57
    public function testJsonSerializeWithAllProperties(): void
58
    {
59
        $onSubmit = [$this->createMock(AbstractStandardCommand::class)];
60
61
        $component = new EditTextComponent(
62
            borderColor: '#blue',
63
            color: '#red',
64
            fontWeight: FontWeight::BOLD,
65
            hint: 'Enter text',
66
            maxLength: 100,
67
            onSubmit: $onSubmit,
68
            selectOnFocus: true,
69
            text: 'Initial text'
70
        );
71
72
        $result = $component->jsonSerialize();
73
74
        $this->assertSame(APLComponentType::EDIT_TEXT->value, $result['type']);
75
        $this->assertSame('#blue', $result['borderColor']);
76
        $this->assertSame('#red', $result['color']);
77
        $this->assertSame(FontWeight::BOLD->value, $result['fontWeight']);
78
        $this->assertSame('Enter text', $result['hint']);
79
        $this->assertSame(100, $result['maxLength']);
80
        $this->assertSame($onSubmit, $result['onSubmit']);
81
        $this->assertTrue($result['selectOnFocus']);
82
        $this->assertSame('Initial text', $result['text']);
83
    }
84
85
    public function testJsonSerializeWithDefaultValues(): void
86
    {
87
        $component = new EditTextComponent();
88
        $result = $component->jsonSerialize();
89
90
        $this->assertArrayNotHasKey('borderWidth', $result);
91
        $this->assertArrayNotHasKey('fontFamily', $result);
92
        $this->assertArrayNotHasKey('fontSize', $result);
93
        $this->assertArrayNotHasKey('hint', $result);
94
        $this->assertArrayNotHasKey('maxLength', $result);
95
        $this->assertArrayNotHasKey('selectOnFocus', $result);
96
        $this->assertArrayNotHasKey('size', $result);
97
        $this->assertArrayNotHasKey('text', $result);
98
    }
99
100
    public function testJsonSerializeWithNonDefaultValues(): void
101
    {
102
        $component = new EditTextComponent(
103
            borderWidth: '2px',
104
            fontFamily: 'Arial',
105
            fontSize: '20dp',
106
            size: 16
107
        );
108
109
        $result = $component->jsonSerialize();
110
111
        $this->assertSame('2px', $result['borderWidth']);
112
        $this->assertSame('Arial', $result['fontFamily']);
113
        $this->assertSame('20dp', $result['fontSize']);
114
        $this->assertSame(16, $result['size']);
115
    }
116
117
    public function testJsonSerializeWithEmptyCommands(): void
118
    {
119
        $component = new EditTextComponent(onTextChange: [], onSubmit: []);
120
        $result = $component->jsonSerialize();
121
122
        $this->assertArrayNotHasKey('onTextChange', $result);
123
        $this->assertArrayNotHasKey('onSubmit', $result);
124
    }
125
126
    public function testJsonSerializeWithEnumProperties(): void
127
    {
128
        $component = new EditTextComponent(
129
            fontStyle: FontStyle::NORMAL,
130
            hintStyle: FontStyle::ITALIC,
131
            submitKeyType: SubmitKeyType::DONE
132
        );
133
134
        $result = $component->jsonSerialize();
135
136
        $this->assertSame(FontStyle::NORMAL->value, $result['fontStyle']);
137
        $this->assertSame(FontStyle::ITALIC->value, $result['hintStyle']);
138
        $this->assertSame(SubmitKeyType::DONE->value, $result['submitKeyType']);
139
    }
140
141
    public function testTypeConstant(): void
142
    {
143
        $this->assertSame(APLComponentType::EDIT_TEXT, EditTextComponent::TYPE);
144
    }
145
}
146