[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/twilio-php/tests/ -> TwimlTest.php (source)

   1  <?php
   2  
   3  use \Mockery as m;
   4  
   5  require_once 'Twilio/Twiml.php';
   6  
   7  class TwimlTest extends PHPUnit_Framework_TestCase {
   8  
   9      function tearDown() {
  10          m::close();
  11      }
  12      
  13      function testEmptyResponse() {
  14          $r = new Services_Twilio_Twiml();
  15          $expected = '<Response></Response>';
  16          $this->assertXmlStringEqualsXmlString($expected, $r,
  17              "Should be an empty response");
  18      }
  19      
  20      public function testSayBasic() {   
  21          $r = new Services_Twilio_Twiml();
  22          $r->say("Hello Monkey");
  23          $expected = '<Response><Say>Hello Monkey</Say></Response>';
  24          $this->assertXmlStringEqualsXmlString($expected, $r);
  25      }
  26      
  27      public function testSayLoopThree() {
  28          $r = new Services_Twilio_Twiml();
  29          $r->say("Hello Monkey", array("loop" => 3));
  30          $expected = '<Response><Say loop="3">Hello Monkey</Say></Response>';
  31          $this->assertXmlStringEqualsXmlString($expected, $r);
  32      }
  33      
  34      public function testSayLoopThreeWoman() {
  35          $r = new Services_Twilio_Twiml();
  36          $r->say("Hello Monkey", array("loop" => 3, "voice"=>"woman"));
  37          $expected = '<Response><Say loop="3" voice="woman">'
  38              . 'Hello Monkey</Say></Response>';
  39          $this->assertXmlStringEqualsXmlString($expected, $r);
  40      }
  41      
  42      public function testSayConvienceMethod() {
  43          $r = new Services_Twilio_Twiml();
  44          $r->say("Hello Monkey", array("language" => "fr"));
  45          $expected = '<Response><Say language="fr">'
  46              . 'Hello Monkey</Say></Response>';
  47          $this->assertXmlStringEqualsXmlString($expected, $r);
  48      }
  49      
  50      public function testSayUTF8() {
  51          $r = new Services_Twilio_Twiml();
  52          $r->say("é tü & må");
  53          $expected = '<Response><Say>'
  54              . '&#xE9; t&#xFC; &amp; m&#xE5;</Say></Response>';
  55          $this->assertXmlStringEqualsXmlString($expected, $r);
  56      }
  57      
  58      public function testSayNamedEntities() {
  59          $r = new Services_Twilio_Twiml();
  60          $r->say("&eacute; t&uuml; &amp; m&aring;");
  61          $expected = '<Response><Say>'
  62              . '&#xE9; t&#xFC; &amp; m&#xE5;</Say></Response>';
  63          $this->assertXmlStringEqualsXmlString($expected, $r);
  64      }
  65      
  66      public function testSayNumericEntities() {
  67          $r = new Services_Twilio_Twiml();
  68          $r->say("&#xE9; t&#xFC; &amp; m&#xE5;");
  69          $expected = '<Response><Say>'
  70              . '&#xE9; t&#xFC; &amp; m&#xE5;</Say></Response>';
  71          $this->assertXmlStringEqualsXmlString($expected, $r);
  72      }
  73      
  74      public function testPlayBasic() {   
  75          $r = new Services_Twilio_Twiml();
  76          $r->play("hello-monkey.mp3");
  77          $expected = '<Response><Play>hello-monkey.mp3</Play></Response>';
  78          $this->assertXmlStringEqualsXmlString($expected, $r);
  79      }
  80      
  81      public function testPlayLoopThree() {
  82          $r = new Services_Twilio_Twiml();
  83          $r->play("hello-monkey.mp3", array("loop" => 3));
  84          $expected = '<Response><Play loop="3">'
  85              . 'hello-monkey.mp3</Play></Response>';
  86          $this->assertXmlStringEqualsXmlString($expected, $r);
  87      }
  88      
  89      public function testPlayConvienceMethod() {
  90          $r = new Services_Twilio_Twiml();
  91          $r->play("hello-monkey.mp3", array("loop" => 3));
  92          $expected = '<Response><Play loop="3">'
  93              . 'hello-monkey.mp3</Play></Response>';
  94          $this->assertXmlStringEqualsXmlString($expected, $r);
  95      }
  96  
  97      //Test Record Verb
  98      public function testRecord() {   
  99          $r = new Services_Twilio_Twiml();
 100          $r->record();
 101          $expected = '<Response><Record></Record></Response>';
 102          $this->assertXmlStringEqualsXmlString($expected, $r);
 103      }
 104      
 105      public function testRecordActionMethod() {   
 106          $r = new Services_Twilio_Twiml();
 107          $r->record(array("action" => "example.com", "method" => "GET"));
 108          $expected = '<Response><Record action="example.com" '
 109              . 'method="GET"></Record></Response>';
 110          $this->assertXmlStringEqualsXmlString($expected, $r);
 111      }
 112  
 113      public function testBooleanBecomesString() {   
 114          $r = new Services_Twilio_Twiml();
 115          $r->record(array("transcribe" => true));
 116          $expected = '<Response><Record transcribe="true" '
 117              . '></Record></Response>';
 118          $this->assertXmlStringEqualsXmlString($expected, $r);
 119      }
 120      
 121      public function testRecordMaxLengthKeyTimeout(){
 122          $r = new Services_Twilio_Twiml();
 123          $r->record(array("timeout" => 4, "finishOnKey" => "#", 
 124              "maxLength" => 30));
 125          $expected = '<Response><Record timeout="4" finishOnKey="#" '
 126              . 'maxLength="30"></Record></Response>';
 127          $this->assertXmlStringEqualsXmlString($expected, $r);
 128      }
 129      
 130      public function testRecordConvienceMethod(){
 131          $r = new Services_Twilio_Twiml();
 132          $r->record(array("transcribeCallback" => "example.com"));
 133          $expected = '<Response><Record '
 134              . 'transcribeCallback="example.com"></Record></Response>';
 135          $this->assertXmlStringEqualsXmlString($expected, $r);
 136      }
 137      
 138      public function testRecordAddAttribute(){
 139          $r = new Services_Twilio_Twiml();
 140          $r->record(array("foo" => "bar"));
 141          $expected = '<Response><Record foo="bar"></Record></Response>';
 142          $this->assertXmlStringEqualsXmlString($expected, $r);
 143      }
 144      
 145      //Test Redirect Verb
 146      public function testRedirect() {
 147          $r = new Services_Twilio_Twiml();
 148          $r->redirect();
 149          $expected = '<Response><Redirect></Redirect></Response>';
 150          $this->assertXmlStringEqualsXmlString($expected, $r);
 151      }
 152  
 153      public function testAmpersandEscaping() {
 154          $r = new Services_Twilio_Twiml();
 155          $test_amp = "test&two&amp;three";
 156          $r->redirect($test_amp);
 157          $expected = '<Response><Redirect>' .
 158              'test&amp;two&amp;three</Redirect></Response>';
 159          $this->assertXmlStringEqualsXmlString($expected, $r);
 160      }
 161  
 162      public function testRedirectConvience() {
 163          $r = new Services_Twilio_Twiml();
 164          $r->redirect();
 165          $expected = '<Response><Redirect></Redirect></Response>';
 166          $this->assertXmlStringEqualsXmlString($expected, $r);
 167      }
 168      public function testRedirectAddAttribute(){
 169          $r = new Services_Twilio_Twiml();
 170          $r->redirect(array("foo" => "bar"));
 171          $expected = '<Response><Redirect foo="bar"></Redirect></Response>';
 172          $this->assertXmlStringEqualsXmlString($expected, $r);
 173      }
 174  
 175      //Test Hangup Verb
 176      public function testHangup() {
 177          $r = new Services_Twilio_Twiml();
 178          $r->hangup();
 179          $expected = '<Response><Hangup></Hangup></Response>';
 180          $this->assertXmlStringEqualsXmlString($expected, $r);
 181      }
 182      
 183      public function testHangupConvience() {
 184          $r = new Services_Twilio_Twiml();
 185          $r->hangup();
 186          $expected = '<Response><Hangup></Hangup></Response>';
 187          $this->assertXmlStringEqualsXmlString($expected, $r);
 188      }
 189      
 190      public function testHangupAddAttribute(){
 191          $r = new Services_Twilio_Twiml();
 192          $r->hangup(array("foo" => "bar"));
 193          $expected = '<Response><Hangup foo="bar"></Hangup></Response>';
 194          $this->assertXmlStringEqualsXmlString($expected, $r);
 195      }
 196      
 197      //Test Pause Verb
 198      public function testPause() {
 199          $r = new Services_Twilio_Twiml();
 200          $r->pause();
 201          $expected = '<Response><Pause></Pause></Response>';
 202          $this->assertXmlStringEqualsXmlString($expected, $r);
 203      }
 204      
 205      public function testPauseConvience() {
 206          $r = new Services_Twilio_Twiml();
 207          $r->pause();
 208          $expected = '<Response><Pause></Pause></Response>';
 209          $this->assertXmlStringEqualsXmlString($expected, $r);
 210      }
 211      
 212      public function testPauseAddAttribute(){
 213          $r = new Services_Twilio_Twiml();
 214          $r->pause(array("foo" => "bar"));
 215          $expected = '<Response><Pause foo="bar"></Pause></Response>';
 216          $this->assertXmlStringEqualsXmlString($expected, $r);
 217      }
 218      
 219      //Test Dial Verb
 220      public function testDial() {
 221          $r = new Services_Twilio_Twiml();
 222          $r->dial("1231231234");
 223          $expected = '<Response><Dial>1231231234</Dial></Response>';
 224          $this->assertXmlStringEqualsXmlString($expected, $r);
 225      }
 226      
 227      public function testDialConvience() {
 228          $r = new Services_Twilio_Twiml();
 229          $r->dial();
 230          $expected = '<Response><Dial></Dial></Response>';
 231          $this->assertXmlStringEqualsXmlString($expected, $r);
 232      }
 233      
 234      public function testDialAddNumber() {
 235          $r = new Services_Twilio_Twiml();
 236          $d = $r->dial();
 237          $d->number("1231231234");
 238          $expected = '<Response><Dial><Number>'
 239              . '1231231234</Number></Dial></Response>';
 240          $this->assertXmlStringEqualsXmlString($expected, $r);
 241      }
 242      
 243      public function testDialAddConference() {
 244          $r = new Services_Twilio_Twiml();
 245          $d = $r->dial();
 246          $d->conference("MyRoom");
 247          $expected = '<Response><Dial><Conference>'
 248              . 'MyRoom</Conference></Dial></Response>';
 249          $this->assertXmlStringEqualsXmlString($expected, $r);
 250      }
 251      
 252      public function testDialAddConferenceConvience() {
 253          $r = new Services_Twilio_Twiml();
 254          $d = $r->dial();
 255          $d->conference("MyRoom", array("startConferenceOnEnter" => "false"));
 256          $expected = '<Response><Dial><Conference startConferenceOnEnter='
 257              . '"false">MyRoom</Conference></Dial></Response>';
 258          $this->assertXmlStringEqualsXmlString($expected, $r);
 259      }
 260      
 261      public function testDialAddAttribute() {
 262          $r = new Services_Twilio_Twiml();
 263          $r->dial(array("foo" => "bar"));
 264          $expected = '<Response><Dial foo="bar"></Dial></Response>';
 265          $this->assertXmlStringEqualsXmlString($expected, $r);
 266      }
 267      
 268      //Test Gather Verb
 269      public function testGather() {
 270          $r = new Services_Twilio_Twiml();
 271          $r->gather();
 272          $expected = '<Response><Gather></Gather></Response>';
 273          $this->assertXmlStringEqualsXmlString($expected, $r);
 274      }
 275      
 276      public function testGatherMethodAction(){
 277          $r = new Services_Twilio_Twiml();
 278          $r->gather(array("action"=>"example.com", "method"=>"GET"));
 279          $expected = '<Response><Gather action="example.com" '
 280              . 'method="GET"></Gather></Response>';
 281          $this->assertXmlStringEqualsXmlString($expected, $r);
 282      }
 283      
 284      public function testGatherActionWithParams(){
 285          $r = new Services_Twilio_Twiml(); 
 286          $r->gather(array("action" => "record.php?action=recordPageNow"
 287              . "&id=4&page=3")); 
 288          $expected = '<Response><Gather action="record.php?action='
 289              . 'recordPageNow&amp;id=4&amp;page=3"></Gather></Response>';
 290          $this->assertXmlStringEqualsXmlString($expected, $r);
 291      }
 292      
 293      public function testGatherNestedVerbs(){
 294          $r = new Services_Twilio_Twiml();
 295          $g = $r->gather(array("action"=>"example.com", "method"=>"GET"));
 296          $g->say("Hello World");
 297          $g->play("helloworld.mp3");
 298          $g->pause();
 299          $expected = '
 300              <Response>
 301                  <Gather action="example.com" method="GET">
 302                      <Say>Hello World</Say>
 303                      <Play>helloworld.mp3</Play>
 304                      <Pause></Pause>
 305                  </Gather>
 306              </Response>';
 307          $this->assertXmlStringEqualsXmlString($expected, $r);
 308      }
 309      
 310      public function testGatherNestedVerbsConvienceMethods(){
 311          $r = new Services_Twilio_Twiml();
 312          $g = $r->gather(array("action"=>"example.com", "method"=>"GET"));
 313          $g->say("Hello World");
 314          $g->play("helloworld.mp3");
 315          $g->pause();
 316          $expected = '
 317              <Response>
 318                  <Gather action="example.com" method="GET">
 319                      <Say>Hello World</Say>
 320                      <Play>helloworld.mp3</Play>
 321                      <Pause></Pause>
 322                  </Gather>
 323              </Response>';
 324          $this->assertXmlStringEqualsXmlString($expected, $r);
 325      }
 326      
 327      public function testGatherAddAttribute(){
 328          $r = new Services_Twilio_Twiml();
 329          $r->gather(array("foo" => "bar"));
 330          $expected = '<Response><Gather foo="bar"></Gather></Response>';
 331          $this->assertXmlStringEqualsXmlString($expected, $r);
 332      }
 333      
 334      public function testSms() {
 335          $r = new Services_Twilio_Twiml();
 336          $r->sms("Hello World");
 337          $expected = '<Response><Sms>Hello World</Sms></Response>';
 338          $this->assertXmlStringEqualsXmlString($expected, $r);
 339      }
 340      
 341      public function testSmsConvience() {
 342          $r = new Services_Twilio_Twiml();
 343          $r->sms("Hello World");
 344          $expected = '<Response><Sms>Hello World</Sms></Response>';
 345          $this->assertXmlStringEqualsXmlString($expected, $r);
 346      }
 347      
 348      public function testSmsAddAttribute() {
 349          $r = new Services_Twilio_Twiml();
 350          $r->sms(array("foo" => "bar"));
 351          $expected = '<Response><Sms foo="bar"></Sms></Response>';
 352          $this->assertXmlStringEqualsXmlString($expected, $r);
 353      }
 354      
 355      public function testReject() {
 356          $r = new Services_Twilio_Twiml();
 357          $r->reject();
 358          $expected = '<Response><Reject></Reject></Response>';
 359          $this->assertXmlStringEqualsXmlString($expected, $r);
 360      }
 361  
 362      function testGeneration() {
 363  
 364          $r = new Services_Twilio_Twiml();
 365          $r->say('hello');
 366          $r->dial()->number('123', array('sendDigits' => '456'));
 367          $r->gather(array('timeout' => 15));
 368  
 369          $doc = simplexml_load_string($r);
 370          $this->assertEquals('Response', $doc->getName());
 371          $this->assertEquals('hello', (string) $doc->Say);
 372          $this->assertEquals('456', (string) $doc->Dial->Number['sendDigits']);
 373          $this->assertEquals('123', (string) $doc->Dial->Number);
 374          $this->assertEquals('15', (string) $doc->Gather['timeout']);
 375      }
 376  
 377  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1