Test_CallSpool.php
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/**
* This class will test the call spool.
*
* PHP Version 5
*
* @category Pagi
* @package Test
* @subpackage Spool
* @author Marcelo Gornstein <[email protected]>
* @license http://marcelog.github.com/ Apache License 2.0
* @version SVN: $Id$
* @link http://marcelog.github.com/
*
* Copyright 2011 Marcelo Gornstein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace {
$mockFopen = false;
$mockTouch = false;
$mockRename = false;
$mockPutContents = false;
}
namespace PAGI\CallSpool\Impl {
function tempnam() {
global $mockTempnam;
if (isset($mockTempnam) && $mockTempnam === true) {
return false;
} else {
return call_user_func_array('\tempnam', func_get_args());
}
}
function rename() {
global $mockRename;
if (isset($mockRename) && $mockRename === true) {
return false;
} else {
return call_user_func_array('\rename', func_get_args());
}
}
function touch() {
global $mockTouch;
if (isset($mockTouch) && $mockTouch === true) {
return false;
} else {
return call_user_func_array('\touch', func_get_args());
}
}
function file_put_contents() {
global $mockPutContents;
if (isset($mockPutContents) && $mockPutContents === true) {
return false;
} else {
return call_user_func_array('\file_put_contents', func_get_args());
}
}
/**
* This class will test the call spool.
*
* PHP Version 5
*
* @category Pagi
* @package Test
* @subpackage Spool
* @author Marcelo Gornstein <[email protected]>
* @license http://marcelog.github.com/ Apache License 2.0
* @link http://marcelog.github.com/
*/
class Test_CallSpool extends \PHPUnit_Framework_TestCase
{
private $_properties = array();
public function setUp()
{
global $mockTempnam;
global $mockPutContents;
global $mockTouch;
global $mockRename;
$mockTouch = false;
$mockRename = false;
$mockTempnam = false;
$mockPutContents = false;
$this->_properties = array();
}
/**
* @test
*/
public function can_create_callfile()
{
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$this->assertFalse($call->getArchive());
$call->setArchive(true);
$call->setContext('context');
$call->setPriority('priority');
$call->setExtension('extension');
$call->setApplication('application');
$call->setAlwaysDelete(true);
$call->setCallerId('callerId');
$call->setMaxRetries(33);
$call->setRetryTime(44);
$call->setWaitTime(22);
$call->setVariable('var', 'value');
$call->setAccount('account');
$this->assertFalse($call->getApplicationData());
$call->setApplicationData(array('a', 'b', 'c'));
$this->assertTrue($call->getArchive());
$this->assertEquals($call->getContext(), 'context');
$this->assertEquals($call->getPriority(), 'priority');
$this->assertEquals($call->getApplication(), 'application');
$this->assertEquals($call->getExtension(), 'extension');
$this->assertEquals($call->getMaxRetries(), 33);
$this->assertEquals($call->getRetryTime(), 44);
$this->assertEquals($call->getWaitTime(), 22);
$this->assertTrue($call->getAlwaysDelete());
$this->assertEquals($call->getCallerId(), 'callerId');
$this->assertEquals($call->getAccount(), 'account');
$this->assertEquals($call->getChannel(), 'SIP/target@provider');
$this->assertEquals($call->getVariable('var'), 'value');
$this->assertFalse($call->getVariable('var2'));
$this->assertEquals($call->getApplicationData(), array('a', 'b', 'c'));
$this->assertEquals($call->serialize(), <<<TEXT
Channel: SIP/target@provider
Archive: Yes
Context: context
Priority: priority
Extension: extension
Application: application
AlwaysDelete: Yes
CallerID: callerId
MaxRetries: 33
RetryTime: 44
WaitTime: 22
Account: account
Data: a,b,c
Set: var=value
TEXT
);
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$call->unserialize(<<<TEXT
Channel: SIP/target@provider
Archive: Yes
Context: context
Priority: priority
Extension: extension
Application: application
AlwaysDelete: Yes
CallerID: callerId
MaxRetries: 33
RetryTime: 44
WaitTime: 22
Account: account
Data: a,b,c
aaaa:
Set: var=value
Set: var2=
TEXT
);
$this->assertEquals($call->serialize(), <<<TEXT
Channel: SIP/target@provider
Archive: Yes
Context: context
Priority: priority
Extension: extension
Application: application
AlwaysDelete: Yes
CallerID: callerId
MaxRetries: 33
RetryTime: 44
WaitTime: 22
Account: account
Data: a,b,c
aaaa: ?
Set: var=value
Set: var2=?
TEXT
);
}
/**
* @test
*/
public function can_get_instance()
{
$props = array(
'tmpDir' => '/tmp',
'spoolDir' => '/tmp'
);
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props);
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props); // should return the same instance.
}
/**
* @test
* @expectedException \PAGI\CallSpool\Exception\CallSpoolException
*/
public function cannot_create_temp_file()
{
global $mockTempnam;
$mockTempnam = true;
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . microtime(true);
$spoolDir = $tmpDir . DIRECTORY_SEPARATOR . 'spool';
$props = array('tmpDir' => $tmpDir, 'spoolDir' => $spoolDir);
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props);
$spool->spool($call);
}
/**
* @test
* @expectedException \PAGI\CallSpool\Exception\CallSpoolException
*/
public function cannot_write_temp_file()
{
global $mockPutContents;
global $mockTempnam;
$mockTempnam = false;
$mockPutContents = true;
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . microtime(true);
$spoolDir = $tmpDir . DIRECTORY_SEPARATOR . 'spool';
if (@mkdir($tmpDir, 0755, true) === false) {
$this->fail('Could not create temporary directory');
}
if (@mkdir($spoolDir . DIRECTORY_SEPARATOR . 'outgoing', 0755, true) === false) {
$this->fail('Could not create temporary spool directory');
}
$props = array('tmpDir' => $tmpDir, 'spoolDir' => $spoolDir);
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props);
$spool->spool($call);
}
/**
* @test
* @expectedException \PAGI\CallSpool\Exception\CallSpoolException
*/
public function cannot_touch_temp_file()
{
global $mockTouch;
$mockTouch = true;
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . microtime(true);
$spoolDir = $tmpDir . DIRECTORY_SEPARATOR . 'spool';
if (@mkdir($tmpDir, 0755, true) === false) {
$this->fail('Could not create temporary directory');
}
if (@mkdir($spoolDir . DIRECTORY_SEPARATOR . 'outgoing', 0755, true) === false) {
$this->fail('Could not create temporary spool directory');
}
$props = array('tmpDir' => $tmpDir, 'spoolDir' => $spoolDir);
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props);
$spool->spool($call, 12);
}
/**
* @test
* @expectedException \PAGI\CallSpool\Exception\CallSpoolException
*/
public function cannot_move_file_to_spool()
{
global $mockRename;
$mockRename = true;
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . microtime(true);
$spoolDir = $tmpDir . DIRECTORY_SEPARATOR . 'spool';
if (@mkdir($tmpDir, 0755, true) === false) {
$this->fail('Could not create temporary directory');
}
if (@mkdir($spoolDir . DIRECTORY_SEPARATOR . 'outgoing', 0755, true) === false) {
$this->fail('Could not create temporary spool directory');
}
$props = array('tmpDir' => $tmpDir, 'spoolDir' => $spoolDir);
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props);
$spool->spool($call, 12);
}
/**
* @test
*/
public function can_spool_and_schedule()
{
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . microtime(true);
$spoolDir = $tmpDir . DIRECTORY_SEPARATOR . 'spool';
if (@mkdir($tmpDir, 0755, true) === false) {
$this->fail('Could not create temporary directory');
}
if (@mkdir($spoolDir . DIRECTORY_SEPARATOR . 'outgoing', 0755, true) === false) {
$this->fail('Could not create temporary spool directory');
}
$props = array('tmpDir' => $tmpDir, 'spoolDir' => $spoolDir);
$spool = \PAGI\CallSpool\Impl\CallSpoolImpl::getInstance($props);
$call = new \PAGI\CallSpool\CallFile(new \PAGI\DialDescriptor\SIPDialDescriptor('target', 'provider'));
$call->unserialize(<<<TEXT
Channel: SIP/target@provider
Archive: Yes
Context: context
Priority: priority
Extension: extension
Application: application
AlwaysDelete: Yes
CallerID: callerId
MaxRetries: 33
RetryTime: 44
WaitTime: 22
Account: account
Data: a,b,c
aaaa:
Set: var=value
Set: var2=
TEXT
);
$file = $spool->spool($call, 12);
$content = <<<TEXT
Channel: SIP/target@provider
Archive: Yes
Context: context
Priority: priority
Extension: extension
Application: application
AlwaysDelete: Yes
CallerID: callerId
MaxRetries: 33
RetryTime: 44
WaitTime: 22
Account: account
Data: a,b,c
aaaa: ?
Set: var=value
Set: var2=?
TEXT
;
$this->assertEquals(file_get_contents($file), $content);
$this->assertEquals(filemtime($file), 12);
if (@unlink($file) === false) {
$this->fail('could not remove call file: ' . $file);
}
if (@rmdir($spoolDir . DIRECTORY_SEPARATOR . 'outgoing') === false) {
$this->fail('could not remove temporary spool outgoing directory');
}
if (@rmdir($spoolDir) === false) {
$this->fail('could not remove temporary spool directory');
}
if (@rmdir($tmpDir) === false) {
$this->fail('could not remove temporary directory');
}
}
}
}