Completed
Push — master ( d4f713...589745 )
by Jonathan
12s queued 11s
created

testExecuteThrowsRuntimeExceptionOnNon200()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 38
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator\Tests;
6
7
use ChangelogGenerator\GitHubUsernamePassword;
8
use ChangelogGenerator\IssueClient;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Http\Client\ClientInterface;
12
use Psr\Http\Message\RequestFactoryInterface;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\StreamInterface;
16
use RuntimeException;
17
18
final class IssueClientTest extends TestCase
19
{
20
    /** @var RequestFactoryInterface|MockObject */
21
    private $messageFactory;
22
23
    /** @var ClientInterface|MockObject */
24
    private $client;
25
26
    /** @var IssueClient */
27
    private $issueClient;
28
29
    public function testExecute() : void
30
    {
31
        $request  = $this->createMock(RequestInterface::class);
32
        $response = $this->createMock(ResponseInterface::class);
33
34
        $this->messageFactory->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Message\RequestFactoryInterface. ( Ignorable by Annotation )

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

34
        $this->messageFactory->/** @scrutinizer ignore-call */ 
35
                               expects(self::once())

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...
35
            ->method('createRequest')
36
            ->with('GET', 'https://www.google.com')
37
            ->willReturn($request);
38
39
        $request->expects(self::once())
40
            ->method('withAddedHeader')
41
            ->with('User-Agent', 'jwage/changelog-generator')
42
            ->willReturn($request);
43
44
        $this->client->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Client\ClientInterface. ( Ignorable by Annotation )

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

44
        $this->client->/** @scrutinizer ignore-call */ 
45
                       expects(self::once())

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...
45
            ->method('sendRequest')
46
            ->with($request)
47
            ->willReturn($response);
48
49
        $stream = $this->createMock(StreamInterface::class);
50
51
        $response->expects(self::once())
52
            ->method('getBody')
53
            ->willReturn($stream);
54
55
        $stream->expects(self::once())
56
            ->method('__toString')
57
            ->willReturn('{"test": true}');
58
59
        $response->expects(self::once())
60
            ->method('getStatusCode')
61
            ->willReturn(200);
62
63
        $response->expects(self::once())
64
            ->method('getHeader')
65
            ->with('Link')
66
            ->willReturn(['<https://www.google.com?next>; rel="next", <https://www.google.com?last>; rel="last"']);
67
68
        $response = $this->issueClient->execute('https://www.google.com');
69
70
        self::assertEquals(['test' => true], $response->getBody());
71
        self::assertEquals('https://www.google.com?next', $response->getNextUrl());
72
    }
73
74
    public function testExecuteNullNextUrl() : void
75
    {
76
        $request  = $this->createMock(RequestInterface::class);
77
        $response = $this->createMock(ResponseInterface::class);
78
79
        $this->messageFactory->expects(self::once())
80
            ->method('createRequest')
81
            ->with('GET', 'https://www.google.com')
82
            ->willReturn($request);
83
84
        $request->expects(self::once())
85
            ->method('withAddedHeader')
86
            ->with('User-Agent', 'jwage/changelog-generator')
87
            ->willReturn($request);
88
89
        $this->client->expects(self::once())
90
            ->method('sendRequest')
91
            ->with($request)
92
            ->willReturn($response);
93
94
        $stream = $this->createMock(StreamInterface::class);
95
96
        $response->expects(self::once())
97
            ->method('getBody')
98
            ->willReturn($stream);
99
100
        $stream->expects(self::once())
101
            ->method('__toString')
102
            ->willReturn('{"test": true}');
103
104
        $response->expects(self::once())
105
            ->method('getStatusCode')
106
            ->willReturn(200);
107
108
        $response->expects(self::once())
109
            ->method('getHeader')
110
            ->with('Link')
111
            ->willReturn([]);
112
113
        $response = $this->issueClient->execute('https://www.google.com');
114
115
        self::assertEquals(['test' => true], $response->getBody());
116
        self::assertNull($response->getNextUrl());
117
    }
118
119
    public function testExecuteThrowsRuntimeExceptionOnNon200() : void
120
    {
121
        $request  = $this->createMock(RequestInterface::class);
122
        $response = $this->createMock(ResponseInterface::class);
123
124
        $this->messageFactory->expects(self::once())
125
            ->method('createRequest')
126
            ->with('GET', 'https://www.google.com')
127
            ->willReturn($request);
128
129
        $request->expects(self::once())
130
            ->method('withAddedHeader')
131
            ->with('User-Agent', 'jwage/changelog-generator')
132
            ->willReturn($request);
133
134
        $this->client->expects(self::once())
135
            ->method('sendRequest')
136
            ->with($request)
137
            ->willReturn($response);
138
139
        $stream = $this->createMock(StreamInterface::class);
140
141
        $response->expects(self::once())
142
            ->method('getBody')
143
            ->willReturn($stream);
144
145
        $stream->expects(self::once())
146
            ->method('__toString')
147
            ->willReturn('{"message": "It failed yo!"}');
148
149
        $response->expects(self::once())
150
            ->method('getStatusCode')
151
            ->willReturn(400);
152
153
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

153
        self::/** @scrutinizer ignore-call */ 
154
              expectException(RuntimeException::class);
Loading history...
154
        self::expectExceptionMessage('API call to GitHub failed with status code 400 and message "It failed yo!"');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

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

154
        self::/** @scrutinizer ignore-call */ 
155
              expectExceptionMessage('API call to GitHub failed with status code 400 and message "It failed yo!"');
Loading history...
155
156
        $this->issueClient->execute('https://www.google.com');
157
    }
158
159
    public function testExecuteWithGitHubCredentials() : void
160
    {
161
        $request  = $this->createMock(RequestInterface::class);
162
        $response = $this->createMock(ResponseInterface::class);
163
164
        $this->messageFactory->expects(self::once())
165
            ->method('createRequest')
166
            ->with('GET', 'https://www.google.com')
167
            ->willReturn($request);
168
169
        $request->expects(self::at(0))
170
            ->method('withAddedHeader')
171
            ->with('User-Agent', 'jwage/changelog-generator')
172
            ->willReturn($request);
173
174
        $request->expects(self::at(1))
175
            ->method('withAddedHeader')
176
            ->with('Authorization', 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=')
177
            ->willReturn($request);
178
179
        $this->client->expects(self::once())
180
            ->method('sendRequest')
181
            ->with($request)
182
            ->willReturn($response);
183
184
        $stream = $this->createMock(StreamInterface::class);
185
186
        $response->expects(self::once())
187
            ->method('getBody')
188
            ->willReturn($stream);
189
190
        $stream->expects(self::once())
191
            ->method('__toString')
192
            ->willReturn('{"test": true}');
193
194
        $response->expects(self::once())
195
            ->method('getStatusCode')
196
            ->willReturn(200);
197
198
        $response->expects(self::once())
199
            ->method('getHeader')
200
            ->with('Link')
201
            ->willReturn([]);
202
203
        $response = $this->issueClient->execute(
204
            'https://www.google.com',
205
            new GitHubUsernamePassword('username', 'password')
206
        );
207
208
        self::assertEquals(['test' => true], $response->getBody());
209
    }
210
211
    protected function setUp() : void
212
    {
213
        $this->messageFactory = $this->createMock(RequestFactoryInterface::class);
214
        $this->client         = $this->createMock(ClientInterface::class);
215
216
        $this->issueClient = new IssueClient($this->messageFactory, $this->client);
217
    }
218
}
219