PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/restxmlTest.php

https://github.com/leoken/plivohelper-php
PHP | 385 lines | 303 code | 48 blank | 34 comment | 0 complexity | f0ec2ee1ecb42c6b0fd7d4c896e87a10 MD5 | raw file
  1. <?php
  2. /*
  3. To run this test suit, you will need to install PHPUnit
  4. for futher information : http://pear.phpunit.de/
  5. On Ubuntu 11.04
  6. sudo apt-get remove phpunit
  7. sudo pear channel-discover pear.phpunit.de
  8. sudo pear channel-discover pear.symfony-project.com
  9. sudo pear channel-discover components.ez.no
  10. sudo pear update-channels
  11. sudo pear upgrade-all
  12. sudo pear install --alldeps phpunit/PHPUnit
  13. On Debian
  14. pear uninstall phpunit/PHPUnit
  15. pear channel-discover components.ez.no
  16. pear channel-discover pear.symfony-project.com
  17. pear install phpunit/PHPUnit
  18. Then run the test suit :
  19. phpunit --verbose restxmlTest.php
  20. */
  21. // Testing Requires PHPUnit
  22. #require_once 'PHPUnit/Framework.php';
  23. require_once 'PHPUnit/Autoload.php';
  24. require_once '../plivohelper.php';
  25. class RESTXMLTest extends PHPUnit_Framework_TestCase
  26. {
  27. public function addBadAttribute($verb){
  28. $r = new $verb($attr = array("foo" => "bar"));
  29. }
  30. // Test Response Element
  31. public function testResponseEmpty(){
  32. $r = new Response();
  33. $expected = '<Response></Response>';
  34. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False),
  35. "Should be an empty response");
  36. }
  37. // Test Speak Element
  38. public function testSpeakBasic() {
  39. $r = new Response();
  40. $r->append(new Speak("Hello Monkey"));
  41. $expected = '<Response><Speak>Hello Monkey</Speak></Response>';
  42. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  43. }
  44. public function testSpeakLoopThree() {
  45. $r = new Response();
  46. $r->append(new Speak("Hello Monkey", array("loop" => 3)));
  47. $expected = '<Response><Speak loop="3">Hello Monkey</Speak></Response>';
  48. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  49. }
  50. public function testSpeakLoopThreeWoman() {
  51. $r = new Response();
  52. $r->append(new Speak("Hello Monkey", array("loop" => 3, "voice"=>"woman")));
  53. $expected = '<Response><Speak loop="3" voice="woman">Hello Monkey</Speak></Response>';
  54. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  55. }
  56. public function testSpeakConvienceMethod() {
  57. $r = new Response();
  58. $r->addSpeak("Hello Monkey", array("language" => "fr"));
  59. $expected = '<Response><Speak language="fr">Hello Monkey</Speak></Response>';
  60. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  61. }
  62. public function testSpeakBadAppend() {
  63. $this->setExpectedException('PlivoException');
  64. $s = new Speak();
  65. $s->append(new Dial());
  66. }
  67. public function testSpeakAddBadAttribute(){
  68. $this->setExpectedException('PlivoException');
  69. $this->addBadAttribute("Speak");
  70. }
  71. //Test Play Element
  72. public function testPlayBasic() {
  73. $r = new Response();
  74. $r->append(new Play("hello-monkey.mp3"));
  75. $expected = '<Response><Play>hello-monkey.mp3</Play></Response>';
  76. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  77. }
  78. public function testPlayLoopThree() {
  79. $r = new Response();
  80. $r->append(new Play("hello-monkey.mp3", array("loop" => 3)));
  81. $expected = '<Response><Play loop="3">hello-monkey.mp3</Play></Response>';
  82. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  83. }
  84. public function testPlayConvienceMethod() {
  85. $r = new Response();
  86. $r->addPlay("hello-monkey.mp3", array("loop" => 3));
  87. $expected = '<Response><Play loop="3">hello-monkey.mp3</Play></Response>';
  88. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  89. }
  90. public function testPlayBadAppend() {
  91. $this->setExpectedException('PlivoException');
  92. $p = new Play();
  93. $p->append(new Dial());
  94. }
  95. public function testPlayAddBadAttribute(){
  96. $this->setExpectedException('PlivoException');
  97. $this->addBadAttribute("Play");
  98. }
  99. //Test Record Element
  100. public function testRecord() {
  101. $r = new Response();
  102. $r->append(new Record());
  103. $expected = '<Response><Record></Record></Response>';
  104. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  105. }
  106. public function testRecordMaxLengthKeyTimeout(){
  107. $r = new Response();
  108. $r->append(new Record(array("timeout" => 4, "finishOnKey" => "#", "maxLength" => 30)));
  109. $expected = '<Response><Record timeout="4" finishOnKey="#" maxLength="30"></Record></Response>';
  110. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  111. }
  112. public function testRecordAddAttribute(){
  113. $r = new Response();
  114. $re = new Record();
  115. $re->set("foo", "bar");
  116. $r->append($re);
  117. $expected = '<Response><Record foo="bar"></Record></Response>';
  118. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  119. }
  120. public function testRecordBadAppend() {
  121. $this->setExpectedException('PlivoException');
  122. $r = new Record();
  123. $r->append(new Dial());
  124. }
  125. public function testRecordAddBadAttribute(){
  126. $this->setExpectedException('PlivoException');
  127. $this->addBadAttribute("Record");
  128. }
  129. //Test Redirect Element
  130. public function testRedirect() {
  131. $r = new Response();
  132. $r->append(new Redirect());
  133. $expected = '<Response><Redirect></Redirect></Response>';
  134. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  135. }
  136. public function testRedirectConvience() {
  137. $r = new Response();
  138. $r->addRedirect();
  139. $expected = '<Response><Redirect></Redirect></Response>';
  140. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  141. }
  142. public function testRedirectAddAttribute(){
  143. $r = new Response();
  144. $re = new Redirect();
  145. $re->set("foo", "bar");
  146. $r->append($re);
  147. $expected = '<Response><Redirect foo="bar"></Redirect></Response>';
  148. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  149. }
  150. public function testRedirectBadAppend() {
  151. $this->setExpectedException('PlivoException');
  152. $r = new Redirect();
  153. $r->append(new Dial());
  154. }
  155. public function testRedirectAddBadAttribute(){
  156. $this->setExpectedException('PlivoException');
  157. $this->addBadAttribute("Redirect");
  158. }
  159. //Test Hangup Element
  160. public function testHangup() {
  161. $r = new Response();
  162. $r->append(new Hangup());
  163. $expected = '<Response><Hangup></Hangup></Response>';
  164. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  165. }
  166. public function testHangupConvience() {
  167. $r = new Response();
  168. $r->addHangup();
  169. $expected = '<Response><Hangup></Hangup></Response>';
  170. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  171. }
  172. public function testHangupAddAttribute(){
  173. $r = new Response();
  174. $re = new Hangup();
  175. $re->set("foo", "bar");
  176. $r->append($re);
  177. $expected = '<Response><Hangup foo="bar"></Hangup></Response>';
  178. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  179. }
  180. public function testHangupBadAppend() {
  181. $this->setExpectedException('PlivoException');
  182. $r = new Hangup();
  183. $r->append(new Dial());
  184. }
  185. //Test Wait Element
  186. public function testWait() {
  187. $r = new Response();
  188. $r->append(new Wait());
  189. $expected = '<Response><Wait></Wait></Response>';
  190. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  191. }
  192. public function testWaitConvience() {
  193. $r = new Response();
  194. $r->addWait();
  195. $expected = '<Response><Wait></Wait></Response>';
  196. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  197. }
  198. public function testWaitAddAttribute(){
  199. $r = new Response();
  200. $re = new Wait();
  201. $re->set("foo", "bar");
  202. $r->append($re);
  203. $expected = '<Response><Wait foo="bar"></Wait></Response>';
  204. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  205. }
  206. public function testWaitBadAppend() {
  207. $this->setExpectedException('PlivoException');
  208. $r = new Wait();
  209. $r->append(new Dial());
  210. }
  211. public function testWaitAddBadAttribute(){
  212. $this->setExpectedException('PlivoException');
  213. new Wait(array("foo" => "bar"));
  214. }
  215. //Test Dial Element
  216. public function testDial() {
  217. $r = new Response();
  218. $r->append(new Dial("1231231234"));
  219. $expected = '<Response><Dial>1231231234</Dial></Response>';
  220. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  221. }
  222. public function testDialConvience() {
  223. $r = new Response();
  224. $r->addDial();
  225. $expected = '<Response><Dial></Dial></Response>';
  226. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  227. }
  228. public function testDialAddNumber() {
  229. $r = new Response();
  230. $d = $r->append(new Dial());
  231. $d->append(new Number("1231231234"));
  232. $expected = '<Response><Dial><Number>1231231234</Number></Dial></Response>';
  233. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  234. }
  235. public function testDialAddConference() {
  236. $r = new Response();
  237. $r->append(new Conference("MyRoom"));
  238. $expected = '<Response><Conference>MyRoom</Conference></Response>';
  239. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  240. }
  241. public function testDialAddConferenceConvience() {
  242. $r = new Response();
  243. $r->addConference("MyRoom", array("startConferenceOnEnter" => "false"));
  244. $expected = '<Response><Conference startConferenceOnEnter="false">MyRoom</Conference></Response>';
  245. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  246. }
  247. public function testDialAddAttribute(){
  248. $r = new Response();
  249. $re = new Dial();
  250. $re->set("foo", "bar");
  251. $r->append($re);
  252. $expected = '<Response><Dial foo="bar"></Dial></Response>';
  253. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  254. }
  255. public function testDialBadAppend() {
  256. $this->setExpectedException('PlivoException');
  257. $r = new Dial();
  258. $r->append(new Wait());
  259. }
  260. public function testDialAddBadAttribute(){
  261. $this->setExpectedException('PlivoException');
  262. new Dial(array("foo" => "bar"));
  263. }
  264. //Test GetDigits Element
  265. public function testGetDigits(){
  266. $r = new Response();
  267. $r->append(new GetDigits());
  268. $expected = '<Response><GetDigits></GetDigits></Response>';
  269. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  270. }
  271. public function testGetDigitsMethodAction(){
  272. $r = new Response();
  273. $r->append(new GetDigits(array("action"=>"example.com", "method"=>"GET")));
  274. $expected = '<Response><GetDigits action="example.com" method="GET"></GetDigits></Response>';
  275. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  276. }
  277. public function testGetDigitsActionWithParams(){
  278. $r = new Response();
  279. $r->append(new GetDigits(array("action" => "record.php?action=recordPageNow&id=4&page=3")));
  280. $expected = '<Response><GetDigits action="record.php?action=recordPageNow&amp;id=4&amp;page=3"></GetDigits></Response>';
  281. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  282. }
  283. public function testGetDigitsNestedElements(){
  284. $r = new Response();
  285. $g = $r->append(new GetDigits(array("action"=>"example.com", "method"=>"GET")));
  286. $g->append(new Speak("Hello World"));
  287. $g->append(new Play("helloworld.mp3"));
  288. $g->append(new Wait());
  289. $expected = '
  290. <Response>
  291. <GetDigits action="example.com" method="GET">
  292. <Speak>Hello World</Speak>
  293. <Play>helloworld.mp3</Play>
  294. <Wait></Wait>
  295. </GetDigits>
  296. </Response>';
  297. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  298. }
  299. public function testGetDigitsNestedElementsConvienceMethods(){
  300. $r = new Response();
  301. $g = $r->addGetDigits(array("action"=>"example.com", "method"=>"GET"));
  302. $g->addSpeak("Hello World");
  303. $g->addPlay("helloworld.mp3");
  304. $g->addWait();
  305. $expected = '
  306. <Response>
  307. <GetDigits action="example.com" method="GET">
  308. <Speak>Hello World</Speak>
  309. <Play>helloworld.mp3</Play>
  310. <Wait></Wait>
  311. </GetDigits>
  312. </Response>';
  313. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  314. }
  315. public function testGetDigitsAddAttribute(){
  316. $r = new Response();
  317. $re = new GetDigits();
  318. $re->set("foo", "bar");
  319. $r->append($re);
  320. $expected = '<Response><GetDigits foo="bar"></GetDigits></Response>';
  321. $this->assertXmlStringEqualsXmlString($expected, $r->asUrl(False));
  322. }
  323. public function testGetDigitsBadAppend() {
  324. $this->setExpectedException('PlivoException');
  325. $r = new GetDigits();
  326. $r->append(new Conference());
  327. }
  328. public function testGetDigitsAddBadAttribute(){
  329. $this->setExpectedException('PlivoException');
  330. new GetDigits(array("foo" => "bar"));
  331. }
  332. }
  333. ?>