-
-
Notifications
You must be signed in to change notification settings - Fork 449
/
ClientTest.php
2242 lines (1992 loc) · 74 KB
/
ClientTest.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
function simple_function($a=null, $b=null, $c=null)
{
assert(0);
}
function invalid_encoding()
{
$fp = fopen(__DIR__ . '/../../data/binary', 'r');
simple_function(fread($fp, 64));
fclose($fp);
}
// XXX: Is there a better way to stub the client?
class Dummy_Raven_Client extends Raven_Client
{
private $__sent_events = array();
public $dummy_breadcrumbs_handlers_has_set = false;
public $dummy_shutdown_handlers_has_set = false;
public function getSentEvents()
{
return $this->__sent_events;
}
public function send(&$data)
{
if (is_callable($this->send_callback) && call_user_func_array($this->send_callback, array(&$data)) === false) {
// if send_callback returns falsely, end native send
return;
}
$this->__sent_events[] = $data;
}
public static function is_http_request()
{
return true;
}
public static function get_auth_header($timestamp, $client, $api_key, $secret_key)
{
return parent::get_auth_header($timestamp, $client, $api_key, $secret_key);
}
public function get_http_data()
{
return parent::get_http_data();
}
public function get_user_data()
{
return parent::get_user_data();
}
public function buildCurlCommand($url, $data, $headers)
{
return parent::buildCurlCommand($url, $data, $headers);
}
// short circuit breadcrumbs
public function registerDefaultBreadcrumbHandlers()
{
$this->dummy_breadcrumbs_handlers_has_set = true;
}
public function registerShutdownFunction()
{
$this->dummy_shutdown_handlers_has_set = true;
}
/**
* Expose the current url method to test it
*
* @return string
*/
public function test_get_current_url()
{
return $this->get_current_url();
}
}
class Dummy_Raven_Client_With_Overrided_Direct_Send extends Raven_Client
{
public $_send_http_asynchronous_curl_exec_called = false;
public $_send_http_synchronous = false;
public $_set_url;
public $_set_data;
public $_set_headers;
public static $_close_curl_resource_called = false;
public function send_http_asynchronous_curl_exec($url, $data, $headers)
{
$this->_send_http_asynchronous_curl_exec_called = true;
$this->_set_url = $url;
$this->_set_data = $data;
$this->_set_headers = $headers;
}
public function send_http_synchronous($url, $data, $headers)
{
$this->_send_http_synchronous = true;
$this->_set_url = $url;
$this->_set_data = $data;
$this->_set_headers = $headers;
}
public function get_curl_options()
{
$options = parent::get_curl_options();
return $options;
}
public function get_curl_handler()
{
return $this->_curl_handler;
}
public function set_curl_handler(Raven_CurlHandler $value)
{
$this->_curl_handler = $value;
}
public function close_curl_resource()
{
parent::close_curl_resource();
self::$_close_curl_resource_called = true;
}
}
class Dummy_Raven_Client_No_Http extends Dummy_Raven_Client
{
/**
* @return bool
*/
public static function is_http_request()
{
return false;
}
}
class Dummy_Raven_Client_With_Sync_Override extends Raven_Client
{
private static $_test_data = null;
public static function get_test_data()
{
if (is_null(self::$_test_data)) {
self::$_test_data = '';
for ($i = 0; $i < 128; $i++) {
self::$_test_data .= chr(mt_rand(ord('a'), ord('z')));
}
}
return self::$_test_data;
}
public static function test_filename()
{
return sys_get_temp_dir().'/clientraven.tmp';
}
protected function buildCurlCommand($url, $data, $headers)
{
return 'echo '.escapeshellarg(self::get_test_data()).' > '.self::test_filename();
}
}
class Dummy_Raven_CurlHandler extends Raven_CurlHandler
{
public $_set_url;
public $_set_data;
public $_set_headers;
public $_enqueue_called = false;
public $_join_called = false;
public function __construct($options = array(), $join_timeout = 5)
{
parent::__construct($options, $join_timeout);
}
public function enqueue($url, $data = null, $headers = array())
{
$this->_enqueue_called = true;
$this->_set_url = $url;
$this->_set_data = $data;
$this->_set_headers = $headers;
return 0;
}
public function join($timeout = null)
{
$this->_join_called = true;
}
}
class Raven_Tests_ClientTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
parent::tearDown();
if (file_exists(Dummy_Raven_Client_With_Sync_Override::test_filename())) {
unlink(Dummy_Raven_Client_With_Sync_Override::test_filename());
}
}
private function create_exception()
{
try {
throw new Exception('Foo bar');
} catch (Exception $ex) {
return $ex;
}
}
private function create_chained_exception()
{
try {
throw new Exception('Foo bar');
} catch (Exception $ex) {
try {
throw new Exception('Child exc', 0, $ex);
} catch (Exception $ex2) {
return $ex2;
}
}
}
public function testParseDSNHttp()
{
$result = Raven_Client::ParseDSN('http:https://public:[email protected]/1');
$this->assertEquals(1, $result['project']);
$this->assertEquals('http:https://example.com/api/1/store/', $result['server']);
$this->assertEquals('public', $result['public_key']);
$this->assertEquals('secret', $result['secret_key']);
}
public function testParseDSNHttps()
{
$result = Raven_Client::ParseDSN('https://public:[email protected]/1');
$this->assertEquals(1, $result['project']);
$this->assertEquals('https://example.com/api/1/store/', $result['server']);
$this->assertEquals('public', $result['public_key']);
$this->assertEquals('secret', $result['secret_key']);
}
public function testParseDSNPath()
{
$result = Raven_Client::ParseDSN('http:https://public:[email protected]/app/1');
$this->assertEquals(1, $result['project']);
$this->assertEquals('http:https://example.com/app/api/1/store/', $result['server']);
$this->assertEquals('public', $result['public_key']);
$this->assertEquals('secret', $result['secret_key']);
}
public function testParseDSNPort()
{
$result = Raven_Client::ParseDSN('http:https://public:[email protected]:9000/app/1');
$this->assertEquals(1, $result['project']);
$this->assertEquals('http:https://example.com:9000/app/api/1/store/', $result['server']);
$this->assertEquals('public', $result['public_key']);
$this->assertEquals('secret', $result['secret_key']);
}
public function testParseDSNInvalidScheme()
{
try {
Raven_Client::ParseDSN('gopher:https://public:secret@/1');
$this->fail();
} catch (Exception $e) {
return;
}
}
public function testParseDSNMissingNetloc()
{
try {
Raven_Client::ParseDSN('http:https://public:secret@/1');
$this->fail();
} catch (Exception $e) {
return;
}
}
public function testParseDSNMissingProject()
{
try {
Raven_Client::ParseDSN('http:https://public:[email protected]');
$this->fail();
} catch (Exception $e) {
return;
}
}
/**
* @expectedException InvalidArgumentException
*/
public function testParseDSNMissingPublicKey()
{
Raven_Client::ParseDSN('http:https://:[email protected]/1');
}
/**
* @expectedException InvalidArgumentException
*/
public function testParseDSNMissingSecretKey()
{
Raven_Client::ParseDSN('http:https://[email protected]/1');
}
/**
* @covers Raven_Client::__construct
*/
public function testDsnFirstArgument()
{
$client = new Dummy_Raven_Client('http:https://public:[email protected]/1');
$this->assertEquals(1, $client->project);
$this->assertEquals('http:https://example.com/api/1/store/', $client->server);
$this->assertEquals('public', $client->public_key);
$this->assertEquals('secret', $client->secret_key);
}
/**
* @covers Raven_Client::__construct
*/
public function testDsnFirstArgumentWithOptions()
{
$client = new Dummy_Raven_Client('http:https://public:[email protected]/1', array(
'site' => 'foo',
));
$this->assertEquals(1, $client->project);
$this->assertEquals('http:https://example.com/api/1/store/', $client->server);
$this->assertEquals('public', $client->public_key);
$this->assertEquals('secret', $client->secret_key);
$this->assertEquals('foo', $client->site);
}
/**
* @covers Raven_Client::__construct
*/
public function testOptionsFirstArgument()
{
$client = new Dummy_Raven_Client(array(
'server' => 'http:https://example.com/api/1/store/',
'project' => 1,
));
$this->assertEquals('http:https://example.com/api/1/store/', $client->server);
}
/**
* @covers Raven_Client::__construct
*/
public function testDsnInOptionsFirstArg()
{
$client = new Dummy_Raven_Client(array(
'dsn' => 'http:https://public:[email protected]/1',
));
$this->assertEquals(1, $client->project);
$this->assertEquals('http:https://example.com/api/1/store/', $client->server);
$this->assertEquals('public', $client->public_key);
$this->assertEquals('secret', $client->secret_key);
}
/**
* @covers Raven_Client::__construct
*/
public function testDsnInOptionsSecondArg()
{
$client = new Dummy_Raven_Client(null, array(
'dsn' => 'http:https://public:[email protected]/1',
));
$this->assertEquals(1, $client->project);
$this->assertEquals('http:https://example.com/api/1/store/', $client->server);
$this->assertEquals('public', $client->public_key);
$this->assertEquals('secret', $client->secret_key);
}
/**
* @covers Raven_Client::__construct
*/
public function testOptionsFirstArgumentWithOptions()
{
$client = new Dummy_Raven_Client(array(
'server' => 'http:https://example.com/api/1/store/',
'project' => 1,
), array(
'site' => 'foo',
));
$this->assertEquals('http:https://example.com/api/1/store/', $client->server);
$this->assertEquals('foo', $client->site);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testOptionsExtraData()
{
$client = new Dummy_Raven_Client(array('extra' => array('foo' => 'bar')));
$client->captureMessage('Test Message %s', array('foo'));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals('bar', $event['extra']['foo']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testOptionsExtraDataWithNull()
{
$client = new Dummy_Raven_Client(array('extra' => array('foo' => 'bar')));
$client->captureMessage('Test Message %s', array('foo'), null);
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals('bar', $event['extra']['foo']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testEmptyExtraData()
{
$client = new Dummy_Raven_Client(array('extra' => array()));
$client->captureMessage('Test Message %s', array('foo'));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(array_key_exists('extra', $event), false);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageDoesHandleUninterpolatedMessage()
{
$client = new Dummy_Raven_Client();
$client->captureMessage('Test Message %s');
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals('Test Message %s', $event['message']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageDoesHandleInterpolatedMessage()
{
$client = new Dummy_Raven_Client();
$client->captureMessage('Test Message %s', array('foo'));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals('Test Message foo', $event['message']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageDoesHandleInterpolatedMessageWithRelease()
{
$client = new Dummy_Raven_Client();
$client->setRelease(20160909144742);
$this->assertEquals(20160909144742, $client->getRelease());
$client->captureMessage('Test Message %s', array('foo'));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(20160909144742, $event['release']);
$this->assertEquals('Test Message foo', $event['message']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageSetsInterface()
{
$client = new Dummy_Raven_Client();
$client->captureMessage('Test Message %s', array('foo'));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(array(
'message' => 'Test Message %s',
'params' => array('foo'),
'formatted' => 'Test Message foo',
), $event['sentry.interfaces.Message']);
$this->assertEquals('Test Message foo', $event['message']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageHandlesOptionsAsThirdArg()
{
$client = new Dummy_Raven_Client();
$client->captureMessage('Test Message %s', array('foo'), array(
'level' => Dummy_Raven_Client::WARNING,
'extra' => array('foo' => 'bar')
));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(Dummy_Raven_Client::WARNING, $event['level']);
$this->assertEquals('bar', $event['extra']['foo']);
$this->assertEquals('Test Message foo', $event['message']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageHandlesLevelAsThirdArg()
{
$client = new Dummy_Raven_Client();
$client->captureMessage('Test Message %s', array('foo'), Dummy_Raven_Client::WARNING);
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(Dummy_Raven_Client::WARNING, $event['level']);
$this->assertEquals('Test Message foo', $event['message']);
}
/**
* @covers Raven_Client::captureException
*/
public function testCaptureExceptionSetsInterfaces()
{
# TODO: it'd be nice if we could mock the stacktrace extraction function here
$client = new Dummy_Raven_Client();
$ex = $this->create_exception();
$client->captureException($ex);
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$exc = $event['exception'];
$this->assertEquals(1, count($exc['values']));
$this->assertEquals('Foo bar', $exc['values'][0]['value']);
$this->assertEquals('Exception', $exc['values'][0]['type']);
$this->assertFalse(empty($exc['values'][0]['stacktrace']['frames']));
$frames = $exc['values'][0]['stacktrace']['frames'];
$frame = $frames[count($frames) - 1];
$this->assertTrue($frame['lineno'] > 0);
$this->assertEquals('create_exception', $frame['function']);
$this->assertFalse(isset($frame['vars']));
$this->assertEquals(' throw new Exception(\'Foo bar\');', $frame['context_line']);
$this->assertFalse(empty($frame['pre_context']));
$this->assertFalse(empty($frame['post_context']));
}
/**
* @covers Raven_Client::captureException
*/
public function testCaptureExceptionChainedException()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('PHP 5.3 required for chained exceptions.');
}
# TODO: it'd be nice if we could mock the stacktrace extraction function here
$client = new Dummy_Raven_Client();
$ex = $this->create_chained_exception();
$client->captureException($ex);
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$exc = $event['exception'];
$this->assertEquals(2, count($exc['values']));
$this->assertEquals('Foo bar', $exc['values'][0]['value']);
$this->assertEquals('Child exc', $exc['values'][1]['value']);
}
/**
* @covers Raven_Client::captureException
*/
public function testCaptureExceptionDifferentLevelsInChainedExceptionsBug()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('PHP 5.3 required for chained exceptions.');
}
$client = new Dummy_Raven_Client();
$e1 = new ErrorException('First', 0, E_DEPRECATED);
$e2 = new ErrorException('Second', 0, E_NOTICE, __FILE__, __LINE__, $e1);
$e3 = new ErrorException('Third', 0, E_ERROR, __FILE__, __LINE__, $e2);
$client->captureException($e1);
$client->captureException($e2);
$client->captureException($e3);
$events = $client->getSentEvents();
$event = array_pop($events);
$this->assertEquals(Dummy_Raven_Client::ERROR, $event['level']);
$event = array_pop($events);
$this->assertEquals(Dummy_Raven_Client::INFO, $event['level']);
$event = array_pop($events);
$this->assertEquals(Dummy_Raven_Client::WARNING, $event['level']);
}
/**
* @covers Raven_Client::captureException
*/
public function testCaptureExceptionHandlesOptionsAsSecondArg()
{
$client = new Dummy_Raven_Client();
$ex = $this->create_exception();
$client->captureException($ex, array('culprit' => 'test'));
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals('test', $event['culprit']);
}
/**
* @covers Raven_Client::captureException
*/
public function testCaptureExceptionHandlesExcludeOption()
{
$client = new Dummy_Raven_Client(array(
'exclude' => array('Exception'),
));
$ex = $this->create_exception();
$client->captureException($ex, 'test');
$events = $client->getSentEvents();
$this->assertEquals(0, count($events));
}
public function testCaptureExceptionInvalidUTF8()
{
if (version_compare(PHP_VERSION, '7.0', '>=')) {
if (ini_get('zend.assertions') != 1) {
$this->markTestSkipped('Production environment does not execute asserts');
return;
}
}
$client = new Dummy_Raven_Client();
try {
invalid_encoding();
} catch (Exception $ex) {
$client->captureException($ex);
}
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
// if this fails to encode it returns false
$message = $client->encode($events[0]);
$this->assertNotFalse($message, $client->getLastError());
}
/**
* @covers Raven_Client::__construct
*/
public function testDoesRegisterProcessors()
{
$client = new Dummy_Raven_Client(array(
'processors' => array('Raven_Processor_SanitizeDataProcessor'),
));
$this->assertEquals(1, count($client->processors));
$this->assertInstanceOf('Raven_Processor_SanitizeDataProcessor', $client->processors[0]);
}
public function testProcessDoesCallProcessors()
{
$data = array("key"=>"value");
$processor = $this->getMockBuilder('Processor')
->setMethods(array('process'))
->getMock();
$processor->expects($this->once())
->method('process')
->with($data);
$client = new Dummy_Raven_Client();
$client->processors[] = $processor;
$client->process($data);
}
/**
* @covers Raven_Client::__construct
* @covers Raven_Client::getDefaultProcessors
*/
public function testDefaultProcessorsAreUsed()
{
$client = new Dummy_Raven_Client();
$defaults = Dummy_Raven_Client::getDefaultProcessors();
$this->assertEquals(count($defaults), count($client->processors));
}
/**
* @covers Raven_Client::getDefaultProcessors
*/
public function testDefaultProcessorsContainSanitizeDataProcessor()
{
$this->assertContains('Raven_Processor_SanitizeDataProcessor', Dummy_Raven_Client::getDefaultProcessors());
}
/**
* @covers Raven_Client::__construct
* @covers Raven_Client::get_default_data
*/
public function testGetDefaultData()
{
$client = new Dummy_Raven_Client();
$client->transaction->push('test');
$expected = array(
'platform' => 'php',
'project' => $client->project,
'server_name' => $client->name,
'site' => $client->site,
'logger' => $client->logger,
'tags' => $client->tags,
'sdk' => array(
'name' => 'sentry-php',
'version' => $client::VERSION,
),
'culprit' => 'test',
);
$this->assertEquals($expected, $client->get_default_data());
}
/**
* @backupGlobals
* @covers Raven_Client::get_http_data
*/
public function testGetHttpData()
{
$_SERVER = array(
'REDIRECT_STATUS' => '200',
'CONTENT_TYPE' => 'text/xml',
'CONTENT_LENGTH' => '99',
'HTTP_HOST' => 'getsentry.com',
'HTTP_ACCEPT' => 'text/html',
'HTTP_ACCEPT_CHARSET' => 'utf-8',
'HTTP_COOKIE' => 'cupcake: strawberry',
'SERVER_PORT' => '443',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'PATCH',
'QUERY_STRING' => 'q=bitch&l=en',
'REQUEST_URI' => '/welcome/',
'SCRIPT_NAME' => '/index.php',
);
$_POST = array(
'stamp' => '1c',
);
$_COOKIE = array(
'donut' => 'chocolat',
);
$expected = array(
'request' => array(
'method' => 'PATCH',
'url' => 'https://getsentry.com/welcome/',
'query_string' => 'q=bitch&l=en',
'data' => array(
'stamp' => '1c',
),
'cookies' => array(
'donut' => 'chocolat',
),
'headers' => array(
'Host' => 'getsentry.com',
'Accept' => 'text/html',
'Accept-Charset' => 'utf-8',
'Cookie' => 'cupcake: strawberry',
'Content-Type' => 'text/xml',
'Content-Length' => '99',
),
)
);
$client = new Dummy_Raven_Client();
$this->assertEquals($expected, $client->get_http_data());
}
/**
* @covers Raven_Client::user_context
* @covers Raven_Client::get_user_data
*/
public function testGetUserDataWithSetUser()
{
$client = new Dummy_Raven_Client();
$id = 'unique_id';
$email = '[email protected]';
$client->user_context(array('id' => $id, 'email' => $email, 'username' => 'my_user', ));
$expected = array(
'user' => array(
'id' => $id,
'username' => 'my_user',
'email' => $email,
)
);
$this->assertEquals($expected, $client->get_user_data());
}
/**
* @covers Raven_Client::get_user_data
*/
public function testGetUserDataWithNoUser()
{
$client = new Dummy_Raven_Client();
$expected = array(
'user' => array(
'id' => session_id(),
)
);
$this->assertEquals($expected, $client->get_user_data());
}
/**
* @covers Raven_Client::get_auth_header
*/
public function testGet_Auth_Header()
{
$client = new Dummy_Raven_Client();
$clientstring = 'sentry-php/test';
$timestamp = '1234341324.340000';
$expected = "Sentry sentry_timestamp={$timestamp}, sentry_client={$clientstring}, " .
"sentry_version=" . Dummy_Raven_Client::PROTOCOL . ", " .
"sentry_key=publickey, sentry_secret=secretkey";
$this->assertEquals($expected, $client->get_auth_header($timestamp, 'sentry-php/test', 'publickey', 'secretkey'));
}
/**
* @covers Raven_Client::getAuthHeader
*/
public function testGetAuthHeader()
{
$client = new Dummy_Raven_Client();
$ts1 = microtime(true);
$header = $client->getAuthHeader();
$ts2 = microtime(true);
$this->assertEquals(1, preg_match('/sentry_timestamp=([0-9.]+)/', $header, $a));
$this->assertRegExp('/^[0-9]+(\\.[0-9]+)?$/', $a[1]);
$this->assertGreaterThanOrEqual($ts1, (double)$a[1]);
$this->assertLessThanOrEqual($ts2, (double)$a[1]);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageWithUserContext()
{
$client = new Dummy_Raven_Client();
$client->user_context(array('email' => '[email protected]'));
$client->captureMessage('test');
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(array(
'email' => '[email protected]',
), $event['user']);
}
/**
* @covers Raven_Client::captureMessage
*/
public function testCaptureMessageWithUnserializableUserData()
{
$client = new Dummy_Raven_Client();
$client->user_context(array(
'email' => '[email protected]',
'data' => array(
'error' => new Exception('test'),
)
));
$client->captureMessage('test');
$events = $client->getSentEvents();
// we're just asserting that this goes off without a hitch
$this->assertEquals(1, count($events));
array_pop($events);
}
/**
* @covers Raven_Client::captureMessage
* @covers Raven_Client::tags_context
*/
public function testCaptureMessageWithTagsContext()
{
$client = new Dummy_Raven_Client();
$client->tags_context(array('foo' => 'bar'));
$client->tags_context(array('biz' => 'boz'));
$client->tags_context(array('biz' => 'baz'));
$client->captureMessage('test');
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(array(
'foo' => 'bar',
'biz' => 'baz',
), $event['tags']);
}
/**
* @covers Raven_Client::captureMessage
* @covers Raven_Client::extra_context
*/
public function testCaptureMessageWithExtraContext()
{
$client = new Dummy_Raven_Client();
$client->extra_context(array('foo' => 'bar'));
$client->extra_context(array('biz' => 'boz'));
$client->extra_context(array('biz' => 'baz'));
$client->captureMessage('test');
$events = $client->getSentEvents();
$this->assertEquals(1, count($events));
$event = array_pop($events);
$this->assertEquals(array(
'foo' => 'bar',
'biz' => 'baz',
), $event['extra']);
}
/**
* @covers Raven_Client::captureException
*/
public function testCaptureExceptionContainingLatin1()
{
// If somebody has a non-utf8 codebase, she/he should add the encoding to the detection order
$options = array(
'mb_detect_order' => array(
'ISO-8859-1', 'ASCII', 'UTF-8'
)
);
$client = new Dummy_Raven_Client($options);
// we need a non-utf8 string here.
// nobody writes non-utf8 in exceptions, but it is the easiest way to test.
// in real live non-utf8 may be somewhere in the exception's stacktrace
$utf8String = 'äöü';
$latin1String = utf8_decode($utf8String);
$client->captureException(new \Exception($latin1String));
$events = $client->getSentEvents();
$event = array_pop($events);
$this->assertEquals($utf8String, $event['exception']['values'][0]['value']);
}
public function testCaptureExceptionInLatin1File()
{
// If somebody has a non-utf8 codebase, she/he should add the encoding to the detection order
$options = array(
'mb_detect_order' => array(
'ISO-8859-1', 'ASCII', 'UTF-8'