3
3
namespace Sentry \SentryBundle \Test \DependencyInjection ;
4
4
5
5
use PHPUnit \Framework \TestCase ;
6
+ use Sentry \Breadcrumb ;
6
7
use Sentry \Event ;
7
8
use Sentry \Options ;
8
9
use Sentry \SentryBundle \DependencyInjection \SentryExtension ;
@@ -89,6 +90,7 @@ public function optionsValueProvider(): array
89
90
{
90
91
return [
91
92
['attach_stacktrace ' , true , 'shouldAttachStacktrace ' ],
93
+ ['before_breadcrumb ' , __NAMESPACE__ . '\mockBeforeBreadcrumb ' , 'getBeforeBreadcrumbCallback ' ],
92
94
['before_send ' , __NAMESPACE__ . '\mockBeforeSend ' , 'getBeforeSendCallback ' ],
93
95
['context_lines ' , 1 ],
94
96
['default_integrations ' , false , 'hasDefaultIntegrations ' ],
@@ -153,7 +155,7 @@ public function testBeforeSendUsingServiceDefinition(): void
153
155
{
154
156
$ container = $ this ->getContainer ([
155
157
'options ' => [
156
- 'before_send ' => '@before_send ' ,
158
+ 'before_send ' => '@callable_mock ' ,
157
159
],
158
160
]);
159
161
@@ -166,13 +168,13 @@ public function testBeforeSendUsingServiceDefinition(): void
166
168
'before_send closure has not been replaced, is the default one '
167
169
);
168
170
$ this ->assertEquals (
169
- CallbackMock::createBeforeSendCallback (),
171
+ CallbackMock::createCallback (),
170
172
$ beforeSendCallback
171
173
);
172
174
}
173
175
174
176
/**
175
- * @dataProvider beforeSendDataProvider
177
+ * @dataProvider scalarCallableDataProvider
176
178
*/
177
179
public function testBeforeSendUsingScalarCallable ($ scalarCallable ): void
178
180
{
@@ -196,26 +198,86 @@ public function testBeforeSendUsingScalarCallable($scalarCallable): void
196
198
);
197
199
}
198
200
199
- public function beforeSendDataProvider (): array
201
+ public function testBeforeSendWithInvalidServiceReference (): void
202
+ {
203
+ $ container = $ this ->getContainer ([
204
+ 'options ' => [
205
+ 'before_send ' => '@event_dispatcher ' ,
206
+ ],
207
+ ]);
208
+
209
+ $ this ->expectException (\TypeError::class);
210
+
211
+ $ this ->getOptionsFrom ($ container )->getBeforeSendCallback ();
212
+ }
213
+
214
+ public function testBeforeBreadcrumbUsingServiceDefinition (): void
215
+ {
216
+ $ container = $ this ->getContainer ([
217
+ 'options ' => [
218
+ 'before_breadcrumb ' => '@callable_mock ' ,
219
+ ],
220
+ ]);
221
+
222
+ $ beforeBreadcrumbCallback = $ this ->getOptionsFrom ($ container )->getBeforeBreadcrumbCallback ();
223
+ $ this ->assertIsCallable ($ beforeBreadcrumbCallback );
224
+ $ defaultOptions = $ this ->getOptionsFrom ($ this ->getContainer ());
225
+ $ this ->assertNotEquals (
226
+ $ defaultOptions ->getBeforeBreadcrumbCallback (),
227
+ $ beforeBreadcrumbCallback ,
228
+ 'before_breadcrumb closure has not been replaced, is the default one '
229
+ );
230
+ $ this ->assertEquals (
231
+ CallbackMock::createCallback (),
232
+ $ beforeBreadcrumbCallback
233
+ );
234
+ }
235
+
236
+ /**
237
+ * @dataProvider scalarCallableDataProvider
238
+ */
239
+ public function testBeforeBreadcrumbUsingScalarCallable ($ scalarCallable ): void
240
+ {
241
+ $ container = $ this ->getContainer ([
242
+ 'options ' => [
243
+ 'before_breadcrumb ' => $ scalarCallable ,
244
+ ],
245
+ ]);
246
+
247
+ $ beforeBreadcrumbCallback = $ this ->getOptionsFrom ($ container )->getBeforeBreadcrumbCallback ();
248
+ $ this ->assertIsCallable ($ beforeBreadcrumbCallback );
249
+ $ defaultOptions = $ this ->getOptionsFrom ($ this ->getContainer ());
250
+ $ this ->assertNotEquals (
251
+ $ defaultOptions ->getBeforeBreadcrumbCallback (),
252
+ $ beforeBreadcrumbCallback ,
253
+ 'before_breadcrumb closure has not been replaced, is the default one '
254
+ );
255
+ $ this ->assertEquals (
256
+ $ scalarCallable ,
257
+ $ beforeBreadcrumbCallback
258
+ );
259
+ }
260
+
261
+ public function scalarCallableDataProvider (): array
200
262
{
201
263
return [
202
- [[CallbackMock::class, 'beforeSend ' ]],
203
- [CallbackMock::class . '::beforeSend ' ],
264
+ [[CallbackMock::class, 'callback ' ]],
265
+ [CallbackMock::class . '::callback ' ],
204
266
[__NAMESPACE__ . '\mockBeforeSend ' ],
205
267
];
206
268
}
207
269
208
- public function testBeforeSendWithInvalidServiceReference (): void
270
+ public function testBeforeBreadcrumbWithInvalidServiceReference (): void
209
271
{
210
272
$ container = $ this ->getContainer ([
211
273
'options ' => [
212
- 'before_send ' => '@event_dispatcher ' ,
274
+ 'before_breadcrumb ' => '@event_dispatcher ' ,
213
275
],
214
276
]);
215
277
216
278
$ this ->expectException (\TypeError::class);
217
279
218
- $ this ->getOptionsFrom ($ container )->getBeforeSendCallback ();
280
+ $ this ->getOptionsFrom ($ container )->getBeforeBreadcrumbCallback ();
219
281
}
220
282
221
283
private function getContainer (array $ configuration = []): Container
@@ -241,8 +303,8 @@ private function getContainer(array $configuration = []): Container
241
303
$ containerBuilder ->setAlias (self ::CONSOLE_LISTENER_TEST_PUBLIC_ALIAS , new Alias (ConsoleListener::class, true ));
242
304
243
305
$ beforeSend = new Definition ('callable ' );
244
- $ beforeSend ->setFactory ([CallbackMock::class, 'createBeforeSendCallback ' ]);
245
- $ containerBuilder ->setDefinition ('before_send ' , $ beforeSend );
306
+ $ beforeSend ->setFactory ([CallbackMock::class, 'createCallback ' ]);
307
+ $ containerBuilder ->setDefinition ('callable_mock ' , $ beforeSend );
246
308
247
309
$ extension = new SentryExtension ();
248
310
$ extension ->load (['sentry ' => $ configuration ], $ containerBuilder );
@@ -268,15 +330,20 @@ function mockBeforeSend(Event $event): ?Event
268
330
return null ;
269
331
}
270
332
333
+ function mockBeforeBreadcrumb (Breadcrumb $ breadcrumb ): ?Breadcrumb
334
+ {
335
+ return null ;
336
+ }
337
+
271
338
class CallbackMock
272
339
{
273
- public static function beforeSend ( Event $ event ): ? Event
340
+ public static function callback ()
274
341
{
275
342
return null ;
276
343
}
277
344
278
- public static function createBeforeSendCallback (): callable
345
+ public static function createCallback (): callable
279
346
{
280
- return [new self (), 'beforeSend ' ];
347
+ return [new self (), 'callback ' ];
281
348
}
282
349
}
0 commit comments