Skip to content

Commit f094c4a

Browse files
authored
added test for advanced machine detection (#501)
* added test for advanced machine detection * Fixed text and logic
1 parent 6b3a8b7 commit f094c4a

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

src/Voice/VoiceObjects/AdvancedMachineDetection.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ protected function isValidTimeout(int $beepTimeout): bool
7474

7575
public function fromArray(array $data): static
7676
{
77-
$this->isArrayValid($data);
77+
if (!$this->isArrayValid($data)) {
78+
throw new \InvalidArgumentException('Invalid payload');
79+
};
7880

7981
$this->behaviour = $data['behaviour'];
8082
$this->mode = $data['mode'];
@@ -102,8 +104,12 @@ protected function isArrayValid(array $data): bool
102104
return false;
103105
}
104106

105-
return $this->isValidBehaviour($data['behaviour'])
106-
|| $this->isValidMode($data['mode'])
107-
|| $this->isValidTimeout($data['beep_timeout']);
107+
if ($this->isValidBehaviour($data['behaviour'])
108+
&& $this->isValidMode($data['mode'])
109+
&& $this->isValidTimeout($data['beep_timeout'])) {
110+
return true;
111+
};
112+
113+
return false;
108114
}
109115
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace VonageTest\Voice\VoiceObjects;
4+
5+
use InvalidArgumentException;
6+
use OutOfBoundsException;
7+
use PHPUnit\Framework\TestCase;
8+
use Vonage\Voice\VoiceObjects\AdvancedMachineDetection;
9+
10+
class AdvancedMachineDetectionTest extends TestCase
11+
{
12+
public function testValidConstructor()
13+
{
14+
$amd = new AdvancedMachineDetection('continue', 60, 'detect');
15+
$this->assertInstanceOf(AdvancedMachineDetection::class, $amd);
16+
}
17+
18+
public function testInvalidBehaviour()
19+
{
20+
$this->expectException(InvalidArgumentException::class);
21+
new AdvancedMachineDetection('invalid_behaviour', 60, 'detect');
22+
}
23+
24+
public function testInvalidMode()
25+
{
26+
$this->expectException(InvalidArgumentException::class);
27+
new AdvancedMachineDetection('continue', 60, 'invalid_mode');
28+
}
29+
30+
public function testInvalidBeepTimeout()
31+
{
32+
$this->expectException(OutOfBoundsException::class);
33+
new AdvancedMachineDetection('continue', 150, 'detect');
34+
}
35+
36+
public function testValidBeepTimeoutRange()
37+
{
38+
$amd = new AdvancedMachineDetection('hangup', 100, 'detect_beep');
39+
$this->assertEquals(100, $amd->toArray()['beep_timeout']);
40+
}
41+
42+
public function testWillRenderDefault()
43+
{
44+
$amd = new AdvancedMachineDetection('hangup', 100, 'default');
45+
$this->assertEquals('default', $amd->toArray()['mode']);
46+
}
47+
48+
public function testToArray()
49+
{
50+
$amd = new AdvancedMachineDetection('continue', 45, 'detect');
51+
$expected = [
52+
'behavior' => 'continue',
53+
'mode' => 'detect',
54+
'beep_timeout' => 45
55+
];
56+
57+
$this->assertEquals($expected, $amd->toArray());
58+
}
59+
60+
public function testFromArrayValid()
61+
{
62+
$data = [
63+
'behaviour' => 'hangup',
64+
'mode' => 'detect_beep',
65+
'beep_timeout' => 60
66+
];
67+
68+
$amd = (new AdvancedMachineDetection('continue', 45))->fromArray($data);
69+
$this->assertEquals('hangup', $amd->toArray()['behavior']);
70+
$this->assertEquals('detect_beep', $amd->toArray()['mode']);
71+
$this->assertEquals(60, $amd->toArray()['beep_timeout']);
72+
}
73+
74+
public function testFromArrayInvalidData()
75+
{
76+
$this->expectException(InvalidArgumentException::class);
77+
78+
$data = [
79+
'behaviour' => 'invalid_behaviour',
80+
'mode' => 'detect',
81+
'beep_timeout' => 60
82+
];
83+
84+
(new AdvancedMachineDetection('continue', 45))->fromArray($data);
85+
}
86+
}

0 commit comments

Comments
 (0)