Issues (1)

test/Unit/Application/SpotifyApiAuthTest.php (1 issue)

1
<?php declare(strict_types=1);
2
3
namespace SpotifyApiConnectTest\Unit\Application;
4
5
use PHPUnit\Framework\MockObject\MockObject;
6
use PHPUnit\Framework\TestCase;
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(): void
15
    {
16
        $sessionMock = $this->getSessionMock();
17
18
        $sessionMock->method('getAuthorizeUrl')
19
            ->willReturn('getAuthorizeUrl-mock');
20
21
        $spotifyApiAuth = new SpotifyApiAuth($sessionMock);
22
        $this->assertSame(
23
            'getAuthorizeUrl-mock',
24
            $spotifyApiAuth->getAuthorizeUrlForPlaylistModifyPublic()
25
        );
26
    }
27
28
    public function testGetRefreshTokenByCode(): void
29
    {
30
        $sessionMock = $this->getSessionMock();
31
        $sessionMock->method('requestAccessToken')
32
            ->willReturn(true);
33
34
        $sessionMock->method('getRefreshToken')
35
            ->willReturn('unit-test');
36
37
        $spotifyApiAuth = new SpotifyApiAuth($sessionMock);
38
        $this->assertSame(
39
            'unit-test',
40
            $spotifyApiAuth->getRefreshTokenByCode('unit')
41
        );
42
    }
43
44
    public function testGetRefreshTokenByCodeWithException(): void
45
    {
46
        $sessionMock = $this->getSessionMock();
47
        $sessionMock->method('requestAccessToken')
48
            ->willReturn(false);
49
50
        try {
51
            $spotifyApiAuth = new SpotifyApiAuth($sessionMock);
52
            $spotifyApiAuth->getRefreshTokenByCode('unit');
53
        } catch (\RuntimeException $e) {
54
            $this->assertSame(
55
                Message::ERROR_GET_REQUEST_TOKEN_BY_CODE,
56
                $e->getMessage()
57
            );
58
            return;
59
        }
60
61
        $this->fail();
62
    }
63
64
    public function testGetAccessByRefreshToken(): void
65
    {
66
        $sessionMock = $this->getSessionMock();
67
        $sessionMock->method('refreshAccessToken')
68
            ->willReturn(true);
69
70
        $sessionMock->method('getAccessToken')
71
            ->willReturn('unit-test');
72
73
        $spotifyApiAuth = new SpotifyApiAuth($sessionMock);
74
        $this->assertSame(
75
            'unit-test',
76
            $spotifyApiAuth->getAccessByRefreshToken('unit')
77
        );
78
    }
79
80
    public function testGetAccessByRefreshTokenWithException(): void
81
    {
82
        $sessionMock = $this->getSessionMock();
83
        $sessionMock->method('refreshAccessToken')
84
            ->willReturn(false);
85
86
        try {
87
            $spotifyApiAuth = new SpotifyApiAuth($sessionMock);
88
            $spotifyApiAuth->getAccessByRefreshToken('unit');
89
        } catch (\RuntimeException $e) {
90
            $this->assertSame(
91
                Message::ERROR_GET_REFRESH_TOKEN_BY_CODE,
92
                $e->getMessage()
93
            );
94
            return;
95
        }
96
97
        $this->fail();
98
    }
99
100
    /**
101
     * @return MockObject
102
     * @throws \ReflectionException
103
     */
104
    private function getSessionMock(): MockObject
105
    {
106
        $mockObject = $this->getMockBuilder(SessionInterface::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

106
        $mockObject = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(SessionInterface::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...
107
            ->setMethods([
108
                'getAuthorizeUrl',
109
                'getAccessToken',
110
                'getRefreshToken',
111
                'refreshAccessToken',
112
                'requestAccessToken',
113
            ]);
114
115
        return $mockObject->getMock();
116
    }
117
118
}
119