-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiTest.php
More file actions
230 lines (176 loc) · 7.12 KB
/
ApiTest.php
File metadata and controls
230 lines (176 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace ProgrammatorDev\Api\Test\Integration;
use Http\Message\Authentication;
use Http\Mock\Client;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\Attributes\DataProvider;
use ProgrammatorDev\Api\Api;
use ProgrammatorDev\Api\Builder\CacheBuilder;
use ProgrammatorDev\Api\Builder\ClientBuilder;
use ProgrammatorDev\Api\Builder\LoggerBuilder;
use ProgrammatorDev\Api\Event\PreRequestEvent;
use ProgrammatorDev\Api\Event\ResponseContentsEvent;
use ProgrammatorDev\Api\Test\AbstractTestCase;
use ProgrammatorDev\Api\Test\MockResponse;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
class ApiTest extends AbstractTestCase
{
private const BASE_URL = 'https://base.com/url';
private Api $api;
private Client $mockClient;
protected function setUp(): void
{
parent::setUp();
// create anonymous class
$this->api = new class extends Api {};
// set mock client
$this->mockClient = new Client();
$this->api->setClientBuilder(new ClientBuilder($this->mockClient));
}
public function testRequest()
{
$this->mockClient->addResponse(new Response(body: MockResponse::SUCCESS));
$response = $this->api->request(
method: 'GET',
path: '/path'
);
$this->assertSame(MockResponse::SUCCESS, $response);
}
public function testBaseUrl()
{
$this->assertNull($this->api->getBaseUrl());
$this->api->setBaseUrl(self::BASE_URL);
$this->assertSame(self::BASE_URL, $this->api->getBaseUrl());
}
public function testQueryDefaults()
{
$this->api->addQueryDefault('test', true);
$this->assertTrue($this->api->getQueryDefault('test'));
$this->api->removeQueryDefault('test');
$this->assertNull($this->api->getQueryDefault('test'));
}
public function testHeaderDefaults()
{
$this->api->addHeaderDefault('X-Test', true);
$this->assertTrue($this->api->getHeaderDefault('X-Test'));
$this->api->removeHeaderDefault('X-Test');
$this->assertNull($this->api->getHeaderDefault('X-Test'));
}
public function testCache()
{
$this->assertNull($this->api->getCacheBuilder());
$cachePool = $this->createMock(CacheItemPoolInterface::class);
$this->api->setCacheBuilder(new CacheBuilder($cachePool));
$cachePool->expects($this->once())->method('save');
$this->api->request(
method: 'GET',
path: '/path'
);
}
public function testLogger()
{
$this->assertNull($this->api->getLoggerBuilder());
$logger = $this->createMock(LoggerInterface::class);
$this->api->setLoggerBuilder(new LoggerBuilder($logger));
// count equals 2 because of the request and response log
$logger->expects($this->exactly(2))->method('info');
$this->api->request(
method: 'GET',
path: '/path'
);
}
public function testCacheWithLogger()
{
$this->assertNull($this->api->getCacheBuilder());
$this->assertNull($this->api->getLoggerBuilder());
$cachePool = $this->createMock(CacheItemPoolInterface::class);
$logger = $this->createMock(LoggerInterface::class);
$this->api->setCacheBuilder(new CacheBuilder($cachePool));
$this->api->setLoggerBuilder(new LoggerBuilder($logger));
// count equals 3 because of the request, response and cache log
$logger->expects($this->exactly(3))->method('info');
// error suppression to hide expected warning of null cache item in CacheLoggerListener
// https://docs.phpunit.de/en/10.5/error-handling.html#ignoring-issue-suppression
// TODO maybe allow user to add cache listeners to CacheBuilder and create a mock?
@$this->api->request(
method: 'GET',
path: '/path'
);
}
public function testAuthentication()
{
$this->assertNull($this->api->getAuthentication());
$authentication = $this->createConfiguredMock(Authentication::class, [
'authenticate' => $this->createMock(RequestInterface::class)
]);
$this->api->setAuthentication($authentication);
$authentication->expects($this->once())->method('authenticate');
$this->api->request(
method: 'GET',
path: '/path'
);
}
public function testPreRequestListener()
{
$this->api->addPreRequestListener(fn() => throw new \Exception('TestMessage'));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('TestMessage');
$this->api->request(
method: 'GET',
path: '/path'
);
}
public function testPostRequestListener()
{
$this->api->addPostRequestListener(fn() => throw new \Exception('TestMessage'));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('TestMessage');
$this->api->request(
method: 'GET',
path: '/path'
);
}
public function testResponseContentsListener()
{
$this->mockClient->addResponse(new Response(body: MockResponse::SUCCESS));
$this->api->addResponseContentsListener(function(ResponseContentsEvent $event) {
$contents = json_decode($event->getContents(), true);
$event->setContents($contents);
});
$response = $this->api->request(
method: 'GET',
path: '/path'
);
$this->assertIsArray($response);
}
#[DataProvider('provideBuildUrlData')]
public function testBuildUrl(?string $baseUrl, string $path, array $query, string $expectedUrl)
{
$this->api->addPreRequestListener(function(PreRequestEvent $event) use ($expectedUrl) {
$url = (string) $event->getRequest()->getUri();
$this->assertSame($expectedUrl, $url);
});
$this->api->setBaseUrl($baseUrl);
$this->api->request(method: 'GET', path: $path, query: $query);
}
public static function provideBuildUrlData(): \Generator
{
yield 'no base url' => [null, '/path', [], '/path'];
yield 'base url' => [self::BASE_URL, '/path', [], 'https://base.com/url/path'];
yield 'path full url' => [self::BASE_URL, 'https://fullurl.com/path', [], 'https://fullurl.com/path'];
yield 'duplicated slashes' => [self::BASE_URL, '////path', [], 'https://base.com/url/path'];
yield 'query' => [self::BASE_URL, '/path', ['foo' => 'bar'], 'https://base.com/url/path?foo=bar'];
yield 'path query' => [self::BASE_URL, '/path?test=true', ['foo' => 'bar'], 'https://base.com/url/path?test=true&foo=bar'];
yield 'query replace' => [self::BASE_URL, '/path?test=true', ['test' => 'false'], 'https://base.com/url/path?test=false'];
}
public function testBuildPath()
{
$path = $this->api->buildPath('/path/{parameter1}/multiple/{parameter2}', [
'parameter1' => 'with',
'parameter2' => 'parameters'
]);
$this->assertSame('/path/with/multiple/parameters', $path);
}
}