[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  use \Mockery as m;
   4  
   5  class TwilioTest extends PHPUnit_Framework_TestCase {
   6  
   7      protected $formHeaders = array('Content-Type' => 'application/x-www-form-urlencoded');
   8      protected $callParams = array('To' => '123', 'From' => '123', 'Url' => 'http://example.com');
   9      protected $nginxError = array(500, array('Content-Type' => 'text/html'),
  10                  '<html>Nginx 500 error</html>'
  11              );
  12  
  13      protected $pagingParams = array('Page' => '0', 'PageSize' => '10');
  14      function tearDown() {
  15          m::close();
  16      }
  17  
  18      function getClient($http) {
  19          return new Services_Twilio('AC123', '123', '2010-04-01', $http);
  20      }
  21  
  22      function createMockHttp($url, $method, $response, $params = null,
  23          $status = 200
  24      ) {
  25          $http = m::mock(new Services_Twilio_TinyHttp);
  26          if ($method === 'post') {
  27              $http->shouldReceive('post')->once()->with(
  28                  "/2010-04-01/Accounts/AC123$url.json",
  29                  $this->formHeaders,
  30                  http_build_query($params)
  31              )->andReturn(array(
  32                      $status,
  33                      array('Content-Type' => 'application/json'),
  34                      json_encode($response)
  35                  )
  36              );
  37          } else {
  38              $query = empty($params) ? '' : '?' . http_build_query($params);
  39              $http->shouldReceive($method)->once()->with(
  40                  "/2010-04-01/Accounts/AC123$url.json$query"
  41              )->andReturn(array(
  42                      $status,
  43                      array('Content-Type' => 'application/json'),
  44                      json_encode($response)
  45                  )
  46              );
  47          }
  48          return $http;
  49      }
  50  
  51      /**
  52       * @dataProvider uriTestProvider
  53       */
  54      function testRequestUriConstructedProperly($path, $params, $full_uri, $end_string) {
  55          $this->assertSame($end_string, Services_Twilio::getRequestUri(
  56              $path, $params, $full_uri
  57          ));
  58      }
  59  
  60      function uriTestProvider() {
  61          return array(
  62              array('/2010-04-01/Accounts', array('FriendlyName' => 'hi'), false,
  63                  '/2010-04-01/Accounts.json?FriendlyName=hi'),
  64              array('/2010-04-01/Accounts', array(), false,
  65                  '/2010-04-01/Accounts.json'),
  66              array('/2010-04-01/Accounts.json', array(), true,
  67                  '/2010-04-01/Accounts.json'),
  68              array('/2010-04-01/Accounts.json', array('FriendlyName' => 'hi'), true,
  69                  '/2010-04-01/Accounts.json'),
  70              array('/2010-04-01/Accounts', array(
  71                  'FriendlyName' => 'hi', 'foo' => 'bar'
  72              ), false, '/2010-04-01/Accounts.json?FriendlyName=hi&foo=bar'),
  73          );
  74      }
  75  
  76      function testNeedsRefining() {
  77          $http = $this->createMockHttp('', 'get', array(
  78                      'sid' => 'AC123',
  79                      'friendly_name' => 'Robert Paulson',
  80                  )
  81              );
  82          $client = $this->getClient($http);
  83          $this->assertEquals('AC123', $client->account->sid);
  84          $this->assertEquals('Robert Paulson', $client->account->friendly_name);
  85      }
  86  
  87      function testAccessSidAvoidsNetworkCall() {
  88          $http = m::mock(new Services_Twilio_TinyHttp);
  89          $http->shouldReceive('get')->never();
  90          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
  91          $client->account->sid;
  92      }
  93  
  94      function testOnlyOneClientCreated() {
  95          $client = new Services_Twilio('AC123', '456');
  96          $client->account->client->sid = 'CL456';
  97          $this->assertSame('CL456', $client->account->sandbox->client->sid);
  98      }
  99  
 100      function testNullVersionReturnsNewest() {
 101          $client = new Services_Twilio('AC123', '123', null);
 102          $this->assertEquals('2010-04-01', $client->getVersion());
 103          $client = new Services_Twilio('AC123', '123', 'v1');
 104          $this->assertEquals('2010-04-01', $client->getVersion());
 105          $client = new Services_Twilio('AC123', '123', '2010-04-01');
 106          $this->assertEquals('2010-04-01', $client->getVersion());
 107          $client = new Services_Twilio('AC123', '123', '2008-08-01');
 108          $this->assertEquals('2008-08-01', $client->getVersion());
 109      }
 110  
 111      function testObjectLoadsOnlyOnce() {
 112          $http = $this->createMockHttp('', 'get', array(
 113                      'sid' => 'AC123',
 114                      'friendly_name' => 'Robert Paulson',
 115                      'status' => 'active',
 116                  ));
 117          $client = $this->getClient($http);
 118          $client->account->friendly_name;
 119          $client->account->friendly_name;
 120          $client->account->status;
 121      }
 122  
 123      function testSubresourceLoad() {
 124          $http = $this->createMockHttp('/Calls/CA123', 'get',
 125              array('status' => 'Completed')
 126          );
 127          $client = $this->getClient($http);
 128          $this->assertEquals(
 129              'Completed',
 130              $client->account->calls->get('CA123')->status
 131          );
 132      }
 133  
 134      function testSubresourceSubresource() {
 135          $http = $this->createMockHttp('/Calls/CA123/Notifications/NO123', 'get',
 136              array('message_text' => 'Foo')
 137          );
 138  
 139          $client = $this->getClient($http);
 140          $notifs = $client->account->calls->get('CA123')->notifications;
 141          $this->assertEquals('Foo', $notifs->get('NO123')->message_text);
 142      }
 143  
 144      function testGetIteratorUsesFilters() {
 145          $params = array_merge($this->pagingParams, array(
 146              'StartTime>' => '2012-07-06',
 147          ));
 148          $response = array(
 149              'total' => 1,
 150              'calls' => array(array('status' => 'Completed', 'sid' => 'CA123'))
 151          );
 152          $http = $this->createMockHttp('/Calls', 'get', $response, $params);
 153          $client = $this->getClient($http);
 154  
 155          $iterator = $client->account->calls->getIterator(
 156              0, 10, array('StartTime>' => '2012-07-06'));
 157          foreach ($iterator as $call) {
 158              $this->assertEquals('Completed', $call->status);
 159              break;
 160          }
 161      }
 162  
 163      function testListResource() {
 164          $response = array(
 165              'total' => 1,
 166              'calls' => array(array('status' => 'completed', 'sid' => 'CA123'))
 167          );
 168          $http = $this->createMockHttp('/Calls', 'get', $response,
 169              $this->pagingParams);
 170          $client = $this->getClient($http);
 171  
 172          $page = $client->account->calls->getPage(0, 10);
 173          $call = current($page->getItems());
 174          $this->assertEquals('completed', $call->status);
 175          $this->assertEquals(1, $page->total);
 176      }
 177  
 178      function testInstanceResourceUriConstructionFromList() {
 179          $response = array(
 180              'total' => 1,
 181              'calls' => array(array(
 182                  'status' => 'in-progress',
 183                  'sid' => 'CA123',
 184                  'uri' => 'junk_uri'
 185              ))
 186          );
 187          $http = $this->createMockHttp('/Calls', 'get', $response,
 188              $this->pagingParams);
 189          $http->shouldReceive('get')->once()
 190              ->with('/2010-04-01/Accounts/AC123/Calls/CA123.json')
 191              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 192                  json_encode(array(
 193                      'status' => 'completed'
 194                  ))
 195              ));
 196          $client = $this->getClient($http);
 197          $page = $client->account->calls->getPage(0, 10);
 198          $call = current($page->getItems());
 199  
 200          /* trigger api fetch by trying to retrieve nonexistent var */
 201          try {
 202              $call->nonexistent;
 203          } catch (Exception $e) {
 204              // pass
 205          }
 206          $this->assertSame($call->status, 'completed');
 207      }
 208  
 209      function testInstanceResourceUriConstructionFromGet() {
 210          $http = m::mock(new Services_Twilio_TinyHttp);
 211          $http->shouldReceive('get')->once()
 212              ->with('/2010-04-01/Accounts/AC123/IncomingPhoneNumbers/PN123.json')
 213              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 214                  json_encode(array(
 215                      'sms_method' => 'POST',
 216                      'sid' => 'PN123',
 217                      'uri' => 'junk_uri',
 218                  ))
 219              ));
 220          $http->shouldReceive('post')->once()
 221              ->with('/2010-04-01/Accounts/AC123/IncomingPhoneNumbers/PN123.json',
 222                  $this->formHeaders, 'SmsMethod=GET')
 223              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 224                  json_encode(array(
 225                      'sms_method' => 'GET',
 226                      'sid' => 'PN123',
 227                      'uri' => 'junk_uri'
 228                  ))
 229              ));
 230          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 231          $number = $client->account->incoming_phone_numbers->get('PN123');
 232          $this->assertSame($number->sms_method, 'POST');
 233  
 234          $number->update(array('SmsMethod' => 'GET'));
 235          $this->assertSame($number->sms_method, 'GET');
 236      }
 237  
 238      function testIterateOverPage() {
 239          $http = m::mock(new Services_Twilio_TinyHttp);
 240          $http->shouldReceive('get')->once()
 241              ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=10')
 242              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 243                  json_encode(array(
 244                      'total' => 1,
 245                      'calls' => array(array('status' => 'Completed', 'sid' => 'CA123'))
 246                  ))
 247              ));
 248          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 249          $page = $client->account->calls->getPage(0, 10);
 250          foreach ($page->getIterator() as $pageitems) {
 251              $this->assertSame('CA123', $pageitems->sid);
 252          }
 253      }
 254  
 255      function testAsymmetricallyNamedResources() {
 256          $http = m::mock(new Services_Twilio_TinyHttp);
 257          $http->shouldReceive('get')->once()
 258              ->with('/2010-04-01/Accounts/AC123/SMS/Messages.json?Page=0&PageSize=10')
 259              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 260                  json_encode(array('sms_messages' => array(
 261                      array('status' => 'sent', 'sid' => 'SM123')
 262                  )))
 263              ));
 264          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 265          $sms = current($client->account->sms_messages->getPage(0, 10)->getItems());
 266          $this->assertEquals('sent', $sms->status);
 267      }
 268  
 269      function testParams() {
 270          $http = m::mock(new Services_Twilio_TinyHttp);
 271          $qs = 'Page=0&PageSize=10&FriendlyName=foo&Status=active';
 272          $http->shouldReceive('get')
 273              ->with('/2010-04-01/Accounts.json?' . $qs)
 274              ->andReturn(array(
 275                  200,
 276                  array('Content-Type' => 'application/json'),
 277                  '{"accounts":[]}'
 278              ));
 279          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 280          $client->accounts->getPage(0, 10, array(
 281              'FriendlyName' => 'foo',
 282              'Status' => 'active',
 283          ));
 284      }
 285  
 286      function testUpdate() {
 287          $http = m::mock(new Services_Twilio_TinyHttp);
 288          $http->shouldReceive('post')->once()->with(
 289                  '/2010-04-01/Accounts/AC123/Calls.json', $this->formHeaders,
 290                  http_build_query($this->callParams)
 291              )->andReturn(
 292                  array(200, array('Content-Type' => 'application/json'),
 293                  '{"sid":"CA123"}')
 294          );
 295          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 296          $client->account->calls->create('123', '123', 'http://example.com');
 297      }
 298  
 299      function testModifyLiveCall() {
 300          $http = m::mock(new Services_Twilio_TinyHttp);
 301          $http->shouldReceive('post')->once()->with(
 302              '/2010-04-01/Accounts/AC123/Calls.json', $this->formHeaders,
 303              http_build_query($this->callParams)
 304          )->andReturn(
 305              array(200, array('Content-Type' => 'application/json'),
 306              '{"sid":"CA123"}')
 307          );
 308          $http->shouldReceive('post')->once()->with(
 309              '/2010-04-01/Accounts/AC123/Calls/CA123.json',
 310              $this->formHeaders,
 311              'Status=completed'
 312          )->andReturn(
 313              array(200, array('Content-Type' => 'application/json'),
 314                  '{"sid":"CA123"}'
 315              )
 316          );
 317          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 318          $calls = $client->account->calls;
 319          $call = $calls->create('123', '123', 'http://example.com');
 320          $call->hangup();
 321      }
 322  
 323      function testUnmute() {
 324          $http = m::mock(new Services_Twilio_TinyHttp);
 325          $http->shouldReceive('get')->once()
 326              ->with(
 327                  '/2010-04-01/Accounts/AC123/Conferences/CF123/Participants.json?Page=0&PageSize=10')
 328                  ->andReturn(array(200, array('Content-Type' => 'application/json'),
 329                      json_encode(array(
 330                          'participants' => array(array('call_sid' => 'CA123'))
 331                      ))
 332                  ));
 333          $http->shouldReceive('post')->once()
 334              ->with(
 335                  '/2010-04-01/Accounts/AC123/Conferences/CF123/Participants/CA123.json',
 336                  $this->formHeaders,
 337                  'Muted=true'
 338              )->andReturn(array(200, array('Content-Type' => 'application/json'),
 339                  json_encode(array())
 340              ));
 341          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 342          $conf = $client->account->conferences->get('CF123');
 343          $page = $conf->participants->getPage(0, 10);
 344          foreach ($page->getItems() as $participant) {
 345              $participant->mute();
 346          }
 347      }
 348  
 349      function testResourcePropertiesReflectUpdates() {
 350          $http = m::mock(new Services_Twilio_TinyHttp);
 351          $http->shouldReceive('get')->once()
 352              ->with('/2010-04-01/Accounts/AC123.json')
 353              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 354                  json_encode(array('friendly_name' => 'foo'))
 355              ));
 356          $http->shouldReceive('post')->once()
 357              ->with('/2010-04-01/Accounts/AC123.json', $this->formHeaders, 'FriendlyName=bar')
 358              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 359                  json_encode(array('friendly_name' => 'bar'))
 360              ));
 361          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 362          $this->assertEquals('foo', $client->account->friendly_name);
 363          $client->account->update('FriendlyName', 'bar');
 364          $this->assertEquals('bar', $client->account->friendly_name);
 365      }
 366  
 367      //function testAccessingNonExistentPropertiesErrorsOut
 368  
 369      function testArrayAccessForListResources() {
 370          $http = m::mock(new Services_Twilio_TinyHttp);
 371          $http->shouldReceive('get')->once()
 372              ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=50')
 373              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 374                  json_encode(array(
 375                      'calls' => array(array('sid' => 'CA123'))
 376                  ))
 377              ));
 378          $http->shouldReceive('get')->once()
 379              ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=1&PageSize=50')
 380              ->andReturn(array(400, array('Content-Type' => 'application/json'),
 381                  '{"status":400,"message":"foo", "code": "20006"}'
 382              ));
 383          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 384          foreach ($client->account->calls as $call) {
 385              $this->assertEquals('CA123', $call->sid);
 386          }
 387          $this->assertInstanceOf('Traversable', $client->account->calls);
 388      }
 389  
 390      function testDeepPagingUsesAfterSid() {
 391          $http = m::mock(new Services_Twilio_TinyHttp);
 392          $callsBase = '/2010-04-01/Accounts/AC123/Calls.json';
 393          $firstPageUri = $callsBase . '?Page=0&PageSize=1';
 394          $afterSidUri = $callsBase . '?Page=1&PageSize=1&AfterSid=CA123';
 395          $secondAfterSidUri = $callsBase . '?Page=2&PageSize=1&AfterSid=CA456';
 396          $http->shouldReceive('get')->once()
 397              ->with($firstPageUri)
 398              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 399                  json_encode(array(
 400                      'next_page_uri' => $afterSidUri,
 401                      'calls' => array(array(
 402                          'sid' => 'CA123',
 403                          'price' => '-0.02000',
 404                      ))
 405                  ))
 406              ));
 407          $http->shouldReceive('get')->once()
 408              ->with($afterSidUri)
 409              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 410                  json_encode(array(
 411                      'next_page_uri' => $secondAfterSidUri,
 412                      'calls' => array(array(
 413                          'sid' => 'CA456',
 414                          'price' => '-0.02000',
 415                      ))
 416                  ))
 417              ));
 418          $http->shouldReceive('get')->once()
 419              ->with($secondAfterSidUri)
 420              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 421                  json_encode(array(
 422                      'next_page_uri' => null,
 423                      'calls' => array(array(
 424                          'sid' => 'CA789',
 425                          'price' => '-0.02000',
 426                      ))
 427                  ))
 428              ));
 429          $http->shouldReceive('get')->once()
 430              ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=3&PageSize=1')
 431              ->andReturn(array(400, array('Content-Type' => 'application/json'),
 432                  '{"status":400,"message":"foo", "code": "20006"}'
 433              ));
 434          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 435          foreach ($client->account->calls->getIterator(0, 1) as $call) {
 436              $this->assertSame($call->price, '-0.02000');
 437          }
 438      }
 439  
 440      function testIteratorWithFiltersPagesCorrectly() {
 441          $http = m::mock(new Services_Twilio_TinyHttp);
 442          $recordingsBase = '/2010-04-01/Accounts/AC123/Recordings.json';
 443          $firstPageUri = $recordingsBase . '?Page=0&PageSize=1&DateCreated%3E=2011-01-01';
 444          $secondPageUri = $recordingsBase . '?DateCreated%3E=2011-01-01&Page=1&PageSize=1';
 445          $thirdPageUri = $recordingsBase . '?DateCreated%3E=2011-01-01&Page=2&PageSize=1';
 446          $http->shouldReceive('get')->once()
 447              ->with($firstPageUri)
 448              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 449                  json_encode(array(
 450                      'next_page_uri' => $secondPageUri,
 451                      'recordings' => array(array(
 452                          'sid' => 'RE123',
 453                          'duration' => 7,
 454                      ))
 455                  ))
 456              ));
 457          $http->shouldReceive('get')->once()
 458              ->with($secondPageUri)
 459              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 460                  json_encode(array(
 461                      'next_page_uri' => $thirdPageUri,
 462                      'recordings' => array(array(
 463                          'sid' => 'RE123',
 464                          'duration' => 7,
 465                      ))
 466                  ))
 467              ));
 468          $http->shouldReceive('get')->once()
 469              ->with($thirdPageUri)
 470              ->andReturn(array(400, array('Content-Type' => 'application/json'),
 471                  '{"status":400,"message":"foo", "code": "20006"}'
 472              ));
 473  
 474          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 475          foreach ($client->account->recordings->getIterator(0, 1, array('DateCreated>' => '2011-01-01')) as $recording) {
 476              $this->assertSame($recording->duration, 7);
 477          }
 478      }
 479  
 480      function testRetryOn500() {
 481          $http = m::mock(new Services_Twilio_TinyHttp);
 482          $http->shouldReceive('get')->once()
 483              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 484              ->andReturn($this->nginxError);
 485          $http->shouldReceive('get')->once()
 486              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 487              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 488                  json_encode(array('price' => 0.5))
 489              )
 490          );
 491          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 492          $message = $client->account->sms_messages->get('SM123');
 493          $this->assertSame($message->price, 0.5);
 494      }
 495  
 496      function testDeleteOn500() {
 497          $http = m::mock(new Services_Twilio_TinyHttp);
 498          $http->shouldReceive('delete')->once()
 499              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 500              ->andReturn($this->nginxError);
 501          $http->shouldReceive('delete')->once()
 502              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 503              ->andReturn(
 504                  array(204, array('Content-Type' => 'application/json'), '')
 505          );
 506          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 507          $client->account->sms_messages->delete('SM123');
 508      }
 509  
 510      function testSetExplicitRetryLimit() {
 511          $http = m::mock(new Services_Twilio_TinyHttp);
 512          $http->shouldReceive('get')->once()
 513              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 514              ->andReturn($this->nginxError);
 515          $http->shouldReceive('get')->once()
 516              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 517              ->andReturn($this->nginxError);
 518          $http->shouldReceive('get')->once()
 519              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 520              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 521                  json_encode(array('price' => 0.5))
 522              )
 523          );
 524          // retry twice
 525          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http, 2);
 526          $message = $client->account->sms_messages->get('SM123');
 527          $this->assertSame($message->price, 0.5);
 528      }
 529  
 530      function testRetryLimitIsHonored() {
 531          $this->setExpectedException('Services_Twilio_RestException');
 532          $http = m::mock(new Services_Twilio_TinyHttp);
 533          $http->shouldReceive('get')->once()
 534              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 535              ->andReturn($this->nginxError);
 536          $http->shouldReceive('get')->once()
 537              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 538              ->andReturn($this->nginxError);
 539          $http->shouldReceive('get')->never()
 540              ->with('/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json')
 541              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 542                  json_encode(array('price' => 0.5))
 543              )
 544          );
 545          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 546          $message = $client->account->sms_messages->get('SM123');
 547          $this->assertSame($message->price, 0.5);
 548      }
 549  
 550      function testRetryIdempotentFunctionsOnly() {
 551          $this->setExpectedException('Services_Twilio_RestException');
 552          $http = m::mock(new Services_Twilio_TinyHttp);
 553          $http->shouldReceive('post')->once()
 554              ->with('/2010-04-01/Accounts/AC123/SMS/Messages.json', $this->formHeaders,
 555                  'From=%2B14105551234&To=%2B14102221234&Body=bar')
 556              ->andReturn($this->nginxError);
 557          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 558          $message = $client->account->sms_messages->create('+14105551234',
 559              '+14102221234', 'bar');
 560      }
 561  
 562      function testExceptionUsesHttpStatus() {
 563          $http = $this->createMockHttp('/Queues/QU123/Members/Front', 'post',
 564              array(), array('Url' => 'http://google.com'), 400);
 565          $client = $this->getClient($http);
 566          try {
 567              $front = $client->account->queues->get('QU123')->members->front();
 568              $front->update(array('Url' => 'http://google.com'));
 569              $this->fail('should throw rest exception before reaching this line.');
 570          } catch (Services_Twilio_RestException $e) {
 571              $this->assertSame($e->getStatus(), 400);
 572              $this->assertSame($e->getMessage(), '');
 573          }
 574      }
 575  
 576      function testUnicode() {
 577          $http = m::mock(new Services_Twilio_TinyHttp);
 578          $http->shouldReceive('post')->once()
 579              ->with('/2010-04-01/Accounts/AC123/SMS/Messages.json', $this->formHeaders,
 580              'From=123&To=123&Body=Hello+%E2%98%BA')
 581              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 582                  json_encode(array('sid' => 'SM123'))
 583              )
 584          );
 585          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 586          $message = $client->account->sms_messages->create('123', '123',
 587              'Hello ☺');
 588          $this->assertSame($message->sid, 'SM123');
 589      }
 590  
 591      function testCount() {
 592          $http = m::mock(new Services_Twilio_TinyHttp);
 593          $http->shouldReceive('get')->once()
 594              ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=1')
 595              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 596                  json_encode(array(
 597                      'total' => '1474',
 598                      'calls' => array(),
 599                  ))
 600              ));
 601          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 602          $this->assertSame(count($client->account->calls), 1474);
 603      }
 604  
 605      function testCountNoTotal() {
 606          $http = m::mock(new Services_Twilio_TinyHttp);
 607          $http->shouldReceive('get')->once()
 608              ->with('/2010-04-01/Accounts/AC123/Calls.json?Page=0&PageSize=1')
 609              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 610                  json_encode(array(
 611                      'calls' => array(),
 612                  ))
 613              ));
 614          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 615          $this->assertSame(count($client->account->calls), 0);
 616      }
 617  
 618      function testPostMultivaluedForm() {
 619          $http = m::mock(new Services_Twilio_TinyHttp);
 620          $http->shouldReceive('post')->once()
 621              ->with('/2010-04-01/Accounts/AC123/Messages.json', $this->formHeaders,
 622              'From=123&To=123&MediaUrl=http%3A%2F%2Fexample.com%2Fimage1&MediaUrl=http%3A%2F%2Fexample.com%2Fimage2')
 623              ->andReturn(array(200, array('Content-Type' => 'application/json'),
 624                  json_encode(array('sid' => 'SM123'))
 625              )
 626          );
 627          $client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
 628          $message = $client->account->messages->sendMessage('123', '123', null,
 629              array('http://example.com/image1', 'http://example.com/image2')
 630          );
 631          $this->assertSame($message->sid, 'SM123');
 632      }
 633  
 634      function testToString() {
 635          $http = m::mock(new Services_Twilio_TinyHttp);
 636          $resp = <<<JSON
 637              {
 638                  "account_sid": "AC123",
 639                  "api_version": "2010-04-01",
 640                  "body": "Hello world!",
 641                  "date_created": "Mon, 06 Jan 2014 04:54:34 +0000",
 642                  "date_sent": "Mon, 06 Jan 2014 04:54:34 +0000",
 643                  "date_updated": "Mon, 06 Jan 2014 04:54:34 +0000",
 644                  "direction": "outbound-api",
 645                  "from": "+19255556789",
 646                  "num_media": null,
 647                  "num_segments": "1",
 648                  "price": "-0.00750",
 649                  "price_unit": "USD",
 650                  "sid": "SM77d5ccc71419444fb730541daaaaaaaa",
 651                  "status": "sent",
 652                  "subresource_uris": {
 653                      "media": "/2010-04-01/Accounts/AC123/Messages/SM77d5ccc71419444fb730541daaaaaaaa/Media.json"
 654                  },
 655                  "to": "+19255551234",
 656                  "uri": "/2010-04-01/Accounts/AC123/Messages/SM77d5ccc71419444fb730541daaaaaaaa.json"
 657              }
 658  JSON;
 659          $sampleMessage = new Services_Twilio_Rest_Message($http, '/foo',
 660              json_decode($resp)
 661          );
 662          $expected = '{"account_sid":"AC123","api_version":"2010-04-01","body":"Hello world!","date_created":"Mon, 06 Jan 2014 04:54:34 +0000","date_sent":"Mon, 06 Jan 2014 04:54:34 +0000","date_updated":"Mon, 06 Jan 2014 04:54:34 +0000","direction":"outbound-api","from":"+19255556789","num_media":null,"num_segments":"1","price":"-0.00750","price_unit":"USD","sid":"SM77d5ccc71419444fb730541daaaaaaaa","status":"sent","subresource_uris":{"media":"\/2010-04-01\/Accounts\/AC123\/Messages\/SM77d5ccc71419444fb730541daaaaaaaa\/Media.json"},"to":"+19255551234","uri":"\/foo"}';
 663          $this->assertSame((string)$sampleMessage, $expected);
 664      }
 665  
 666      function testSubresourceUris() {
 667          $http = m::mock(new Services_Twilio_TinyHttp);
 668          $call = new Services_Twilio_Rest_Call($http, '/foo');
 669          $recordings = $call->subresources['recordings'];
 670          $this->assertSame($recordings->uri, '/foo/Recordings');
 671      }
 672  }


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