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\Foundation\ResponseBag; |
14
|
|
|
use TimSDK\TimCloud; |
15
|
|
|
|
16
|
|
|
class TimCloudTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function testEnv() |
19
|
|
|
{ |
20
|
|
|
$this->assertTrue(phpunit_env('phpunit_running')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testCertPemExist() |
24
|
|
|
{ |
25
|
|
|
$t = $this->timCloud(); |
26
|
|
|
|
27
|
|
|
$this->assertTrue(file_exists($t['path.cert'] . '/cacert.pem')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testRefreshConfiguration() |
31
|
|
|
{ |
32
|
|
|
$timCloud = $this->timCloud(); |
33
|
|
|
$this->assertFalse($timCloud->im->isNeedRefresh()); |
34
|
|
|
|
35
|
|
|
$timCloud->setIdentifier('admin'); |
36
|
|
|
$this->assertTrue($timCloud->im->isNeedRefresh()); |
37
|
|
|
|
38
|
|
|
$query = $timCloud->im->getRefreshedQueryStringArray(); |
39
|
|
|
$this->assertSame('admin', $query['identifier']); |
40
|
|
|
|
41
|
|
|
$timCloud->setAppId('1404xxxxx'); |
42
|
|
|
$timCloud->setPublicKey('public_key_xxxxxx'); |
43
|
|
|
$timCloud->setPrivateKey('private_key_xxxxxx'); |
44
|
|
|
$query = $timCloud->im->getRefreshedQueryStringArray(); |
45
|
|
|
|
46
|
|
|
$this->assertSame('1404xxxxx', $query['app_id']); |
47
|
|
|
$this->assertSame('-----BEGIN PRIVATE KEY----- |
48
|
|
|
private_key_xxxxxx |
49
|
|
|
-----END PRIVATE KEY-----', $query['private_key']); |
50
|
|
|
$this->assertSame('-----BEGIN PUBLIC KEY----- |
51
|
|
|
public_key_xxxxxx |
52
|
|
|
-----END PUBLIC KEY-----', $query['public_key']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testSetter() |
56
|
|
|
{ |
57
|
|
|
$timCloud = $this->timCloud(); |
58
|
|
|
|
59
|
|
|
$timCloud->setAppId('1404xxxxx'); |
60
|
|
|
$timCloud->setIdentifier('TimSDK'); |
61
|
|
|
$timCloud->setPublicKey('public_key_xxxxxx'); |
62
|
|
|
$timCloud->setPrivateKey('private_key_xxxxxx'); |
63
|
|
|
$query = $timCloud->im->getRefreshedQueryStringArray(); |
64
|
|
|
|
65
|
|
|
$this->assertSame('1404xxxxx', $query['app_id']); |
66
|
|
|
$this->assertSame('TimSDK', $query['identifier']); |
67
|
|
|
$this->assertSame('-----BEGIN PRIVATE KEY----- |
68
|
|
|
private_key_xxxxxx |
69
|
|
|
-----END PRIVATE KEY-----', $query['private_key']); |
70
|
|
|
$this->assertSame('-----BEGIN PUBLIC KEY----- |
71
|
|
|
public_key_xxxxxx |
72
|
|
|
-----END PUBLIC KEY-----', $query['public_key']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testFormatKey() |
76
|
|
|
{ |
77
|
|
|
$timCloud = $this->timCloud(); |
78
|
|
|
|
79
|
|
|
$priKeyContent = 'MIGqAgEAAiEAsHYdyE9VvL9gwVBXVQrUFSWiWRTD+A+bgyMizSN8uqcCAwEAAQIg |
80
|
|
|
B1LfqZChXlQTD/LlrQHmC2j+E5Fm1+55V/AcT39xGgECEQDauiGoffbvSGVcMPej |
81
|
|
|
Qy+5AhEAzogp60smRdoK0RYDE76tXwIRAMl/xbgqa02fHTmkJs6x+4kCEEouJ/hG |
82
|
|
|
FqoSJb5xjItj+jsCEBxm38VmLmQgIHwKP3ids9U='; |
83
|
|
|
$pubKeyContent = 'MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhALB2HchPVby/YMFQV1UK1BUlolkUw/gP |
84
|
|
|
m4MjIs0jfLqnAgMBAAE='; |
85
|
|
|
|
86
|
|
|
$openSSLPrivateKey = "-----BEGIN PRIVATE KEY----- |
87
|
|
|
$priKeyContent |
88
|
|
|
-----END PRIVATE KEY-----"; |
89
|
|
|
$openSSLPublicKey = "-----BEGIN PUBLIC KEY----- |
90
|
|
|
$pubKeyContent |
91
|
|
|
-----END PUBLIC KEY-----"; |
92
|
|
|
|
93
|
|
|
$prikey1 = $timCloud->formatKey($priKeyContent, 'private'); |
94
|
|
|
$pubkey1 = $timCloud->formatKey($pubKeyContent, 'public'); |
95
|
|
|
|
96
|
|
|
$prikey2 = $timCloud->formatKey($openSSLPrivateKey, 'private'); |
97
|
|
|
$pubkey2 = $timCloud->formatKey($openSSLPublicKey, 'public'); |
98
|
|
|
|
99
|
|
|
$this->assertSame($openSSLPrivateKey, $prikey1); |
100
|
|
|
$this->assertSame($openSSLPublicKey, $pubkey1); |
101
|
|
|
$this->assertSame($openSSLPrivateKey, $prikey2); |
102
|
|
|
$this->assertSame($openSSLPublicKey, $pubkey2); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testRequestApi() |
106
|
|
|
{ |
107
|
|
|
$t = $this->timCloud(); |
108
|
|
|
$t->offsetSet('im', function () { |
109
|
|
|
$m = Mockery::mock('im'); |
110
|
|
|
$m->shouldReceive('handle')->withAnyArgs()->andReturn(new ResponseBag([ |
|
|
|
|
111
|
|
|
'ActionStatus' => 'OK' |
112
|
|
|
], [ |
113
|
|
|
'content-type' => 'application/json' |
114
|
|
|
])); |
115
|
|
|
return $m; |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$c = $t->request(API::DIRTY_WORDS_GET); |
119
|
|
|
$this->assertSame('OK', $c->getContent('ActionStatus')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function timCloud() |
123
|
|
|
{ |
124
|
|
|
return new TimCloud([ |
125
|
|
|
'app_id' => phpunit_env('app_id', '1400xxxxxx'), |
126
|
|
|
'identifier' => phpunit_env('identifier', 'common_user'), |
127
|
|
|
'private_key' => phpunit_env('private_key', 'openssl_private_key'), |
128
|
|
|
'public_key' => phpunit_env('public_key', 'openssl_public_key'), |
129
|
|
|
], [ |
130
|
|
|
'TLSSig' => function () { |
131
|
|
|
$m = Mockery::mock('TLSSig'); |
132
|
|
|
$m->shouldReceive('genSig')->withAnyArgs()->andReturn('test usersig'); |
|
|
|
|
133
|
|
|
return $m; |
134
|
|
|
}, |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function tearDown() |
139
|
|
|
{ |
140
|
|
|
Mockery::close(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.