Completed
Push — master ( 379c63...bb81c5 )
by Rafal
22s
created

SpotifyApiAuthTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 58
dl 0
loc 102
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetRefreshTokenByCode() 0 13 1
A testGetAccessByRefreshToken() 0 13 1
A testGetRefreshTokenByCodeWithException() 0 18 2
A getSessionMock() 0 11 1
A testGetAccessByRefreshTokenWithException() 0 18 2
A testGetAuthorizeUrlForPlaylistModifyPublic() 0 10 1
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')
1 ignored issue
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

17
        $sessionMock->/** @scrutinizer ignore-call */ 
18
                      method('getAuthorizeUrl')

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.

Loading history...
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