FlareTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace Facade\FlareClient\Tests;
  3. use Error;
  4. use ErrorException;
  5. use Facade\FlareClient\Api;
  6. use Facade\FlareClient\Enums\MessageLevels;
  7. use Facade\FlareClient\Flare;
  8. use Facade\FlareClient\Tests\Concerns\MatchesReportSnapshots;
  9. use Facade\FlareClient\Tests\Mocks\FakeClient;
  10. use Facade\FlareClient\Tests\TestClasses\ExceptionWithContext;
  11. use PHPUnit\Framework\Exception;
  12. use Throwable;
  13. class FlareTest extends TestCase
  14. {
  15. use MatchesReportSnapshots;
  16. /** @var FakeClient */
  17. protected $fakeClient;
  18. /** @var Flare */
  19. protected $flare;
  20. public function setUp()
  21. {
  22. parent::setUp();
  23. Api::sendReportsInBatches(false);
  24. $this->fakeClient = new FakeClient();
  25. $this->flare = new Flare($this->fakeClient);
  26. $this->useTime('2019-01-01 12:34:56');
  27. }
  28. /* This function at the top so our snapshots won't fail
  29. * every time we add a test
  30. */
  31. protected function reportException()
  32. {
  33. $throwable = new Exception('This is a test');
  34. $this->flare->report($throwable);
  35. }
  36. protected function reportError(int $code)
  37. {
  38. $throwable = new Error('This is a test', $code);
  39. $this->flare->report($throwable);
  40. }
  41. /** @test */
  42. public function it_can_report_an_exception()
  43. {
  44. $this->reportException();
  45. $this->fakeClient->assertRequestsSent(1);
  46. $report = $this->fakeClient->getLastPayload();
  47. $this->assertMatchesReportSnapshot($report);
  48. }
  49. /** @test */
  50. public function it_can_reset_queued_exceptions()
  51. {
  52. Api::sendReportsInBatches(true);
  53. $this->reportException();
  54. $this->flare->reset();
  55. $this->fakeClient->assertRequestsSent(1);
  56. $this->flare->reset();
  57. $this->fakeClient->assertRequestsSent(1);
  58. }
  59. /** @test */
  60. public function it_can_add_user_provided_context()
  61. {
  62. $this->flare->context('my key', 'my value');
  63. $this->reportException();
  64. $this->fakeClient->assertLastRequestHas('context.context', [
  65. 'my key' => 'my value',
  66. ]);
  67. }
  68. /** @test */
  69. public function callbacks_can_modify_the_report()
  70. {
  71. $this->flare->context('my key', 'my value');
  72. $this->flare->stage('production');
  73. $this->flare->messageLevel('info');
  74. $throwable = new Exception('This is a test');
  75. $this->flare->report($throwable, function ($report) {
  76. $report->context('my key', 'new value');
  77. $report->stage('development');
  78. $report->messageLevel('warning');
  79. });
  80. $this->fakeClient->assertLastRequestHas('context.context', [
  81. 'my key' => 'new value',
  82. ]);
  83. $this->fakeClient->assertLastRequestHas('stage', 'development');
  84. $this->fakeClient->assertLastRequestHas('message_level', 'warning');
  85. }
  86. /** @test */
  87. public function it_can_anonymize_the_ip()
  88. {
  89. $_ENV['APP_RUNNING_IN_CONSOLE'] = true;
  90. $_SERVER['REMOTE_ADDR'] = '127.0.0.2';
  91. $this->reportException();
  92. $this->fakeClient->assertLastRequestContains('context.request', [
  93. 'ip' => '127.0.0.2',
  94. ]);
  95. $this->flare->anonymizeIp();
  96. $this->reportException();
  97. $this->fakeClient->assertLastRequestContains('context.request', [
  98. 'ip' => null,
  99. ]);
  100. }
  101. /** @test */
  102. public function it_can_censor_request_data()
  103. {
  104. $_ENV['FLARE_FAKE_WEB_REQUEST'] = true;
  105. $_POST['user'] = 'john@example.com';
  106. $_POST['password'] = 'secret';
  107. $this->flare->censorRequestBodyFields(['user', 'password']);
  108. $this->reportException();
  109. $this->fakeClient->assertLastRequestContains('context.request_data.body', [
  110. 'user' => '<CENSORED>',
  111. 'password' => '<CENSORED>',
  112. ]);
  113. }
  114. /** @test */
  115. public function it_can_merge_user_provided_context()
  116. {
  117. $this->flare->context('my key', 'my value');
  118. $this->flare->context('another key', 'another value');
  119. $this->reportException();
  120. $this->fakeClient->assertLastRequestHas('context.context', [
  121. 'my key' => 'my value',
  122. 'another key' => 'another value',
  123. ]);
  124. }
  125. /** @test */
  126. public function it_can_add_custom_exception_context()
  127. {
  128. $this->flare->context('my key', 'my value');
  129. $throwable = new ExceptionWithContext('This is a test');
  130. $this->flare->report($throwable);
  131. $this->fakeClient->assertLastRequestHas('context.context', [
  132. 'my key' => 'my value',
  133. 'another key' => 'another value',
  134. ]);
  135. }
  136. /** @test */
  137. public function it_can_add_a_group()
  138. {
  139. $this->flare->group('custom group', ['my key' => 'my value']);
  140. $this->reportException();
  141. $this->fakeClient->assertLastRequestHas('context.custom group', [
  142. 'my key' => 'my value',
  143. ]);
  144. }
  145. /** @test */
  146. public function it_can_return_groups()
  147. {
  148. $this->flare->context('key', 'value');
  149. $this->flare->group('custom group', ['my key' => 'my value']);
  150. $this->assertSame(['key' => 'value'], $this->flare->getGroup());
  151. $this->assertSame([], $this->flare->getGroup('foo'));
  152. $this->assertSame(['my key' => 'my value'], $this->flare->getGroup('custom group'));
  153. }
  154. /** @test */
  155. public function it_can_merge_groups()
  156. {
  157. $this->flare->group('custom group', ['my key' => 'my value']);
  158. $this->flare->group('custom group', ['another key' => 'another value']);
  159. $this->reportException();
  160. $this->fakeClient->assertLastRequestHas('context.custom group', [
  161. 'my key' => 'my value',
  162. 'another key' => 'another value',
  163. ]);
  164. }
  165. /** @test */
  166. public function it_can_set_stages()
  167. {
  168. $this->flare->stage('production');
  169. $this->reportException();
  170. $this->fakeClient->assertLastRequestHas('stage', 'production');
  171. }
  172. /** @test */
  173. public function it_can_set_message_levels()
  174. {
  175. $this->flare->messageLevel('info');
  176. $this->reportException();
  177. $this->fakeClient->assertLastRequestHas('message_level', 'info');
  178. }
  179. /** @test */
  180. public function it_can_add_glows()
  181. {
  182. $this->flare->glow(
  183. 'my glow',
  184. MessageLevels::INFO,
  185. ['my key' => 'my value']
  186. );
  187. $this->flare->glow(
  188. 'another glow',
  189. MessageLevels::ERROR,
  190. ['another key' => 'another value']
  191. );
  192. $this->reportException();
  193. $payload = $this->fakeClient->getLastPayload();
  194. $glows = collect($payload['glows'])->map(function ($glow) {
  195. unset($glow['microtime']);
  196. return $glow;
  197. })->toArray();
  198. $this->assertEquals([
  199. [
  200. 'name' => 'my glow',
  201. 'message_level' => 'info',
  202. 'meta_data' => ['my key' => 'my value'],
  203. 'time' => 1546346096,
  204. ],
  205. [
  206. 'name' => 'another glow',
  207. 'message_level' => 'error',
  208. 'meta_data' => ['another key' => 'another value'],
  209. 'time' => 1546346096,
  210. ],
  211. ], $glows);
  212. }
  213. /** @test */
  214. public function a_version_callable_can_be_set()
  215. {
  216. $this->assertNull($this->flare->version());
  217. $this->flare->determineVersionUsing(function () {
  218. return '123';
  219. });
  220. $this->assertEquals('123', $this->flare->version());
  221. }
  222. /** @test */
  223. public function it_will_add_the_version_to_the_report()
  224. {
  225. $this->reportException();
  226. $payload = $this->fakeClient->getLastPayload();
  227. $this->assertNull($payload['application_version']);
  228. $this->flare->determineVersionUsing(function () {
  229. return '123';
  230. });
  231. $this->reportException();
  232. $payload = $this->fakeClient->getLastPayload();
  233. $this->assertEquals('123', $payload['application_version']);
  234. }
  235. /** @test */
  236. public function it_can_filter_exceptions_being_reported()
  237. {
  238. $this->reportException();
  239. $this->fakeClient->assertRequestsSent(1);
  240. $this->flare->filterExceptionsUsing(function (Throwable $exception) {
  241. return false;
  242. });
  243. $this->reportException();
  244. $this->fakeClient->assertRequestsSent(1);
  245. $this->flare->filterExceptionsUsing(function (Throwable $exception) {
  246. return true;
  247. });
  248. $this->reportException();
  249. $this->fakeClient->assertRequestsSent(2);
  250. }
  251. /** @test */
  252. public function it_can_filter_errors_based_on_their_level()
  253. {
  254. $this->reportError(E_NOTICE);
  255. $this->reportError(E_WARNING);
  256. $this->fakeClient->assertRequestsSent(2);
  257. $this->flare->reportErrorLevels(E_ALL & ~E_NOTICE);
  258. $this->reportError(E_NOTICE);
  259. $this->reportError(E_WARNING);
  260. $this->fakeClient->assertRequestsSent(3);
  261. }
  262. /** @test */
  263. public function it_can_filter_error_exceptions_based_on_their_severity()
  264. {
  265. $this->flare->report(new ErrorException('test', 0, E_NOTICE));
  266. $this->flare->report(new ErrorException('test', 0, E_WARNING));
  267. $this->fakeClient->assertRequestsSent(2);
  268. $this->flare->reportErrorLevels(E_ALL & ~E_NOTICE);
  269. $this->flare->report(new ErrorException('test', 0, E_NOTICE));
  270. $this->flare->report(new ErrorException('test', 0, E_WARNING));
  271. $this->fakeClient->assertRequestsSent(3);
  272. }
  273. }