diff options
author | matt <matt@twilio.com> | 2016-08-11 19:57:23 -0700 |
---|---|---|
committer | matt <matt@twilio.com> | 2016-08-11 19:57:23 -0700 |
commit | c3e6bbde4e283dbf81aea1424197126165b28f07 (patch) | |
tree | f0920083fd5c8efc8db9e01dc3f4696b5c5f6860 | |
parent | c0d7e5cec50d4f3fcb17b1dce6996b78ad2a6e84 (diff) | |
download | twilio-php-c3e6bbde4e283dbf81aea1424197126165b28f07.zip twilio-php-c3e6bbde4e283dbf81aea1424197126165b28f07.tar.gz twilio-php-c3e6bbde4e283dbf81aea1424197126165b28f07.tar.bz2 |
Add region configurability + \Twilio\Rest\Client unit tests
-rw-r--r-- | Twilio/Rest/Client.php | 40 | ||||
-rw-r--r-- | Twilio/Tests/HolodeckTestCase.php | 2 | ||||
-rw-r--r-- | Twilio/Tests/Unit/Rest/ClientTest.php | 113 |
3 files changed, 152 insertions, 3 deletions
diff --git a/Twilio/Rest/Client.php b/Twilio/Rest/Client.php index ec2ff8d..4700cb4 100644 --- a/Twilio/Rest/Client.php +++ b/Twilio/Rest/Client.php @@ -78,6 +78,7 @@ class Client { protected $username; protected $password; protected $accountSid; + protected $region; protected $httpClient; protected $_account; protected $_api = null; @@ -98,14 +99,16 @@ class Client { * @param string $password Password to authenticate with * @param string $accountSid Account Sid to authenticate with, defaults to * $username + * @param string $region Region to send requests to, defaults to no region + * selection * @param \Twilio\Http\Client $httpClient HttpClient, defaults to CurlClient * @param mixed[] $environment Environment to look for auth details, defaults * to $_ENV * @return \Twilio\Rest\Client Twilio Client * @throws ConfigurationException If valid authentication is not present */ - public function __construct($username = null, $password = null, $accountSid = null, HttpClient $httpClient = null, $environment = null) { - if (!$environment) { + public function __construct($username = null, $password = null, $accountSid = null, $region = null, HttpClient $httpClient = null, $environment = null) { + if (is_null($environment)) { $environment = $_ENV; } @@ -130,6 +133,7 @@ class Client { } $this->accountSid = $accountSid ?: $this->username; + $this->region = $region; if ($httpClient) { $this->httpClient = $httpClient; @@ -168,6 +172,11 @@ class Client { $headers['Accept'] = 'application/json'; } + if ($this->region) { + list($head, $tail) = explode('.', $uri, 2); + $uri = implode('.', array($head, $this->region, $tail)); + } + return $this->getHttpClient()->request( $method, $uri, @@ -181,6 +190,24 @@ class Client { } /** + * Retrieve the Username + * + * @return string Current Username + */ + public function getUsername() { + return $this->username; + } + + /** + * Retrieve the Password + * + * @return string Current Password + */ + public function getPassword() { + return $this->password; + } + + /** * Retrieve the AccountSid * * @return string Current AccountSid @@ -190,6 +217,15 @@ class Client { } /** + * Retrieve the Region + * + * @return string Current Region + */ + public function getRegion() { + return $this->region; + } + + /** * Retrieve the HttpClient * * @return \Twilio\Http\Client Current HttpClient diff --git a/Twilio/Tests/HolodeckTestCase.php b/Twilio/Tests/HolodeckTestCase.php index 3b31f01..6aff19f 100644 --- a/Twilio/Tests/HolodeckTestCase.php +++ b/Twilio/Tests/HolodeckTestCase.php @@ -15,7 +15,7 @@ class HolodeckTestCase extends PHPUnit_Framework_TestCase protected function setUp() { $this->holodeck = new Holodeck(); - $this->twilio = new Client('ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'AUTHTOKEN', null, $this->holodeck); + $this->twilio = new Client('ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'AUTHTOKEN', null, null, $this->holodeck); } protected function tearDown() { diff --git a/Twilio/Tests/Unit/Rest/ClientTest.php b/Twilio/Tests/Unit/Rest/ClientTest.php new file mode 100644 index 0000000..b31f1cb --- /dev/null +++ b/Twilio/Tests/Unit/Rest/ClientTest.php @@ -0,0 +1,113 @@ +<?php + + +namespace Twilio\Tests\Unit\Rest; + + +use Twilio\Rest\Client; +use Twilio\Tests\Holodeck; +use Twilio\Tests\Request; +use Twilio\Tests\Unit\UnitTest; + +class ClientTest extends UnitTest { + + /** + * @expectedException \Twilio\Exceptions\ConfigurationException + */ + public function testThrowsWhenUsernameAndPasswordMissing() { + new Client(null, null, null, null, null, array()); + } + + /** + * @expectedException \Twilio\Exceptions\ConfigurationException + */ + public function testThrowsWhenUsernameMissing() { + new Client(null, 'password', null, null, null, array()); + } + + /** + * @expectedException \Twilio\Exceptions\ConfigurationException + */ + public function testThrowsWhenPasswordMissing() { + new Client('username', null, null, null, null, array()); + } + + public function testUsernamePulledFromEnvironment() { + $client = new Client(null, 'password', null, null, null, array( + Client::ENV_ACCOUNT_SID => 'username', + )); + + $this->assertEquals('username', $client->getUsername()); + } + + public function testPasswordPulledFromEnvironment() { + $client = new Client('username', null, null, null, null, array( + Client::ENV_AUTH_TOKEN => 'password', + )); + + $this->assertEquals('password', $client->getPassword()); + } + + public function testUsernameAndPasswordPulledFromEnvironment() { + $client = new Client(null, null, null, null, null, array( + Client::ENV_ACCOUNT_SID => 'username', + Client::ENV_AUTH_TOKEN => 'password', + )); + + $this->assertEquals('username', $client->getUsername()); + $this->assertEquals('password', $client->getPassword()); + } + + public function testUsernameParameterPreferredOverEnvironment() { + $client = new Client('username', 'password', null, null, null, array( + Client::ENV_ACCOUNT_SID => 'environmentUsername', + )); + + $this->assertEquals('username', $client->getUsername()); + } + + public function testPasswordParameterPreferredOverEnvironment() { + $client = new Client('username', 'password', null, null, null, array( + Client::ENV_AUTH_TOKEN => 'environmentPassword', + )); + + $this->assertEquals('password', $client->getPassword()); + } + + public function testUsernameAndPasswordParametersPreferredOverEnvironment() { + $client = new Client('username', 'password', null, null, null, array( + Client::ENV_ACCOUNT_SID => 'environmentUsername', + Client::ENV_AUTH_TOKEN => 'environmentPassword', + )); + + $this->assertEquals('username', $client->getUsername()); + $this->assertEquals('password', $client->getPassword()); + } + + public function testAccountSidDefaultsToUsername() { + $client = new Client('username', 'password'); + $this->assertEquals('username', $client->getAccountSid()); + } + + public function testAccountSidPreferredOverUsername() { + $client = new Client('username', 'password', 'accountSid'); + $this->assertEquals('accountSid', $client->getAccountSid()); + } + + public function testRegionDefaultsToEmpty() { + $network = new Holodeck(); + $client = new Client('username', 'password', null, null, $network); + $client->request('POST', 'https://test.twilio.com/v1/Resources'); + $expected = new Request('POST', 'https://test.twilio.com/v1/Resources'); + $this->assertTrue($network->hasRequest($expected)); + } + + public function testRegionInjectedWhenSet() { + $network = new Holodeck(); + $client = new Client('username', 'password', null, 'ie1', $network); + $client->request('POST', 'https://test.twilio.com/v1/Resources'); + $expected = new Request('POST', 'https://test.ie1.twilio.com/v1/Resources'); + $this->assertTrue($network->hasRequest($expected)); + } + +}
\ No newline at end of file |