Passed
Push — master ( 248b67...e47890 )
by Jim
01:53
created

TimCloudTest::testGetVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
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\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 testGetVersion()
24
    {
25
        $timCloud = $this->timCloud();
26
27
        $this->assertSame(TimCloud::VERSION, $timCloud->version());
28
    }
29
    
30
    public function testCertPemExist()
31
    {
32
        $t = $this->timCloud();
33
34
        $this->assertTrue(file_exists($t['path.cert'] . '/cacert.pem'));
35
    }
36
37
    public function testRefreshConfiguration()
38
    {
39
        $timCloud = $this->timCloud();
40
        $this->assertFalse($timCloud->im->isNeedRefresh());
41
42
        $timCloud->setIdentifier('admin');
43
        $this->assertTrue($timCloud->im->isNeedRefresh());
44
45
        $query = $timCloud->im->getRefreshedQueryStringArray();
46
        $this->assertSame('admin', $query['identifier']);
47
48
        $timCloud->setAppId('1404xxxxx');
49
        $timCloud->setPublicKey('public_key_xxxxxx');
50
        $timCloud->setPrivateKey('private_key_xxxxxx');
51
        $query = $timCloud->im->getRefreshedQueryStringArray();
52
53
        $this->assertSame('1404xxxxx', $query['app_id']);
54
        $this->assertSame('-----BEGIN PRIVATE KEY-----
55
private_key_xxxxxx
56
-----END PRIVATE KEY-----', $query['private_key']);
57
        $this->assertSame('-----BEGIN PUBLIC KEY-----
58
public_key_xxxxxx
59
-----END PUBLIC KEY-----', $query['public_key']);
60
    }
61
62
    public function testSetter()
63
    {
64
        $timCloud = $this->timCloud();
65
66
        $timCloud->setAppId('1404xxxxx');
67
        $timCloud->setIdentifier('TimSDK');
68
        $timCloud->setPublicKey('public_key_xxxxxx');
69
        $timCloud->setPrivateKey('private_key_xxxxxx');
70
        $query = $timCloud->im->getRefreshedQueryStringArray();
71
72
        $this->assertSame('1404xxxxx', $query['app_id']);
73
        $this->assertSame('TimSDK', $query['identifier']);
74
        $this->assertSame('-----BEGIN PRIVATE KEY-----
75
private_key_xxxxxx
76
-----END PRIVATE KEY-----', $query['private_key']);
77
        $this->assertSame('-----BEGIN PUBLIC KEY-----
78
public_key_xxxxxx
79
-----END PUBLIC KEY-----', $query['public_key']);
80
    }
81
82
    public function testFormatKey()
83
    {
84
        $timCloud = $this->timCloud();
85
86
        $priKeyContent = 'MIGqAgEAAiEAsHYdyE9VvL9gwVBXVQrUFSWiWRTD+A+bgyMizSN8uqcCAwEAAQIg
87
B1LfqZChXlQTD/LlrQHmC2j+E5Fm1+55V/AcT39xGgECEQDauiGoffbvSGVcMPej
88
Qy+5AhEAzogp60smRdoK0RYDE76tXwIRAMl/xbgqa02fHTmkJs6x+4kCEEouJ/hG
89
FqoSJb5xjItj+jsCEBxm38VmLmQgIHwKP3ids9U=';
90
        $pubKeyContent = 'MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhALB2HchPVby/YMFQV1UK1BUlolkUw/gP
91
m4MjIs0jfLqnAgMBAAE=';
92
93
        $openSSLPrivateKey = "-----BEGIN PRIVATE KEY-----
94
$priKeyContent
95
-----END PRIVATE KEY-----";
96
        $openSSLPublicKey = "-----BEGIN PUBLIC KEY-----
97
$pubKeyContent
98
-----END PUBLIC KEY-----";
99
100
        $prikey1 = $timCloud->formatKey($priKeyContent, 'private');
101
        $pubkey1 = $timCloud->formatKey($pubKeyContent, 'public');
102
103
        $prikey2 = $timCloud->formatKey($openSSLPrivateKey, 'private');
104
        $pubkey2 = $timCloud->formatKey($openSSLPublicKey, 'public');
105
106
        $this->assertSame($openSSLPrivateKey, $prikey1);
107
        $this->assertSame($openSSLPublicKey, $pubkey1);
108
        $this->assertSame($openSSLPrivateKey, $prikey2);
109
        $this->assertSame($openSSLPublicKey, $pubkey2);
110
    }
111
112
    public function testRequestApi()
113
    {
114
        $t = $this->timCloud();
115
        $t->offsetSet('im', function ()  {
116
            $m = Mockery::mock('im');
117
            $m->shouldReceive('handle')->withAnyArgs()->andReturn(new ResponseBag([
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'handle'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
            $m->/** @scrutinizer ignore-call */ 
118
                shouldReceive('handle')->withAnyArgs()->andReturn(new ResponseBag([

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.

Loading history...
118
                'ActionStatus' => 'OK'
119
            ], [
120
                'content-type' => 'application/json'
121
            ]));
122
            return $m;
123
        });
124
125
        $c = $t->request(API::DIRTY_WORDS_GET);
126
        $this->assertSame('OK', $c->getContent('ActionStatus'));
127
    }
128
129
    public function timCloud()
130
    {
131
        return new TimCloud([
132
            'app_id'   => phpunit_env('app_id', '1400xxxxxx'),
133
            'identifier' => phpunit_env('identifier', 'common_user'),
134
            'private_key'     => phpunit_env('private_key', 'openssl_private_key'),
135
            'public_key'     => phpunit_env('public_key', 'openssl_public_key'),
136
        ], [
137
            'TLSSig' => function () {
138
                $m = Mockery::mock('TLSSig');
139
                $m->shouldReceive('genSig')->withAnyArgs()->andReturn('test usersig');
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'genSig'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
                $m->/** @scrutinizer ignore-call */ 
140
                    shouldReceive('genSig')->withAnyArgs()->andReturn('test usersig');

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.

Loading history...
140
                return $m;
141
            },
142
        ]);
143
    }
144
145
    public function tearDown()
146
    {
147
        Mockery::close();
148
    }
149
}
150