TwimlTest.php 13.6 KB
Newer Older
Juliper committed
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
<?php

use \Mockery as m;

require_once 'Twilio/Twiml.php';

class TwimlTest extends PHPUnit_Framework_TestCase {

    function tearDown() {
        m::close();
    }
    
    function testEmptyResponse() {
        $r = new Services_Twilio_Twiml();
        $expected = '<Response></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r,
            "Should be an empty response");
    }
    
    public function testSayBasic() {   
        $r = new Services_Twilio_Twiml();
        $r->say("Hello Monkey");
        $expected = '<Response><Say>Hello Monkey</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSayLoopThree() {
        $r = new Services_Twilio_Twiml();
        $r->say("Hello Monkey", array("loop" => 3));
        $expected = '<Response><Say loop="3">Hello Monkey</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSayLoopThreeWoman() {
        $r = new Services_Twilio_Twiml();
        $r->say("Hello Monkey", array("loop" => 3, "voice"=>"woman"));
        $expected = '<Response><Say loop="3" voice="woman">'
            . 'Hello Monkey</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSayConvienceMethod() {
        $r = new Services_Twilio_Twiml();
        $r->say("Hello Monkey", array("language" => "fr"));
        $expected = '<Response><Say language="fr">'
            . 'Hello Monkey</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSayUTF8() {
        $r = new Services_Twilio_Twiml();
        $r->say("é tü & må");
        $expected = '<Response><Say>'
            . '&#xE9; t&#xFC; &amp; m&#xE5;</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSayNamedEntities() {
        $r = new Services_Twilio_Twiml();
        $r->say("&eacute; t&uuml; &amp; m&aring;");
        $expected = '<Response><Say>'
            . '&#xE9; t&#xFC; &amp; m&#xE5;</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSayNumericEntities() {
        $r = new Services_Twilio_Twiml();
        $r->say("&#xE9; t&#xFC; &amp; m&#xE5;");
        $expected = '<Response><Say>'
            . '&#xE9; t&#xFC; &amp; m&#xE5;</Say></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testPlayBasic() {   
        $r = new Services_Twilio_Twiml();
        $r->play("hello-monkey.mp3");
        $expected = '<Response><Play>hello-monkey.mp3</Play></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testPlayLoopThree() {
        $r = new Services_Twilio_Twiml();
        $r->play("hello-monkey.mp3", array("loop" => 3));
        $expected = '<Response><Play loop="3">'
            . 'hello-monkey.mp3</Play></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testPlayConvienceMethod() {
        $r = new Services_Twilio_Twiml();
        $r->play("hello-monkey.mp3", array("loop" => 3));
        $expected = '<Response><Play loop="3">'
            . 'hello-monkey.mp3</Play></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }

    //Test Record Verb
    public function testRecord() {   
        $r = new Services_Twilio_Twiml();
        $r->record();
        $expected = '<Response><Record></Record></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testRecordActionMethod() {   
        $r = new Services_Twilio_Twiml();
        $r->record(array("action" => "example.com", "method" => "GET"));
        $expected = '<Response><Record action="example.com" '
            . 'method="GET"></Record></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }

    public function testBooleanBecomesString() {   
        $r = new Services_Twilio_Twiml();
        $r->record(array("transcribe" => true));
        $expected = '<Response><Record transcribe="true" '
            . '></Record></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testRecordMaxLengthKeyTimeout(){
        $r = new Services_Twilio_Twiml();
        $r->record(array("timeout" => 4, "finishOnKey" => "#", 
            "maxLength" => 30));
        $expected = '<Response><Record timeout="4" finishOnKey="#" '
            . 'maxLength="30"></Record></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testRecordConvienceMethod(){
        $r = new Services_Twilio_Twiml();
        $r->record(array("transcribeCallback" => "example.com"));
        $expected = '<Response><Record '
            . 'transcribeCallback="example.com"></Record></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testRecordAddAttribute(){
        $r = new Services_Twilio_Twiml();
        $r->record(array("foo" => "bar"));
        $expected = '<Response><Record foo="bar"></Record></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    //Test Redirect Verb
    public function testRedirect() {
        $r = new Services_Twilio_Twiml();
        $r->redirect();
        $expected = '<Response><Redirect></Redirect></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }

    public function testAmpersandEscaping() {
        $r = new Services_Twilio_Twiml();
        $test_amp = "test&two&amp;three";
        $r->redirect($test_amp);
        $expected = '<Response><Redirect>' .
            'test&amp;two&amp;three</Redirect></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }

    public function testRedirectConvience() {
        $r = new Services_Twilio_Twiml();
        $r->redirect();
        $expected = '<Response><Redirect></Redirect></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    public function testRedirectAddAttribute(){
        $r = new Services_Twilio_Twiml();
        $r->redirect(array("foo" => "bar"));
        $expected = '<Response><Redirect foo="bar"></Redirect></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }

    //Test Hangup Verb
    public function testHangup() {
        $r = new Services_Twilio_Twiml();
        $r->hangup();
        $expected = '<Response><Hangup></Hangup></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testHangupConvience() {
        $r = new Services_Twilio_Twiml();
        $r->hangup();
        $expected = '<Response><Hangup></Hangup></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testHangupAddAttribute(){
        $r = new Services_Twilio_Twiml();
        $r->hangup(array("foo" => "bar"));
        $expected = '<Response><Hangup foo="bar"></Hangup></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    //Test Pause Verb
    public function testPause() {
        $r = new Services_Twilio_Twiml();
        $r->pause();
        $expected = '<Response><Pause></Pause></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testPauseConvience() {
        $r = new Services_Twilio_Twiml();
        $r->pause();
        $expected = '<Response><Pause></Pause></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testPauseAddAttribute(){
        $r = new Services_Twilio_Twiml();
        $r->pause(array("foo" => "bar"));
        $expected = '<Response><Pause foo="bar"></Pause></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    //Test Dial Verb
    public function testDial() {
        $r = new Services_Twilio_Twiml();
        $r->dial("1231231234");
        $expected = '<Response><Dial>1231231234</Dial></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testDialConvience() {
        $r = new Services_Twilio_Twiml();
        $r->dial();
        $expected = '<Response><Dial></Dial></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testDialAddNumber() {
        $r = new Services_Twilio_Twiml();
        $d = $r->dial();
        $d->number("1231231234");
        $expected = '<Response><Dial><Number>'
            . '1231231234</Number></Dial></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testDialAddConference() {
        $r = new Services_Twilio_Twiml();
        $d = $r->dial();
        $d->conference("MyRoom");
        $expected = '<Response><Dial><Conference>'
            . 'MyRoom</Conference></Dial></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testDialAddConferenceConvience() {
        $r = new Services_Twilio_Twiml();
        $d = $r->dial();
        $d->conference("MyRoom", array("startConferenceOnEnter" => "false"));
        $expected = '<Response><Dial><Conference startConferenceOnEnter='
            . '"false">MyRoom</Conference></Dial></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testDialAddAttribute() {
        $r = new Services_Twilio_Twiml();
        $r->dial(array("foo" => "bar"));
        $expected = '<Response><Dial foo="bar"></Dial></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    //Test Gather Verb
    public function testGather() {
        $r = new Services_Twilio_Twiml();
        $r->gather();
        $expected = '<Response><Gather></Gather></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testGatherMethodAction(){
        $r = new Services_Twilio_Twiml();
        $r->gather(array("action"=>"example.com", "method"=>"GET"));
        $expected = '<Response><Gather action="example.com" '
            . 'method="GET"></Gather></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testGatherActionWithParams(){
        $r = new Services_Twilio_Twiml(); 
        $r->gather(array("action" => "record.php?action=recordPageNow"
            . "&id=4&page=3")); 
        $expected = '<Response><Gather action="record.php?action='
            . 'recordPageNow&amp;id=4&amp;page=3"></Gather></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testGatherNestedVerbs(){
        $r = new Services_Twilio_Twiml();
        $g = $r->gather(array("action"=>"example.com", "method"=>"GET"));
        $g->say("Hello World");
        $g->play("helloworld.mp3");
        $g->pause();
        $expected = '
            <Response>
                <Gather action="example.com" method="GET">
                    <Say>Hello World</Say>
                    <Play>helloworld.mp3</Play>
                    <Pause></Pause>
                </Gather>
            </Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testGatherNestedVerbsConvienceMethods(){
        $r = new Services_Twilio_Twiml();
        $g = $r->gather(array("action"=>"example.com", "method"=>"GET"));
        $g->say("Hello World");
        $g->play("helloworld.mp3");
        $g->pause();
        $expected = '
            <Response>
                <Gather action="example.com" method="GET">
                    <Say>Hello World</Say>
                    <Play>helloworld.mp3</Play>
                    <Pause></Pause>
                </Gather>
            </Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testGatherAddAttribute(){
        $r = new Services_Twilio_Twiml();
        $r->gather(array("foo" => "bar"));
        $expected = '<Response><Gather foo="bar"></Gather></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSms() {
        $r = new Services_Twilio_Twiml();
        $r->sms("Hello World");
        $expected = '<Response><Sms>Hello World</Sms></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSmsConvience() {
        $r = new Services_Twilio_Twiml();
        $r->sms("Hello World");
        $expected = '<Response><Sms>Hello World</Sms></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testSmsAddAttribute() {
        $r = new Services_Twilio_Twiml();
        $r->sms(array("foo" => "bar"));
        $expected = '<Response><Sms foo="bar"></Sms></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }
    
    public function testReject() {
        $r = new Services_Twilio_Twiml();
        $r->reject();
        $expected = '<Response><Reject></Reject></Response>';
        $this->assertXmlStringEqualsXmlString($expected, $r);
    }

    function testGeneration() {

        $r = new Services_Twilio_Twiml();
        $r->say('hello');
        $r->dial()->number('123', array('sendDigits' => '456'));
        $r->gather(array('timeout' => 15));

        $doc = simplexml_load_string($r);
        $this->assertEquals('Response', $doc->getName());
        $this->assertEquals('hello', (string) $doc->Say);
        $this->assertEquals('456', (string) $doc->Dial->Number['sendDigits']);
        $this->assertEquals('123', (string) $doc->Dial->Number);
        $this->assertEquals('15', (string) $doc->Gather['timeout']);
    }

}