-
-
Notifications
You must be signed in to change notification settings - Fork 450
/
ClientBuilderTest.php
309 lines (262 loc) · 10.7 KB
/
ClientBuilderTest.php
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
declare(strict_types=1);
namespace Sentry\Tests;
use Http\Client\Common\Plugin as PluginInterface;
use Http\Client\HttpAsyncClient as HttpAsyncClientInterface;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\MessageFactory as MessageFactoryInterface;
use Http\Message\UriFactory as UriFactoryInterface;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise as PromiseInterface;
use Jean85\PrettyVersions;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Event;
use Sentry\FlushableClientInterface;
use Sentry\Integration\ErrorListenerIntegration;
use Sentry\Integration\ExceptionListenerIntegration;
use Sentry\Integration\FatalErrorListenerIntegration;
use Sentry\Integration\IntegrationInterface;
use Sentry\Integration\RequestIntegration;
use Sentry\Integration\TransactionIntegration;
use Sentry\Options;
use Sentry\Transport\HttpTransport;
use Sentry\Transport\NullTransport;
use Sentry\Transport\TransportInterface;
final class ClientBuilderTest extends TestCase
{
/**
* @group legacy
*
* @expectedDeprecationMessage Delaying the sending of the events using the "Sentry\Transport\HttpTransport" class is deprecated since version 2.2 and will not work in 3.0.
*/
public function testHttpTransportIsUsedWhenServerIsConfigured(): void
{
$clientBuilder = ClientBuilder::create(['dsn' => 'http://public:secret@example.com/sentry/1']);
$transport = $this->getObjectAttribute($clientBuilder->getClient(), 'transport');
$this->assertInstanceOf(HttpTransport::class, $transport);
}
public function testNullTransportIsUsedWhenNoServerIsConfigured(): void
{
$clientBuilder = new ClientBuilder();
$transport = $this->getObjectAttribute($clientBuilder->getClient(), 'transport');
$this->assertInstanceOf(NullTransport::class, $transport);
}
/**
* @group legacy
*
* @expectedDeprecation Method Sentry\ClientBuilder::setUriFactory() is deprecated since version 2.3 and will be removed in 3.0.
*/
public function testSetUriFactory(): void
{
/** @var UriFactoryInterface&MockObject $uriFactory */
$uriFactory = $this->createMock(UriFactoryInterface::class);
$uriFactory->expects($this->once())
->method('createUri')
->willReturn(UriFactoryDiscovery::find()->createUri('http://www.example.com'));
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setUriFactory($uriFactory)
->getClient();
$client->captureMessage('foo');
}
/**
* @group legacy
*
* @expectedDeprecationMessage Delaying the sending of the events using the "Sentry\Transport\HttpTransport" class is deprecated since version 2.2 and will not work in 3.0.
*/
public function testSetMessageFactory(): void
{
/** @var MessageFactoryInterface&MockObject $messageFactory */
$messageFactory = $this->createMock(MessageFactoryInterface::class);
$messageFactory->expects($this->once())
->method('createRequest')
->willReturn(MessageFactoryDiscovery::find()->createRequest('POST', 'http://www.example.com'));
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setMessageFactory($messageFactory)
->getClient();
$client->captureMessage('foo');
}
/**
* @group legacy
*
* @expectedDeprecation Method Sentry\ClientBuilder::setTransport() is deprecated since version 2.3 and will be removed in 3.0. Use the setTransportFactory() method instead.
*/
public function testSetTransport(): void
{
/** @var TransportInterface&MockObject $transport */
$transport = $this->createMock(TransportInterface::class);
$transport->expects($this->once())
->method('send')
->willReturn('ddb4a0b9ab1941bf92bd2520063663e3');
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setTransport($transport)
->getClient();
$this->assertSame('ddb4a0b9ab1941bf92bd2520063663e3', $client->captureMessage('foo'));
}
/**
* @group legacy
*
* @expectedDeprecationMessage Delaying the sending of the events using the "Sentry\Transport\HttpTransport" class is deprecated since version 2.2 and will not work in 3.0.
*/
public function testSetHttpClient(): void
{
/** @var HttpAsyncClientInterface&MockObject $httpClient */
$httpClient = $this->createMock(HttpAsyncClientInterface::class);
$httpClient->expects($this->once())
->method('sendAsyncRequest')
->willReturn(new FulfilledPromise(true));
/** @var FlushableClientInterface $client */
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->setHttpClient($httpClient)
->getClient();
$client->captureMessage('foo');
$client->flush();
}
/**
* @group legacy
*
* @expectedDeprecationMessage Method Sentry\ClientBuilder::addHttpClientPlugin() is deprecated since version 2.3 and will be removed in 3.0.
*/
public function testAddHttpClientPlugin(): void
{
/** @var PluginInterface&MockObject $plugin */
$plugin = $this->createMock(PluginInterface::class);
$plugin->expects($this->once())
->method('handleRequest')
->willReturn(new FulfilledPromise(true));
/** @var FlushableClientInterface $client */
$client = ClientBuilder::create(['dsn' => 'http://public@example.com/sentry/1'])
->addHttpClientPlugin($plugin)
->getClient();
$client->captureMessage('foo');
$client->flush();
}
/**
* @group legacy
*
* @expectedDeprecation Method Sentry\ClientBuilder::addHttpClientPlugin() is deprecated since version 2.3 and will be removed in 3.0.
* @expectedDeprecation Method Sentry\ClientBuilder::removeHttpClientPlugin() is deprecated since version 2.3 and will be removed in 3.0.
*/
public function testRemoveHttpClientPlugin(): void
{
$plugin = new class() implements PluginInterface {
public function handleRequest(RequestInterface $request, callable $next, callable $first): PromiseInterface
{
return new FulfilledPromise(true);
}
};
$plugin2 = new class() implements PluginInterface {
public function handleRequest(RequestInterface $request, callable $next, callable $first): PromiseInterface
{
return new FulfilledPromise(true);
}
};
/** @var FlushableClientInterface $client */
$client = ClientBuilder::create()
->addHttpClientPlugin($plugin)
->addHttpClientPlugin($plugin)
->addHttpClientPlugin($plugin2)
->removeHttpClientPlugin(\get_class($plugin2))
->getClient();
$client->captureMessage('foo');
$client->flush();
}
/**
* @dataProvider integrationsAreAddedToClientCorrectlyDataProvider
*/
public function testIntegrationsAreAddedToClientCorrectly(bool $defaultIntegrations, array $integrations, array $expectedIntegrations): void
{
$options = new Options();
$options->setDefaultIntegrations($defaultIntegrations);
$options->setIntegrations($integrations);
$client = (new ClientBuilder($options))->getClient();
$actualIntegrationsClassNames = array_map('\get_class', $client->getOptions()->getIntegrations());
$this->assertEquals($expectedIntegrations, $actualIntegrationsClassNames, '', 0, 10, true);
}
public function integrationsAreAddedToClientCorrectlyDataProvider(): array
{
return [
[
false,
[],
[],
],
[
false,
[new StubIntegration()],
[StubIntegration::class],
],
[
true,
[],
[
ErrorListenerIntegration::class,
FatalErrorListenerIntegration::class,
ExceptionListenerIntegration::class,
RequestIntegration::class,
TransactionIntegration::class,
],
],
[
true,
[new StubIntegration()],
[
ErrorListenerIntegration::class,
FatalErrorListenerIntegration::class,
ExceptionListenerIntegration::class,
RequestIntegration::class,
TransactionIntegration::class,
StubIntegration::class,
],
],
];
}
public function testClientBuilderFallbacksToDefaultSdkIdentifierAndVersion(): void
{
$callbackCalled = false;
$expectedVersion = PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion();
$options = new Options();
$options->setBeforeSendCallback(function (Event $event) use ($expectedVersion, &$callbackCalled) {
$callbackCalled = true;
$this->assertSame(Client::SDK_IDENTIFIER, $event->getSdkIdentifier());
$this->assertSame($expectedVersion, $event->getSdkVersion());
return null;
});
(new ClientBuilder($options))->getClient()->captureMessage('test');
$this->assertTrue($callbackCalled, 'Callback not invoked, no assertions performed');
}
public function testClientBuilderSetsSdkIdentifierAndVersion(): void
{
$callbackCalled = false;
$options = new Options();
$options->setBeforeSendCallback(function (Event $event) use (&$callbackCalled) {
$callbackCalled = true;
$this->assertSame('sentry.test', $event->getSdkIdentifier());
$this->assertSame('1.2.3-test', $event->getSdkVersion());
return null;
});
(new ClientBuilder($options))
->setSdkIdentifier('sentry.test')
->setSdkVersion('1.2.3-test')
->getClient()
->captureMessage('test');
$this->assertTrue($callbackCalled, 'Callback not invoked, no assertions performed');
}
public function testCreateWithNoOptionsIsTheSameAsDefaultOptions(): void
{
$this->assertEquals(
new ClientBuilder(new Options()),
ClientBuilder::create([])
);
}
}
final class StubIntegration implements IntegrationInterface
{
public function setupOnce(): void
{
}
}