PageRenderTime 26ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mock/Test_Mock.php

https://github.com/marcelog/PAGI
PHP | 317 lines | 201 code | 14 blank | 102 comment | 0 complexity | 7ea9495ca75391c3fecc3af456ac34e8 MD5 | raw file
  1. <?php
  2. /**
  3. * This class will test the agi client mock
  4. *
  5. * PHP Version 5
  6. *
  7. * @category Pagi
  8. * @package Test
  9. * @subpackage Mock
  10. * @author Marcelo Gornstein <marcelog@gmail.com>
  11. * @license http://marcelog.github.com/ Apache License 2.0
  12. * @version SVN: $Id$
  13. * @link http://marcelog.github.com/
  14. *
  15. * Copyright 2011 Marcelo Gornstein <marcelog@gmail.com>
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License");
  18. * you may not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS,
  25. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *
  29. */
  30. use PAGI\Node\MockedNode;
  31. use PAGI\Node\Node;
  32. /**
  33. * This class will test the agi client mock
  34. *
  35. * PHP Version 5
  36. *
  37. * @category Pagi
  38. * @package Test
  39. * @subpackage Mock
  40. * @author Marcelo Gornstein <marcelog@gmail.com>
  41. * @license http://marcelog.github.com/ Apache License 2.0
  42. * @link http://marcelog.github.com/
  43. */
  44. class Test_Mock extends PHPUnit_Framework_TestCase
  45. {
  46. private $_properties = array();
  47. public function setUp()
  48. {
  49. $this->_properties = array(
  50. 'variables' => array(),
  51. 'resultStrings' => array()
  52. );
  53. }
  54. /**
  55. * @test
  56. * @expectedException PAGI\Exception\MockedException
  57. */
  58. public function cannot_respond_if_no_onMethod_was_defined_first()
  59. {
  60. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  61. $result = $mock->streamFile('blah', '01234567890*#');
  62. }
  63. /**
  64. * @test
  65. * @expectedException PAGI\Exception\MockedException
  66. */
  67. public function can_assert_number_of_arguments()
  68. {
  69. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  70. $mock->assert('streamFile', array('blah', '*'));
  71. $mock->streamFile('blah');
  72. }
  73. /**
  74. * @test
  75. * @expectedException PAGI\Exception\MockedException
  76. */
  77. public function can_assert_arguments_equality()
  78. {
  79. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  80. $mock->assert('waitDigit', array(1000));
  81. $mock->waitDigit(100);
  82. }
  83. /**
  84. * @test
  85. * @expectedException PAGI\Exception\MockedException
  86. */
  87. public function cannot_finish_without_using_all_results()
  88. {
  89. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  90. $mock->onDial(true, 'name', '123456', 20, 'ANSWER', '#blah');
  91. unset($mock);
  92. }
  93. /**
  94. * @test
  95. * @expectedException PAGI\Exception\MockedException
  96. */
  97. public function cannot_finish_without_using_all_asserts()
  98. {
  99. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  100. $mock->assert('waitDigit', array(1));
  101. unset($mock);
  102. }
  103. /**
  104. * @test
  105. */
  106. public function can_dial_success()
  107. {
  108. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  109. $mock->onDial(true, 'name', '123456', 20, 'ANSWER', '#blah');
  110. $result = $mock->dial('SIP/blah', array(60, 'tH'));
  111. $this->assertTrue($result->isAnswer());
  112. $this->assertTrue($result->isResult(0));
  113. }
  114. /**
  115. * @test
  116. */
  117. public function can_dial_failed()
  118. {
  119. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  120. $mock->onDial(false, 'name', '123456', 20, 'CONGESTION', '#blah');
  121. $result = $mock->dial('SIP/blah', array(60, 'tH'));
  122. $this->assertTrue($result->isCongestion());
  123. $this->assertTrue($result->isResult(-1));
  124. }
  125. /**
  126. * @test
  127. */
  128. public function can_get_full_variable_failed()
  129. {
  130. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  131. $mock->onGetFullVariable(false);
  132. $this->assertFalse($mock->getFullVariable('whatever'));
  133. }
  134. /**
  135. * @test
  136. */
  137. public function can_get_full_variable_success()
  138. {
  139. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  140. $mock->onGetFullVariable(true, 'value');
  141. $this->assertEquals($mock->getFullVariable('whatever'), 'value');
  142. }
  143. /**
  144. * @test
  145. */
  146. public function can_get_variable_failed()
  147. {
  148. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  149. $mock->onGetVariable(false);
  150. $this->assertFalse($mock->getVariable('whatever'));
  151. }
  152. /**
  153. * @test
  154. */
  155. public function can_get_variable_success()
  156. {
  157. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  158. $mock->onGetVariable(true, 'value');
  159. $this->assertEquals($mock->getVariable('whatever'), 'value');
  160. }
  161. /**
  162. * @test
  163. */
  164. public function can_record_with_hangup()
  165. {
  166. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  167. $mock->onRecord(false, true, '#', 0);
  168. $result = $mock->record('blah', 'wav', '#');
  169. $this->assertTrue($result->isHangup());
  170. $this->assertTrue($result->isInterrupted());
  171. }
  172. /**
  173. * @test
  174. */
  175. public function can_record_with_interrupt()
  176. {
  177. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  178. $mock->onRecord(true, false, '#', 0);
  179. $result = $mock->record('blah', 'wav', '#');
  180. $this->assertFalse($result->isHangup());
  181. $this->assertTrue($result->isInterrupted());
  182. $this->assertEquals($result->getDigits(), '#');
  183. }
  184. /**
  185. * @test
  186. */
  187. public function can_get_option_without_interrupt()
  188. {
  189. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  190. $mock->onGetOption(false, '#', 1000);
  191. $result = $mock->getOption('blah', '#', 1000);
  192. $this->assertTrue($result->isTimeout());
  193. }
  194. /**
  195. * @test
  196. */
  197. public function can_get_option_with_interrupt()
  198. {
  199. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  200. $mock->onGetOption(true, '#', 1000);
  201. $result = $mock->getOption('blah', '#', 1000);
  202. $this->assertFalse($result->isTimeout());
  203. $this->assertEquals($result->getDigits(), '#');
  204. }
  205. /**
  206. * @test
  207. */
  208. public function can_record_with_timeout()
  209. {
  210. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  211. $mock->onRecord(false, false, '#', 0);
  212. $result = $mock->record('blah', 'wav', '#');
  213. $this->assertFalse($result->isHangup());
  214. $this->assertFalse($result->isInterrupted());
  215. }
  216. /**
  217. * @test
  218. */
  219. public function can_example_mock()
  220. {
  221. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  222. $mock
  223. ->assert('waitDigit', array(1000))
  224. ->assert('streamFile', array('blah', '01234567890*#'))
  225. ->assert('getData', array('blah', 123, '#'))
  226. ->assert('sayDateTime', array('asd', 123))
  227. ->assert('setVariable', array('asd', 'asd'))
  228. ->assert('setCallerId', array('name', 'number'))
  229. ->assert('playBusyTone')
  230. ->assert('playDialTone')
  231. ->assert('playCongestionTone')
  232. ->assert('stopPlayingTones')
  233. ->assert('playTone', array('some'))
  234. ->assert('playCustomTones', array(1, 2, 3))
  235. ->assert('amd', array())
  236. ->onAnswer(true)
  237. ->onWaitDigit(false)
  238. ->onWaitDigit(true, '*')
  239. ->onStreamFile(false)
  240. ->onStreamFile(true, '#')
  241. ->onGetData(false)
  242. ->onGetData(true, '44449*#')
  243. ->onSayDate(true, '#')
  244. ->onSayTime(true, '#')
  245. ->onSayDateTime(true, '#')
  246. ->onSayAlpha(true, '#')
  247. ->onSayPhonetic(true, '#')
  248. ->onSayNumber(true, '#')
  249. ->onSayDigits(true, '#')
  250. ->onAmd('status', 'cause')
  251. ->onHangup(true)
  252. ->onChannelStatus(PAGI\Client\ChannelStatus::LINE_UP)
  253. ;
  254. $mock->answer();
  255. $mock->waitDigit(1000);
  256. $mock->waitDigit(1000);
  257. $mock->streamFile('blah', '01234567890*#');
  258. $mock->streamFile('blah', '01234567890*#');
  259. $mock->getData('blah', 123, '#');
  260. $mock->getData('blah', 123, '#');
  261. $mock->sayDate('asd', time());
  262. $mock->sayTime('asd', time());
  263. $mock->sayDateTime('asd', 123);
  264. $mock->sayAlpha('asd');
  265. $mock->sayPhonetic('asd');
  266. $mock->sayNumber(123);
  267. $mock->sayDigits(123);
  268. $mock->amd();
  269. $mock->hangup();
  270. $mock->channelStatus();
  271. $mock->setCallerId('name', 'number');
  272. $mock->playBusyTone();
  273. $mock->playDialTone();
  274. $mock->playCongestionTone();
  275. $mock->stopPlayingTones();
  276. $mock->playTone('some');
  277. $mock->playCustomTones(array(1, 2, 3));
  278. $mock->consoleLog("blah");
  279. $mock->log("blah");
  280. $mock->setVariable('asd', 'asd');
  281. $mock->setContext('context');
  282. $mock->setExtension('extension');
  283. $mock->setPriority(1);
  284. $mock->setMusic(true);
  285. }
  286. /**
  287. * @test
  288. */
  289. public function can_get_unknown_node()
  290. {
  291. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  292. $this->assertTrue($mock->createNode('test') instanceof Node);
  293. }
  294. /**
  295. * @test
  296. */
  297. public function can_get_knownmocked_node()
  298. {
  299. $mock = new PAGI\Client\Impl\MockedClientImpl($this->_properties);
  300. $mock->onCreateNode('test')->runWithInput('*');
  301. $this->assertTrue($mock->createNode('test') instanceof MockedNode);
  302. }
  303. }