9
9
use Sentry \SentryBundle \EventListener \SentryExceptionListenerInterface ;
10
10
use Sentry \SentryBundle \SentrySymfonyEvents ;
11
11
use Symfony \Component \Console \Command \Command ;
12
+ use Symfony \Component \Console \Event \ConsoleCommandEvent ;
12
13
use Symfony \Component \Console \Event \ConsoleErrorEvent ;
13
14
use Symfony \Component \Console \Event \ConsoleExceptionEvent ;
15
+ use Symfony \Component \Console \Event \ConsoleTerminateEvent ;
14
16
use Symfony \Component \Console \Input \InputInterface ;
15
17
use Symfony \Component \Console \Output \OutputInterface ;
16
18
use Symfony \Component \DependencyInjection \Alias ;
17
19
use Symfony \Component \DependencyInjection \ContainerBuilder ;
18
20
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
19
21
use Symfony \Component \HttpFoundation \Request ;
20
22
use Symfony \Component \HttpFoundation \RequestStack ;
23
+ use Symfony \Component \HttpKernel \Event \FinishRequestEvent ;
21
24
use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
22
25
use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
23
26
use Symfony \Component \HttpKernel \Exception \HttpException ;
@@ -81,6 +84,10 @@ public function test_that_user_data_is_not_set_on_subrequest()
81
84
{
82
85
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
83
86
87
+ $ mockEvent
88
+ ->method ('getRequest ' )
89
+ ->willReturn (new Request ());
90
+
84
91
$ mockEvent
85
92
->expects ($ this ->once ())
86
93
->method ('getRequestType ' )
@@ -107,6 +114,10 @@ public function test_that_user_data_is_not_set_if_token_storage_not_present()
107
114
108
115
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
109
116
117
+ $ mockEvent
118
+ ->method ('getRequest ' )
119
+ ->willReturn (new Request ());
120
+
110
121
$ mockEvent
111
122
->expects ($ this ->once ())
112
123
->method ('getRequestType ' )
@@ -136,6 +147,10 @@ public function test_that_user_data_is_not_set_if_authorization_checker_not_pres
136
147
137
148
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
138
149
150
+ $ mockEvent
151
+ ->method ('getRequest ' )
152
+ ->willReturn (new Request ());
153
+
139
154
$ mockEvent
140
155
->expects ($ this ->once ())
141
156
->method ('getRequestType ' )
@@ -167,6 +182,10 @@ public function test_that_user_data_is_not_set_if_token_not_present()
167
182
168
183
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
169
184
185
+ $ mockEvent
186
+ ->method ('getRequest ' )
187
+ ->willReturn (new Request ());
188
+
170
189
$ mockEvent
171
190
->expects ($ this ->once ())
172
191
->method ('getRequestType ' )
@@ -209,6 +228,10 @@ public function test_that_user_data_is_not_set_if_not_authorized()
209
228
210
229
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
211
230
231
+ $ mockEvent
232
+ ->method ('getRequest ' )
233
+ ->willReturn (new Request ());
234
+
212
235
$ mockEvent ->expects ($ this ->once ())
213
236
->method ('getRequestType ' )
214
237
->willReturn (HttpKernelInterface::MASTER_REQUEST );
@@ -254,6 +277,10 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a
254
277
255
278
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
256
279
280
+ $ mockEvent
281
+ ->method ('getRequest ' )
282
+ ->willReturn (new Request ());
283
+
257
284
$ mockEvent
258
285
->expects ($ this ->once ())
259
286
->method ('getRequestType ' )
@@ -303,6 +330,10 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a
303
330
304
331
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
305
332
333
+ $ mockEvent
334
+ ->method ('getRequest ' )
335
+ ->willReturn (new Request ());
336
+
306
337
$ mockEvent
307
338
->expects ($ this ->once ())
308
339
->method ('getRequestType ' )
@@ -358,6 +389,10 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a
358
389
359
390
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
360
391
392
+ $ mockEvent
393
+ ->method ('getRequest ' )
394
+ ->willReturn (new Request ());
395
+
361
396
$ mockEvent
362
397
->expects ($ this ->once ())
363
398
->method ('getRequestType ' )
@@ -404,6 +439,10 @@ public function test_that_ip_is_set_from_request_stack()
404
439
405
440
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
406
441
442
+ $ mockEvent
443
+ ->method ('getRequest ' )
444
+ ->willReturn (new Request ());
445
+
407
446
$ mockRequest = $ this ->createMock (Request::class);
408
447
409
448
$ mockRequest
@@ -438,6 +477,59 @@ public function test_that_ip_is_set_from_request_stack()
438
477
$ listener ->onKernelRequest ($ mockEvent );
439
478
}
440
479
480
+ public function test_that_it_sets_transaction_from_route ()
481
+ {
482
+ $ mockEvent = $ this ->createMock (GetResponseEvent::class);
483
+
484
+ $ request = new Request ();
485
+ $ request ->attributes ->set ('_route ' , 'my_route ' );
486
+ $ mockEvent
487
+ ->expects ($ this ->once ())
488
+ ->method ('getRequest ' )
489
+ ->willReturn ($ request );
490
+
491
+ $ mockEvent
492
+ ->method ('getRequestType ' )
493
+ ->willReturn (HttpKernelInterface::SUB_REQUEST );
494
+
495
+ $ mockTransactionStack = $ this ->createMock (\Raven_TransactionStack::class);
496
+ $ this ->mockSentryClient ->transaction = $ mockTransactionStack ;
497
+
498
+ $ mockTransactionStack
499
+ ->expects ($ this ->once ())
500
+ ->method ('push ' )
501
+ ->with ('my_route ' );
502
+
503
+ $ this ->containerBuilder ->compile ();
504
+ $ listener = $ this ->getListener ();
505
+ $ listener ->onKernelRequest ($ mockEvent );
506
+ }
507
+
508
+ public function test_that_it_pops_transaction_from_route ()
509
+ {
510
+ $ mockEvent = $ this ->createMock (FinishRequestEvent::class);
511
+
512
+ $ request = new Request ();
513
+ $ request ->attributes ->set ('_route ' , 'my_route ' );
514
+ $ mockEvent
515
+ ->expects ($ this ->once ())
516
+ ->method ('getRequest ' )
517
+ ->willReturn ($ request );
518
+
519
+ $ mockTransactionStack = $ this ->createMock (\Raven_TransactionStack::class);
520
+ $ this ->mockSentryClient ->transaction = $ mockTransactionStack ;
521
+
522
+ $ mockTransactionStack
523
+ ->expects ($ this ->once ())
524
+ ->method ('pop ' )
525
+ ->with ('my_route ' );
526
+
527
+ $ this ->containerBuilder ->compile ();
528
+ /** @var ExceptionListener $listener */
529
+ $ listener = $ this ->getListener ();
530
+ $ listener ->onFinishRequest ($ mockEvent );
531
+ }
532
+
441
533
public function test_regression_with_unauthenticated_user_token_PR_78 ()
442
534
{
443
535
$ mockToken = $ this ->createMock (TokenInterface::class);
@@ -447,6 +539,10 @@ public function test_regression_with_unauthenticated_user_token_PR_78()
447
539
448
540
$ mockEvent = $ this ->createMock (GetResponseEvent::class);
449
541
542
+ $ mockEvent
543
+ ->method ('getRequest ' )
544
+ ->willReturn (new Request ());
545
+
450
546
$ mockEvent
451
547
->expects ($ this ->once ())
452
548
->method ('getRequestType ' )
@@ -465,6 +561,10 @@ public function test_that_it_does_not_report_http_exception_if_included_in_captu
465
561
{
466
562
$ mockEvent = $ this ->createMock (GetResponseForExceptionEvent::class);
467
563
564
+ $ mockEvent
565
+ ->method ('getRequest ' )
566
+ ->willReturn (new Request ());
567
+
468
568
$ mockEvent
469
569
->expects ($ this ->once ())
470
570
->method ('getException ' )
@@ -510,6 +610,30 @@ public function test_that_it_captures_exception()
510
610
$ listener ->onKernelException ($ mockEvent );
511
611
}
512
612
613
+ public function test_that_it_sets_transaction_from_command ()
614
+ {
615
+ $ mockEvent = $ this ->createMock (ConsoleCommandEvent::class);
616
+
617
+ $ command = new Command ('my_command ' );
618
+ $ mockEvent
619
+ ->expects ($ this ->once ())
620
+ ->method ('getCommand ' )
621
+ ->willReturn ($ command );
622
+
623
+ $ mockTransactionStack = $ this ->createMock (\Raven_TransactionStack::class);
624
+ $ this ->mockSentryClient ->transaction = $ mockTransactionStack ;
625
+
626
+ $ mockTransactionStack
627
+ ->expects ($ this ->once ())
628
+ ->method ('push ' )
629
+ ->with ('my_command ' );
630
+
631
+ $ this ->containerBuilder ->compile ();
632
+ /** @var ExceptionListener $listener */
633
+ $ listener = $ this ->getListener ();
634
+ $ listener ->onConsoleCommand ($ mockEvent );
635
+ }
636
+
513
637
/**
514
638
* @dataProvider mockCommandProvider
515
639
*/
@@ -593,6 +717,30 @@ public function test_that_it_captures_console_error(?Command $mockCommand, strin
593
717
$ listener ->onConsoleError ($ event );
594
718
}
595
719
720
+ public function test_that_it_pops_transaction_from_command ()
721
+ {
722
+ $ mockEvent = $ this ->createMock (ConsoleTerminateEvent::class);
723
+
724
+ $ command = new Command ('my_command ' );
725
+ $ mockEvent
726
+ ->expects ($ this ->once ())
727
+ ->method ('getCommand ' )
728
+ ->willReturn ($ command );
729
+
730
+ $ mockTransactionStack = $ this ->createMock (\Raven_TransactionStack::class);
731
+ $ this ->mockSentryClient ->transaction = $ mockTransactionStack ;
732
+
733
+ $ mockTransactionStack
734
+ ->expects ($ this ->once ())
735
+ ->method ('pop ' )
736
+ ->with ('my_command ' );
737
+
738
+ $ this ->containerBuilder ->compile ();
739
+ /** @var ExceptionListener $listener */
740
+ $ listener = $ this ->getListener ();
741
+ $ listener ->onFinishCommand ($ mockEvent );
742
+ }
743
+
596
744
public function mockCommandProvider ()
597
745
{
598
746
$ mockCommand = $ this ->createMock (Command::class);
0 commit comments