Skip to content

Commit 4283114

Browse files
committed
TTS Tests
- Added tests for TTS request and callback. - Fixed callback / machine logic in request. - Fixed date returns in callback.
1 parent acc3ec3 commit 4283114

File tree

4 files changed

+153
-4
lines changed

4 files changed

+153
-4
lines changed

src/Nexmo/Voice/Message/Callback.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public function getDuration()
5656

5757
public function getCreated()
5858
{
59-
\DateTime::createFromFormat(self::TIME_FORMAT, $this->data['call-request']);
59+
return \DateTime::createFromFormat(self::TIME_FORMAT, $this->data['call-request']);
6060
}
6161

6262
public function getStart()
6363
{
64-
\DateTime::createFromFormat(self::TIME_FORMAT, $this->data['call-start']);
64+
return \DateTime::createFromFormat(self::TIME_FORMAT, $this->data['call-start']);
6565
}
6666

6767
public function getEnd()
6868
{
69-
\DateTime::createFromFormat(self::TIME_FORMAT, $this->data['call-end']);
69+
return \DateTime::createFromFormat(self::TIME_FORMAT, $this->data['call-end']);
7070
}
7171

7272
public function getNetwork()

src/Nexmo/Voice/Message/Message.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function setCallback($url, $method = null)
4040
$this->params['callback'] = $url;
4141
if(!is_null($method)){
4242
$this->params['callback_method'] = $method;
43+
} else {
44+
unset($this->params['callback_method']);
4345
}
4446

4547
return $this;
@@ -50,6 +52,8 @@ public function setMachineDetection($hangup = true, $timeout = null)
5052
$this->params['machine_detection'] = ($hangup ? 'hangup' : 'true');
5153
if(!is_null($timeout)){
5254
$this->params['machine_timeout'] = (int) $timeout;
55+
} else {
56+
unset($this->params['machine_timeout']);
5357
}
5458

5559
return $this;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* @author Tim Lytle <tim@timlytle.net>
4+
*/
5+
namespace Nexmo\Voice\Message;
6+
7+
class CallbackTest extends \PHPUnit_Framework_TestCase
8+
{
9+
protected $data = [
10+
'call-id' => '1234abcd',
11+
'status' => 'ok',
12+
'call-price' => '.0012',
13+
'call-rate' => '.012',
14+
'call-duration' => '10',
15+
'to' => '15553332323',
16+
'call-request' => '2014-01-01 10:30:15',
17+
'network-code' => '1234',
18+
'call-start' => '2014-01-01 10:30:25',
19+
'call-end' => '2014-01-01 10:30:35'
20+
];
21+
22+
/**
23+
* @var \Nexmo\Voice\Message\Callback
24+
*/
25+
protected $callback;
26+
27+
public function setUp()
28+
{
29+
$this->callback = new Callback($this->data);
30+
}
31+
32+
public function testSimpleValues()
33+
{
34+
$this->assertEquals($this->data['call-id'], $this->callback->getId());
35+
$this->assertEquals($this->data['status'], $this->callback->getStatus());
36+
$this->assertEquals($this->data['call-price'], $this->callback->getPrice());
37+
$this->assertEquals($this->data['call-rate'], $this->callback->getRate());
38+
$this->assertEquals($this->data['call-duration'], $this->callback->getDuration());
39+
$this->assertEquals($this->data['to'], $this->callback->getTo());
40+
$this->assertEquals($this->data['network-code'], $this->callback->getNetwork());
41+
}
42+
43+
public function testDateValues()
44+
{
45+
$this->assertEquals(new \DateTime('2014-01-01 10:30:15'), $this->callback->getCreated());
46+
$this->assertEquals(new \DateTime('2014-01-01 10:30:25'), $this->callback->getStart());
47+
$this->assertEquals(new \DateTime('2014-01-01 10:30:35'), $this->callback->getEnd());
48+
}
49+
}
50+

tests/Nexmo/Voice/Message/MessageTest.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
namespace Nexmo\Voice\Message;
77

8-
class MessageTest extends PHPUnit_Framework_TestCase
8+
class MessageTest extends \PHPUnit_Framework_TestCase
99
{
10+
/**
11+
* @var Message
12+
*/
1013
protected $message;
1114

1215
protected $text = 'TTS Text';
@@ -18,5 +21,97 @@ public function setUp()
1821
$this->message = new Message($this->text, $this->to, $this->from);
1922
}
2023

24+
public function testConstructorParams()
25+
{
26+
$params = $this->message->getParams();
27+
28+
$this->assertArrayHasKey('text', $params);
29+
$this->assertArrayHasKey('to', $params);
30+
$this->assertArrayHasKey('from', $params);
31+
32+
$this->assertEquals($this->text, $params['text']);
33+
$this->assertEquals($this->to, $params['to']);
34+
$this->assertEquals($this->from, $params['from']);
35+
}
36+
37+
public function testFromIsOptional()
38+
{
39+
$message = new Message($this->text, $this->to);
40+
41+
$params = $message->getParams();
42+
$this->assertArrayNotHasKey('from', $params);
43+
}
44+
45+
public function testCallback()
46+
{
47+
$this->message->setCallback('http://example.com');
48+
$params = $this->message->getParams();
49+
$this->assertArrayHasKey('callback', $params);
50+
$this->assertEquals('http://example.com', $params['callback']);
51+
$this->assertArrayNotHasKey('callback_method', $params);
52+
53+
$this->message->setCallback('http://example.com', 'POST');
54+
$params = $this->message->getParams();
55+
$this->assertArrayHasKey('callback', $params);
56+
$this->assertEquals('http://example.com', $params['callback']);
57+
$this->assertArrayHasKey('callback_method', $params);
58+
$this->assertEquals('POST', $params['callback_method']);
59+
60+
$this->message->setCallback('http://example.com');
61+
$params = $this->message->getParams();
62+
$this->assertArrayHasKey('callback', $params);
63+
$this->assertEquals('http://example.com', $params['callback']);
64+
$this->assertArrayNotHasKey('callback_method', $params);
65+
}
66+
67+
public function testMachine()
68+
{
69+
$this->message->setMachineDetection();
70+
$params = $this->message->getParams();
71+
$this->assertArrayHasKey('machine_detection', $params);
72+
$this->assertArrayNotHasKey('machine_timeout', $params);
73+
$this->assertEquals('hangup', $params['machine_detection']);
74+
75+
$this->message->setMachineDetection(true, 100);
76+
$params = $this->message->getParams();
77+
$this->assertArrayHasKey('machine_detection', $params);
78+
$this->assertArrayHasKey('machine_timeout', $params);
79+
$this->assertEquals('hangup', $params['machine_detection']);
80+
$this->assertEquals(100, $params['machine_timeout']);
81+
82+
$this->message->setMachineDetection(false);
83+
$params = $this->message->getParams();
84+
$this->assertArrayHasKey('machine_detection', $params);
85+
$this->assertArrayNotHasKey('machine_timeout', $params);
86+
$this->assertEquals('true', $params['machine_detection']);
87+
}
88+
89+
/**
90+
* @dataProvider optionalParams
91+
*/
92+
public function testOptionalParams($setter, $param, $values)
93+
{
94+
//check no default value
95+
$params = $this->message->getParams();
96+
$this->assertArrayNotHasKey($param, $params);
97+
98+
//test values
99+
foreach($values as $value => $expected){
100+
$this->message->$setter($value);
101+
$params = $this->message->getParams();
102+
$this->assertArrayHasKey($param, $params);
103+
$this->assertEquals($expected, $params[$param]);
104+
}
105+
}
106+
107+
public function optionalParams()
108+
{
109+
return array(
110+
array('setLanguage', 'lg', array('test' => 'test')),
111+
array('setVoice', 'voice', array('test' => 'test')),
112+
array('setRepeat', 'repeat', array(2 => 2)),
113+
);
114+
}
115+
21116
}
22117

0 commit comments

Comments
 (0)