summaryrefslogtreecommitdiffstats
path: root/Twilio/Tests/Unit/Rest/ClientTest.php
blob: b31f1cb71d58926532590d77f6b728294a9b5e4d (plain)
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
<?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));
    }

}