|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Buffer; |
| 4 | + |
| 5 | +use G4\Log\Buffer\FieldsTruncator; |
| 6 | +use G4\ValueObject\StringLiteral; |
| 7 | + |
| 8 | +class FieldsTruncatorTest extends \PHPUnit\Framework\TestCase |
| 9 | +{ |
| 10 | + /** @var array[] */ |
| 11 | + private $config; |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + $this->config = [ |
| 15 | + 'enabled' => 1, |
| 16 | + 'truncate_above' => 100, |
| 17 | + 'truncate_to' => 50, |
| 18 | + 'nd_requests' => [ |
| 19 | + 'resource', |
| 20 | + 'params', |
| 21 | + ], |
| 22 | + ]; |
| 23 | + } |
| 24 | + |
| 25 | + public function testEnabled(): void |
| 26 | + { |
| 27 | + $config = $this->config; |
| 28 | + $fieldsTruncator = new FieldsTruncator(new StringLiteral('nd_requests'), $config); |
| 29 | + self::assertTrue($fieldsTruncator->enabled()); |
| 30 | + $config = $this->config; |
| 31 | + $config['enabled'] = 0; |
| 32 | + $fieldsTruncator = new FieldsTruncator(new StringLiteral('nd_requests'), $config); |
| 33 | + self::assertFalse($fieldsTruncator->enabled()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testShouldTruncateField(): void |
| 37 | + { |
| 38 | + $fieldsTruncator = new FieldsTruncator(new StringLiteral('nd_requests'), $this->config); |
| 39 | + self::assertTrue($fieldsTruncator->shouldTruncateField('resource')); |
| 40 | + self::assertTrue($fieldsTruncator->shouldTruncateField('params')); |
| 41 | + self::assertFalse($fieldsTruncator->shouldTruncateField('app_message')); |
| 42 | + } |
| 43 | + |
| 44 | + public function testTruncate(): void |
| 45 | + { |
| 46 | + $fieldsTruncator = new FieldsTruncator(new StringLiteral('nd_requests'), $this->config); |
| 47 | + $logData = [ |
| 48 | + 'app_message' => str_repeat('x', 200), |
| 49 | + 'resource' => str_repeat('y', 200), |
| 50 | + 'params' => str_repeat('z', 200), |
| 51 | + ]; |
| 52 | + |
| 53 | + $suffix = '...[truncated, change the truncated_fields.enable=1]'; |
| 54 | + $expected = [ |
| 55 | + 'app_message' => str_repeat('x', 200), |
| 56 | + 'resource' => str_repeat('y', 50) . $suffix, |
| 57 | + 'params' => str_repeat('z', 50) . $suffix, |
| 58 | + ]; |
| 59 | + |
| 60 | + self::assertSame($expected, $fieldsTruncator->truncate($logData)); |
| 61 | + } |
| 62 | +} |
0 commit comments