1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpotifyApiConnectTest\Unit\Application; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use SebastianBergmann\Timer\RuntimeException; |
7
|
|
|
use SpotifyApiConnect\Application\SpotifyApiAuth; |
8
|
|
|
use SpotifyApiConnect\Application\SpotifyWebApiPhp\SessionInterface; |
9
|
|
|
use SpotifyApiConnect\Message; |
10
|
|
|
|
11
|
|
|
class SpotifyApiAuthTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public function testGetAuthorizeUrlForPlaylistModifyPublic() |
15
|
|
|
{ |
16
|
|
|
$sessionMock = $this->getSessionMock(); |
17
|
|
|
$sessionMock->method('getAuthorizeUrl') |
|
|
|
|
18
|
|
|
->will($this->returnValue('getAuthorizeUrl-mock')); |
19
|
|
|
|
20
|
|
|
$spotifyApiAuth = new SpotifyApiAuth($sessionMock); |
21
|
|
|
$this->assertSame( |
22
|
|
|
'getAuthorizeUrl-mock', |
23
|
|
|
$spotifyApiAuth->getAuthorizeUrlForPlaylistModifyPublic() |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetRefreshTokenByCode() |
28
|
|
|
{ |
29
|
|
|
$sessionMock = $this->getSessionMock(); |
30
|
|
|
$sessionMock->method('requestAccessToken') |
31
|
|
|
->will($this->returnValue(true)); |
32
|
|
|
|
33
|
|
|
$sessionMock->method('getRefreshToken') |
34
|
|
|
->will($this->returnValue('unit-test')); |
35
|
|
|
|
36
|
|
|
$spotifyApiAuth = new SpotifyApiAuth($sessionMock); |
37
|
|
|
$this->assertSame( |
38
|
|
|
'unit-test', |
39
|
|
|
$spotifyApiAuth->getRefreshTokenByCode('unit') |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetRefreshTokenByCodeWithException() |
44
|
|
|
{ |
45
|
|
|
$sessionMock = $this->getSessionMock(); |
46
|
|
|
$sessionMock->method('requestAccessToken') |
47
|
|
|
->will($this->returnValue(false)); |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$spotifyApiAuth = new SpotifyApiAuth($sessionMock); |
51
|
|
|
$spotifyApiAuth->getRefreshTokenByCode('unit'); |
52
|
|
|
} catch (\RuntimeException $e) { |
53
|
|
|
$this->assertSame( |
54
|
|
|
Message::ERROR_GET_REQUEST_TOKEN_BY_CODE, |
55
|
|
|
$e->getMessage() |
56
|
|
|
); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->fail(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetAccessByRefreshToken() |
64
|
|
|
{ |
65
|
|
|
$sessionMock = $this->getSessionMock(); |
66
|
|
|
$sessionMock->method('refreshAccessToken') |
67
|
|
|
->will($this->returnValue(true)); |
68
|
|
|
|
69
|
|
|
$sessionMock->method('getAccessToken') |
70
|
|
|
->will($this->returnValue('unit-test')); |
71
|
|
|
|
72
|
|
|
$spotifyApiAuth = new SpotifyApiAuth($sessionMock); |
73
|
|
|
$this->assertSame( |
74
|
|
|
'unit-test', |
75
|
|
|
$spotifyApiAuth->getAccessByRefreshToken('unit') |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetAccessByRefreshTokenWithException() |
80
|
|
|
{ |
81
|
|
|
$sessionMock = $this->getSessionMock(); |
82
|
|
|
$sessionMock->method('refreshAccessToken') |
83
|
|
|
->will($this->returnValue(false)); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$spotifyApiAuth = new SpotifyApiAuth($sessionMock); |
87
|
|
|
$spotifyApiAuth->getAccessByRefreshToken('unit'); |
88
|
|
|
} catch (\RuntimeException $e) { |
89
|
|
|
$this->assertSame( |
90
|
|
|
Message::ERROR_GET_REFRESH_TOKEN_BY_CODE, |
91
|
|
|
$e->getMessage() |
92
|
|
|
); |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->fail(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject |
101
|
|
|
*/ |
102
|
|
|
protected function getSessionMock(): \PHPUnit\Framework\MockObject\MockObject |
103
|
|
|
{ |
104
|
|
|
return $this->getMockBuilder(SessionInterface::class) |
105
|
|
|
->setMethods([ |
106
|
|
|
'getAuthorizeUrl', |
107
|
|
|
'getAccessToken', |
108
|
|
|
'getRefreshToken', |
109
|
|
|
'refreshAccessToken', |
110
|
|
|
'requestAccessToken', |
111
|
|
|
]) |
112
|
|
|
->getMock(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.