Passed
Push — master ( 27dd3a...31fea7 )
by Alex
06:49 queued 02:55
created

AdoptedBaseAuth::getOauthUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\SocialNetwork\Auth\Tests;
3
4
use Mezon\SocialNetwork\BaseAuth;
5
use PHPUnit\Framework\TestCase;
6
7
class AdoptedBaseAuth extends BaseAuth
8
{
9
10
    public function getUserInfoUri(string $token = ''): string
11
    {
12
        return 'http://user-info-uri/?' . $token;
13
    }
14
15
    public function getTokenUri(): string
16
    {
17
        return 'http://token-uri';
18
    }
19
20
    public function getOauthUri(): string
21
    {
22
        return 'http://oauth-uri';
23
    }
24
}
25
26
class BaseAuthUnitTest extends TestCase
27
{
28
29
    /**
30
     * Method returns fake settings
31
     *
32
     * @return array fake settings
33
     */
34
    protected function getSettings(): array
35
    {
36
        return ([
37
            'client_id' => 1,
38
            'client_secret' => 2,
39
            'redirect_uri' => 3
40
        ]);
41
    }
42
43
    /**
44
     * Testing constructor
45
     */
46
    public function testConstructor()
47
    {
48
        // setup and test body
49
        $Auth = new AdoptedBaseAuth($this->getSettings());
50
51
        // assertions
52
        $this->assertEquals(3, count($Auth->settings), 'Setting were not set');
53
    }
54
55
    /**
56
     * Testing get_link
57
     */
58
    public function testGetLink()
59
    {
60
        // setup
61
        $auth = new AdoptedBaseAuth($this->getSettings());
62
63
        // test body
64
        $link = $auth->getLink();
65
66
        // assertions
67
        $this->assertStringContainsString(
68
            'http://oauth-uriclient_id=1&redirect_uri=3&response_type=code',
69
            $link,
70
            'Invalid link was generated');
71
    }
72
73
    /**
74
     * Testing get_link exception
75
     */
76
    public function testGetLinkException()
77
    {
78
        // assertions
79
        $this->expectException(\Exception::class);
80
81
        // setup
82
        $auth = new AdoptedBaseAuth([]);
83
84
        // test body
85
        $auth->getLink();
86
    }
87
88
    /**
89
     * Testing getUserInfoUri
90
     */
91
    public function testGetUserInfoUri()
92
    {
93
        // setup
94
        $auth = new AdoptedBaseAuth($this->getSettings());
95
96
        // test body
97
        $link = $auth->getUserInfoUri();
98
99
        // assertions
100
        $this->assertStringContainsString('://user-info-uri/?', $link, 'Invalid user info URI');
101
    }
102
103
    /**
104
     * Testing get_token_params method
105
     */
106
    public function testGetTokenParams()
107
    {
108
        // setup
109
        $auth = new AdoptedBaseAuth($this->getSettings());
110
111
        // test body
112
        $params = $auth->getTokenParams(123);
113
114
        // assertions
115
        $this->assertEquals(1, $params['client_id'], 'Invalid "client_id"');
116
        $this->assertEquals(2, $params['client_secret'], 'Invalid "client_secret"');
117
        $this->assertEquals(3, $params['redirect_uri'], 'Invalid "redirect_uri"');
118
        $this->assertEquals(123, $params['code'], 'Invalid "code"');
119
    }
120
121
    /**
122
     * Testing get_token_uri
123
     */
124
    public function testGetTokenUri()
125
    {
126
        // setup
127
        $auth = new AdoptedBaseAuth($this->getSettings());
128
129
        // test body
130
        $link = $auth->getTokenUri();
131
132
        // assertions
133
        $this->assertStringContainsString('://token-uri', $link, 'Invalid token URI');
134
    }
135
136
    /**
137
     * Testing get_desired_fields
138
     */
139
    public function testGetDesiredFields()
140
    {
141
        // setup
142
        $auth = new AdoptedBaseAuth($this->getSettings());
143
144
        // test body
145
        $fields = $auth->getDesiredFields();
146
147
        // assertions
148
        $this->assertStringContainsString('desired,fields', $fields, 'Invalid token URI');
149
    }
150
151
    /**
152
     * Testing 'dispatchUserInfo' method
153
     */
154
    public function testDispatchUserInfo()
155
    {
156
        // setup
157
        $auth = new AdoptedBaseAuth($this->getSettings());
158
        $userInfo = [
159
            'picture' => [
160
                'data' => [
161
                    'url' => 'image url'
162
                ]
163
            ]
164
        ];
165
166
        // test body
167
        $userInfo = $auth->dispatchUserInfo($userInfo);
168
169
        // assertions
170
        $this->assertIsString($userInfo['picture'], 'Record was not transformed');
171
    }
172
173
    /**
174
     * Testing 'auth' method
175
     */
176
    public function testAuth()
177
    {
178
        // setup
179
        $auth = $this->getMockBuilder(AdoptedBaseAuth::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

179
        $auth = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(AdoptedBaseAuth::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
180
            ->setMethods([
181
            'getRequest',
182
            'requestToken'
183
        ])
184
            ->setConstructorArgs([
185
            $this->getSettings()
186
        ])
187
            ->getMock();
188
        $auth->method('getRequest')->willReturn(
189
            json_encode([
190
                'id' => 1,
191
                'picture' => [
192
                    'data' => [
193
                        'url' => 'http://'
194
                    ]
195
                ]
196
            ]));
197
198
        $auth->method('requestToken')->willReturn([
199
            'access_token' => 'some-token'
200
        ]);
201
202
        // test body
203
        $result = $auth->auth('some-code');
204
205
        // assertions
206
        $this->assertTrue($result, 'Auth was not performed');
207
    }
208
}
209