TrimStringsStrategyTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Facade\FlareClient\Tests\Truncation;
  3. use Facade\FlareClient\Truncation\ReportTrimmer;
  4. use Facade\FlareClient\Truncation\TrimStringsStrategy;
  5. use PHPUnit\Framework\TestCase;
  6. class TrimStringsStrategyTest extends TestCase
  7. {
  8. /** @test */
  9. public function it_can_trim_long_strings_in_payload()
  10. {
  11. foreach (TrimStringsStrategy::thresholds() as $threshold) {
  12. [$payload, $expected] = $this->createLargePayload($threshold);
  13. $strategy = new TrimStringsStrategy(new ReportTrimmer());
  14. $this->assertSame($expected, $strategy->execute($payload));
  15. }
  16. }
  17. /** @test */
  18. public function it_does_not_trim_short_payloads()
  19. {
  20. $payload = [
  21. 'data' => [
  22. 'body' => 'short',
  23. 'nested' => [
  24. 'message' => 'short',
  25. ],
  26. ],
  27. ];
  28. $strategy = new TrimStringsStrategy(new ReportTrimmer());
  29. $trimmedPayload = $strategy->execute($payload);
  30. $this->assertSame($payload, $trimmedPayload);
  31. }
  32. protected function createLargePayload($threshold)
  33. {
  34. $payload = $expected = [
  35. 'data' => [
  36. 'messages' => [],
  37. ],
  38. ];
  39. while (strlen(json_encode($payload)) < ReportTrimmer::getMaxPayloadSize()) {
  40. $payload['data']['messages'][] = str_repeat('A', $threshold + 10);
  41. $expected['data']['messages'][] = str_repeat('A', $threshold);
  42. }
  43. return [$payload, $expected];
  44. }
  45. }