|
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\Bind; |
|
8
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\BindType; |
|
9
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
class BindTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function testConstructorWithAllParameters(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$name = 'testBinding'; |
|
17
|
|
|
$value = 'testValue'; |
|
18
|
|
|
$type = BindType::STRING; |
|
19
|
|
|
$onChange = [$this->createMock(AbstractStandardCommand::class)]; |
|
20
|
|
|
|
|
21
|
|
|
$bind = new Bind($name, $value, $type, $onChange); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertSame($name, $bind->name); |
|
24
|
|
|
$this->assertSame($value, $bind->value); |
|
25
|
|
|
$this->assertSame($type, $bind->type); |
|
26
|
|
|
$this->assertSame($onChange, $bind->onChange); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testConstructorWithDefaultType(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$name = 'myBinding'; |
|
32
|
|
|
$value = 42; |
|
33
|
|
|
|
|
34
|
|
|
$bind = new Bind($name, $value); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame($name, $bind->name); |
|
37
|
|
|
$this->assertSame($value, $bind->value); |
|
38
|
|
|
$this->assertSame(BindType::ANY, $bind->type); |
|
39
|
|
|
$this->assertEmpty($bind->onChange); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testJsonSerializeWithAllProperties(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$name = 'binding1'; |
|
45
|
|
|
$value = ['array', 'value']; |
|
46
|
|
|
$type = BindType::BOOLEAN; |
|
47
|
|
|
$onChange = [ |
|
48
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
49
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
$bind = new Bind($name, $value, $type, $onChange); |
|
53
|
|
|
$result = $bind->jsonSerialize(); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertSame($name, $result['name']); |
|
56
|
|
|
$this->assertSame($value, $result['value']); |
|
57
|
|
|
$this->assertSame($type->value, $result['type']); |
|
58
|
|
|
$this->assertSame($onChange, $result['onChange']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testJsonSerializeWithEmptyOnChange(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$bind = new Bind('test', 'value', BindType::NUMBER, []); |
|
64
|
|
|
$result = $bind->jsonSerialize(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertArrayNotHasKey('onChange', $result); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testJsonSerializeWithDifferentValueTypes(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$testCases = [ |
|
72
|
|
|
['string value', BindType::STRING], |
|
73
|
|
|
[123, BindType::NUMBER], |
|
74
|
|
|
[45.67, BindType::NUMBER], |
|
75
|
|
|
[true, BindType::BOOLEAN], |
|
76
|
|
|
[['nested' => 'array'], BindType::ANY], |
|
77
|
|
|
[null, BindType::ANY], |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($testCases as [$value, $type]) { |
|
81
|
|
|
$bind = new Bind('test', $value, $type); |
|
82
|
|
|
$result = $bind->jsonSerialize(); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertSame($value, $result['value']); |
|
85
|
|
|
$this->assertSame($type->value, $result['type']); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testJsonSerializeWithDifferentBindTypes(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$bindTypes = [BindType::ANY, BindType::STRING, BindType::NUMBER, BindType::BOOLEAN]; |
|
92
|
|
|
|
|
93
|
|
|
foreach ($bindTypes as $type) { |
|
94
|
|
|
$bind = new Bind('test', 'value', $type); |
|
95
|
|
|
$result = $bind->jsonSerialize(); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertSame($type->value, $result['type']); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testImplementsJsonSerializable(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$bind = new Bind('test', 'value'); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertInstanceOf(\JsonSerializable::class, $bind); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|