Skip to content

Commit

Permalink
added test for advanced machine detection (#501)
Browse files Browse the repository at this point in the history
* added test for advanced machine detection

* Fixed text and logic
  • Loading branch information
SecondeJK authored Sep 12, 2024
1 parent 6b3a8b7 commit f094c4a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Voice/VoiceObjects/AdvancedMachineDetection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ protected function isValidTimeout(int $beepTimeout): bool

public function fromArray(array $data): static
{
$this->isArrayValid($data);
if (!$this->isArrayValid($data)) {
throw new \InvalidArgumentException('Invalid payload');
};

$this->behaviour = $data['behaviour'];
$this->mode = $data['mode'];
Expand Down Expand Up @@ -102,8 +104,12 @@ protected function isArrayValid(array $data): bool
return false;
}

return $this->isValidBehaviour($data['behaviour'])
|| $this->isValidMode($data['mode'])
|| $this->isValidTimeout($data['beep_timeout']);
if ($this->isValidBehaviour($data['behaviour'])
&& $this->isValidMode($data['mode'])
&& $this->isValidTimeout($data['beep_timeout'])) {
return true;
};

return false;
}
}
86 changes: 86 additions & 0 deletions test/Voice/VoiceObjects/AdvancedMachineDetectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace VonageTest\Voice\VoiceObjects;

use InvalidArgumentException;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
use Vonage\Voice\VoiceObjects\AdvancedMachineDetection;

class AdvancedMachineDetectionTest extends TestCase
{
public function testValidConstructor()
{
$amd = new AdvancedMachineDetection('continue', 60, 'detect');
$this->assertInstanceOf(AdvancedMachineDetection::class, $amd);
}

public function testInvalidBehaviour()
{
$this->expectException(InvalidArgumentException::class);
new AdvancedMachineDetection('invalid_behaviour', 60, 'detect');
}

public function testInvalidMode()
{
$this->expectException(InvalidArgumentException::class);
new AdvancedMachineDetection('continue', 60, 'invalid_mode');
}

public function testInvalidBeepTimeout()
{
$this->expectException(OutOfBoundsException::class);
new AdvancedMachineDetection('continue', 150, 'detect');
}

public function testValidBeepTimeoutRange()
{
$amd = new AdvancedMachineDetection('hangup', 100, 'detect_beep');
$this->assertEquals(100, $amd->toArray()['beep_timeout']);
}

public function testWillRenderDefault()
{
$amd = new AdvancedMachineDetection('hangup', 100, 'default');
$this->assertEquals('default', $amd->toArray()['mode']);
}

public function testToArray()
{
$amd = new AdvancedMachineDetection('continue', 45, 'detect');
$expected = [
'behavior' => 'continue',
'mode' => 'detect',
'beep_timeout' => 45
];

$this->assertEquals($expected, $amd->toArray());
}

public function testFromArrayValid()
{
$data = [
'behaviour' => 'hangup',
'mode' => 'detect_beep',
'beep_timeout' => 60
];

$amd = (new AdvancedMachineDetection('continue', 45))->fromArray($data);
$this->assertEquals('hangup', $amd->toArray()['behavior']);
$this->assertEquals('detect_beep', $amd->toArray()['mode']);
$this->assertEquals(60, $amd->toArray()['beep_timeout']);
}

public function testFromArrayInvalidData()
{
$this->expectException(InvalidArgumentException::class);

$data = [
'behaviour' => 'invalid_behaviour',
'mode' => 'detect',
'beep_timeout' => 60
];

(new AdvancedMachineDetection('continue', 45))->fromArray($data);
}
}

0 comments on commit f094c4a

Please sign in to comment.