Completed
Push — master ( 3a7482...36f3d7 )
by Jim
08:32
created

TimCloudTest::testRefreshConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/15/2018
6
 * Time: 3:28 PM
7
 */
8
9
namespace TimSDK\Tests;
10
11
use Mockery;
12
use TimSDK\Core\API;
13
use TimSDK\Core\IMCloud;
14
use TimSDK\Foundation\ResponseBag;
15
use TimSDK\TimCloud;
16
17
class TimCloudTest extends TestCase
18
{
19
    public function testEnv()
20
    {
21
        $this->assertTrue(phpunit_env('phpunit_running'));
22
    }
23
24
    public function testGetVersion()
25
    {
26
        $timCloud = $this->timCloud();
27
28
        $this->assertSame(TimCloud::VERSION, $timCloud->version());
29
    }
30
    
31
    public function testCertPemExist()
32
    {
33
        $t = $this->timCloud();
34
35
        $this->assertTrue(file_exists($t['path.cert'] . '/cacert.pem'));
36
    }
37
38
    public function testFormatKey()
39
    {
40
        $timCloud = $this->timCloud();
41
42
        $priKeyContent = 'MIGqAgEAAiEAsHYdyE9VvL9gwVBXVQrUFSWiWRTD+A+bgyMizSN8uqcCAwEAAQIg
43
B1LfqZChXlQTD/LlrQHmC2j+E5Fm1+55V/AcT39xGgECEQDauiGoffbvSGVcMPej
44
Qy+5AhEAzogp60smRdoK0RYDE76tXwIRAMl/xbgqa02fHTmkJs6x+4kCEEouJ/hG
45
FqoSJb5xjItj+jsCEBxm38VmLmQgIHwKP3ids9U=';
46
        $pubKeyContent = 'MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhALB2HchPVby/YMFQV1UK1BUlolkUw/gP
47
m4MjIs0jfLqnAgMBAAE=';
48
49
        $openSSLPrivateKey = "-----BEGIN PRIVATE KEY-----
50
$priKeyContent
51
-----END PRIVATE KEY-----";
52
        $openSSLPublicKey = "-----BEGIN PUBLIC KEY-----
53
$pubKeyContent
54
-----END PUBLIC KEY-----";
55
56
        $prikey1 = $timCloud->formatKey($priKeyContent, 'private');
57
        $pubkey1 = $timCloud->formatKey($pubKeyContent, 'public');
58
59
        $prikey2 = $timCloud->formatKey($openSSLPrivateKey, 'private');
60
        $pubkey2 = $timCloud->formatKey($openSSLPublicKey, 'public');
61
62
        $this->assertSame($openSSLPrivateKey, $prikey1);
63
        $this->assertSame($openSSLPublicKey, $pubkey1);
64
        $this->assertSame($openSSLPrivateKey, $prikey2);
65
        $this->assertSame($openSSLPublicKey, $pubkey2);
66
    }
67
68
    public function testSetter()
69
    {
70
        $timCloud = $this->timCloud();
71
72
        $timCloud->setAppId('1404xxxxx');
73
        $timCloud->setIdentifier('TimSDK');
74
        $timCloud->setPublicKey('public_key_xxxxxx');
75
        $timCloud->setPrivateKey('private_key_xxxxxx');
76
        $query = $timCloud->im->getLatestQueryString();
77
78
        $this->assertSame('1404xxxxx', $query['app_id']);
79
        $this->assertSame('TimSDK', $query['identifier']);
80
        $this->assertSame('-----BEGIN PRIVATE KEY-----
81
private_key_xxxxxx
82
-----END PRIVATE KEY-----', $query['private_key']);
83
        $this->assertSame('-----BEGIN PUBLIC KEY-----
84
public_key_xxxxxx
85
-----END PUBLIC KEY-----', $query['public_key']);
86
    }
87
88
    public function testRefreshConfiguration()
89
    {
90
        $timCloud = $this->timCloud();
91
        $this->assertFalse($timCloud->im->isNeedRefresh());
92
93
        $timCloud->setIdentifier('admin');
94
        $this->assertTrue($timCloud->im->isNeedRefresh());
95
96
        $query = $timCloud->im->getLatestQueryString();
97
        $this->assertSame('admin', $query['identifier']);
98
        $this->assertFalse($timCloud->im->isNeedRefresh());
99
100
        $timCloud->setAppId('1404xxxxx');
101
        $timCloud->setIdentifier('common_user');
102
        $timCloud->setPublicKey('public_key_xxxxxx');
103
        $timCloud->setPrivateKey('private_key_xxxxxx');
104
        $query = $timCloud->im->getLatestQueryString();
105
106
        $this->assertSame([
107
            'sdkappid'    => '1404xxxxx',
108
            'identifier'  => 'common_user',
109
            'contenttype' => 'json',
110
        ], $timCloud->im->getLatestQueryString([
111
            'sdkappid',
112
            'identifier',
113
            'contenttype',
114
        ]));
115
        $this->assertArrayHasKey('random', $timCloud->im->getLatestQueryString());
116
        $this->assertArrayHasKey('usersig', $timCloud->im->getLatestQueryString());
117
        $this->assertSame('-----BEGIN PRIVATE KEY-----
118
private_key_xxxxxx
119
-----END PRIVATE KEY-----', $query['private_key']);
120
        $this->assertSame('-----BEGIN PUBLIC KEY-----
121
public_key_xxxxxx
122
-----END PUBLIC KEY-----', $query['public_key']);
123
    }
124
125
    /**
126
     * @expectedException \TimSDK\Core\Exceptions\MissingArgumentsException
127
     * @expectedExceptionMessage Missing app_id.
128
     */
129
    public function testMissAppId()
130
    {
131
        $timCloud = new TimCloud([
132
            'identifier'  => phpunit_env('identifier', 'common_user'),
133
            'private_key' => phpunit_env('private_key', 'openssl_private_key'),
134
            'public_key'  => phpunit_env('public_key', 'openssl_public_key'),
135
        ]);
136
137
        $timCloud['im'];
138
    }
139
140
    /**
141
     * @expectedException \TimSDK\Core\Exceptions\MissingArgumentsException
142
     * @expectedExceptionMessage Missing identifier.
143
     */
144
    public function testMissIdentifier()
145
    {
146
        $timCloud = new TimCloud([
147
            'app_id'      => phpunit_env('app_id', '1400xxxxxx'),
148
            'private_key' => phpunit_env('private_key', 'openssl_private_key'),
149
            'public_key'  => phpunit_env('public_key', 'openssl_public_key'),
150
        ]);
151
152
        $timCloud['im'];
153
    }
154
155
    /**
156
     * @expectedException \TimSDK\Core\Exceptions\MissingArgumentsException
157
     * @expectedExceptionMessage Missing private_key.
158
     */
159
    public function testMissPrivateKey()
160
    {
161
        $timCloud = new TimCloud([
162
            'app_id'      => phpunit_env('app_id', '1400xxxxxx'),
163
            'identifier'  => phpunit_env('identifier', 'common_user'),
164
            'public_key'  => phpunit_env('public_key', 'openssl_public_key'),
165
        ]);
166
167
        $timCloud['im'];
168
    }
169
170
    /**
171
     * @expectedException \TimSDK\Core\Exceptions\MissingArgumentsException
172
     * @expectedExceptionMessage Missing public_key.
173
     */
174
    public function testMissPublicKey()
175
    {
176
        $timCloud = new TimCloud([
177
            'app_id'      => phpunit_env('app_id', '1400xxxxxx'),
178
            'identifier'  => phpunit_env('identifier', 'common_user'),
179
            'private_key' => phpunit_env('private_key', 'openssl_private_key'),
180
        ]);
181
182
        $timCloud['im'];
183
    }
184
185
    public function testRequestApiSuccess()
186
    {
187
        $t = $this->timCloud();
188
        $t->offsetSet('im', function () {
189
            $m = Mockery::mock('im');
190
            $m->shouldReceive('handle')->withAnyArgs()->andReturn(new ResponseBag([
191
                'ActionStatus' => 'OK',
192
            ], [
193
                'content-type' => 'application/json',
194
            ]));
195
196
            return $m;
197
        });
198
199
        $c1 = $t->request(API::DIRTY_WORDS_GET);
200
        $this->assertSame('OK', $c1->getContent('ActionStatus'));
201
202
        $c2 = $t->requestDirtyWordsAdd([
203
            'DirtyWordsList' => ['foo', 'bar']
204
        ]);
205
        $this->assertSame('OK', $c2->getContent('ActionStatus'));
206
    }
207
208
    /**
209
     * @expectedException \TimSDK\Core\Exceptions\HttpException
210
     * @expectedExceptionMessage HTTP Parse Error.
211
     */
212
    public function testRequestApiError()
213
    {
214
        $t = $this->timCloud([
215
            'im' => function () {
216
                $m = Mockery::mock(IMCloud::class);
217
                $return = new ResponseBag([
218
                    'ErrorCode' => 80002,
219
                    'ErrorInfo' => 'HTTP Parse Error.',
220
                ], [
221
                    'content-type' => 'application/json',
222
                ]);
223
224
                $m->shouldAllowMockingMethod('handle')->shouldDeferMissing();
225
226
                $m->shouldReceive('getLatestQueryString')->withAnyArgs()->andReturn([]);
227
                $m->shouldReceive('httpPostJson')->withAnyArgs()->andReturn($return);
228
229
                return $m;
230
            },
231
        ]);
232
233
        $t->request(API::DIRTY_WORDS_GET);
234
    }
235
236
    public function testGetApiAlias()
237
    {
238
        $cloud = $this->timCloud();
239
240
        $this->assertSame('foo', $cloud['apiAlias']['foo']);
241
        $this->assertSame(API::BASE_URL, $cloud['apiAlias']['BASE_URL']);
242
243
    }
244
245
    public function timCloud($prepends = [])
246
    {
247
        return new TimCloud([
248
            'app_id'      => phpunit_env('app_id', '1400xxxxxx'),
249
            'identifier'  => phpunit_env('identifier', 'common_user'),
250
            'private_key' => phpunit_env('private_key', 'openssl_private_key'),
251
            'public_key'  => phpunit_env('public_key', 'openssl_public_key'),
252
            'log' => [
253
                'cli_on' => false
254
            ]
255
        ], array_merge([
256
            'TLSSig' => function () {
257
                $m = Mockery::mock('TLSSig');
258
                $m->shouldReceive('genSig')->withAnyArgs()->andReturn('test usersig');
259
                return $m;
260
            },
261
        ], $prepends));
262
    }
263
}
264