-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoggerServiceTest.php
More file actions
109 lines (83 loc) · 3.54 KB
/
Copy pathLoggerServiceTest.php
File metadata and controls
109 lines (83 loc) · 3.54 KB
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
<?php
namespace SeQura\Middleware\Tests\Unit;
use PHPUnit\Framework\TestCase;
use SeQura\Middleware\Service\Infrastructure\LoggerService;
/**
* Class LoggerServiceTest
*
* @package SeQura\Middleware\Tests\Unit
*/
class LoggerServiceTest extends TestCase
{
private \ReflectionMethod $formatContextValue;
private LoggerService $loggerService;
protected function setUp(): void
{
parent::setUp();
$this->loggerService = LoggerService::getInstance();
$this->formatContextValue = new \ReflectionMethod(LoggerService::class, 'formatContextValue');
$this->formatContextValue->setAccessible(true);
}
public function testThrowableIsFormattedWithoutPrintR(): void
{
$exception = new \RuntimeException('Something went wrong');
$result = $this->formatContextValue->invoke($this->loggerService, $exception);
$this->assertStringContainsString('RuntimeException: Something went wrong', $result);
$this->assertStringContainsString(__FILE__, $result);
// Must NOT contain full stack trace output from print_r
$this->assertStringNotContainsString('#0 ', $result);
$this->assertStringNotContainsString('Array', $result);
}
public function testNestedExceptionOnlyShowsOuterException(): void
{
$previous = new \InvalidArgumentException('Root cause');
$exception = new \RuntimeException('Wrapper', 0, $previous);
$result = $this->formatContextValue->invoke($this->loggerService, $exception);
$this->assertStringContainsString('RuntimeException: Wrapper', $result);
// Should NOT recurse into the previous exception
$this->assertStringNotContainsString('Root cause', $result);
$this->assertStringNotContainsString('#0 ', $result);
}
public function testObjectUsesJsonEncode(): void
{
$object = new \stdClass();
$object->key = 'value';
$object->number = 42;
$result = $this->formatContextValue->invoke($this->loggerService, $object);
$this->assertStringContainsString('"key":"value"', $result);
$this->assertStringContainsString('"number":42', $result);
}
public function testNonSerializableObjectFallsBackToClassName(): void
{
$object = new class {
public float $value = NAN;
};
$result = $this->formatContextValue->invoke($this->loggerService, $object);
// JSON_PARTIAL_OUTPUT_ON_ERROR will produce output or fall back to class name
$this->assertNotEmpty($result);
// Should not crash or produce unbounded output
$this->assertLessThan(1000, strlen($result));
}
public function testScalarStringIsReturnedAsIs(): void
{
$result = $this->formatContextValue->invoke($this->loggerService, 'simple string');
$this->assertEquals('simple string', $result);
}
public function testIntegerIsReturnedViaPrintR(): void
{
$result = $this->formatContextValue->invoke($this->loggerService, 42);
$this->assertEquals('42', $result);
}
public function testArrayIsReturnedViaPrintR(): void
{
$result = $this->formatContextValue->invoke($this->loggerService, ['a', 'b', 'c']);
$this->assertStringContainsString('a', $result);
$this->assertStringContainsString('b', $result);
$this->assertStringContainsString('c', $result);
}
public function testNullIsHandled(): void
{
$result = $this->formatContextValue->invoke($this->loggerService, null);
$this->assertIsString($result);
}
}