|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +namespace Naoray\LaravelGithubMonolog\Tests\Issues\Formatters; |
| 4 | + |
3 | 5 | use Naoray\LaravelGithubMonolog\Issues\Formatters\StackTraceFormatter; |
4 | 6 |
|
5 | 7 | beforeEach(function () { |
|
54 | 56 | ->toContain('/app/Services/TestService.php') |
55 | 57 | ->not->toContain('[Vendor frames]'); |
56 | 58 | }); |
| 59 | + |
| 60 | +test('it replaces base path in stack traces', function () { |
| 61 | + $formatter = new StackTraceFormatter(); |
| 62 | + $basePath = base_path(); |
| 63 | + |
| 64 | + $stackTrace = <<<TRACE |
| 65 | +#0 {$basePath}/app/Services/ImageService.php(25): Spatie\\Image\\Image->loadFile() |
| 66 | +#1 {$basePath}/vendor/laravel/framework/src/Foundation/Application.php(1235): App\\Services\\ImageService->process() |
| 67 | +#2 {$basePath}/artisan(13): Illuminate\\Foundation\\Application->run() |
| 68 | +TRACE; |
| 69 | + |
| 70 | + // Test simplified trace (with vendor frame collapsing) |
| 71 | + expect($formatter->format($stackTrace, true)) |
| 72 | + ->toBe(<<<TRACE |
| 73 | +#00 /app/Services/ImageService.php(25): Spatie\\Image\\Image->loadFile() |
| 74 | +[Vendor frames] |
| 75 | +TRACE |
| 76 | + ); |
| 77 | + |
| 78 | + // Test full trace (without vendor frame collapsing) |
| 79 | + expect($formatter->format($stackTrace, false)) |
| 80 | + ->toBe(<<<TRACE |
| 81 | +#00 /app/Services/ImageService.php(25): Spatie\\Image\\Image->loadFile() |
| 82 | +#01 /vendor/laravel/framework/src/Foundation/Application.php(1235): App\\Services\\ImageService->process() |
| 83 | +#02 /artisan(13): Illuminate\\Foundation\\Application->run() |
| 84 | +TRACE |
| 85 | + ); |
| 86 | +}); |
| 87 | + |
| 88 | +test('it handles empty base path correctly', function () { |
| 89 | + $formatter = new StackTraceFormatter(); |
| 90 | + |
| 91 | + $stackTrace = <<<TRACE |
| 92 | +#0 /absolute/path/to/app/Services/ImageService.php(25): someMethod() |
| 93 | +#1 /vendor/package/src/File.php(10): otherMethod() |
| 94 | +TRACE; |
| 95 | + |
| 96 | + $result = $formatter->format($stackTrace, true); |
| 97 | + |
| 98 | + expect($result) |
| 99 | + ->toBe(<<<TRACE |
| 100 | +#00 /absolute/path/to/app/Services/ImageService.php(25): someMethod() |
| 101 | +[Vendor frames] |
| 102 | +TRACE |
| 103 | + ); |
| 104 | +}); |
| 105 | + |
| 106 | +test('it preserves non-stack-trace lines', function () { |
| 107 | + $formatter = new StackTraceFormatter(); |
| 108 | + |
| 109 | + $stackTrace = <<<TRACE |
| 110 | +[2024-03-21 12:00:00] Error: Something went wrong |
| 111 | +#0 /app/Services/Service.php(25): someMethod() |
| 112 | +#1 /vendor/package/src/File.php(10): otherMethod() |
| 113 | +TRACE; |
| 114 | + |
| 115 | + $result = $formatter->format($stackTrace, true); |
| 116 | + |
| 117 | + expect($result) |
| 118 | + ->toBe(<<<TRACE |
| 119 | +[2024-03-21 12:00:00] Error: Something went wrong |
| 120 | +#00 /app/Services/Service.php(25): someMethod() |
| 121 | +[Vendor frames] |
| 122 | +TRACE |
| 123 | + ); |
| 124 | +}); |
| 125 | + |
| 126 | +test('it recognizes artisan lines as vendor frames', function () { |
| 127 | + $formatter = new StackTraceFormatter(); |
| 128 | + $basePath = base_path(); |
| 129 | + |
| 130 | + $stackTrace = <<<TRACE |
| 131 | +#0 {$basePath}/app/Console/Commands/ImportCommand.php(25): handle() |
| 132 | +#1 {$basePath}/artisan(13): Illuminate\\Foundation\\Application->run() |
| 133 | +#2 {$basePath}/artisan(37): require() |
| 134 | +TRACE; |
| 135 | + |
| 136 | + // Test simplified trace (with vendor frame collapsing) |
| 137 | + expect($formatter->format($stackTrace, true)) |
| 138 | + ->toBe(<<<TRACE |
| 139 | +#00 /app/Console/Commands/ImportCommand.php(25): handle() |
| 140 | +[Vendor frames] |
| 141 | +TRACE |
| 142 | + ); |
| 143 | + |
| 144 | + // Test that artisan lines are preserved in full trace |
| 145 | + expect($formatter->format($stackTrace, false)) |
| 146 | + ->toBe(<<<TRACE |
| 147 | +#00 /app/Console/Commands/ImportCommand.php(25): handle() |
| 148 | +#01 /artisan(13): Illuminate\\Foundation\\Application->run() |
| 149 | +#02 /artisan(37): require() |
| 150 | +TRACE |
| 151 | + ); |
| 152 | +}); |
| 153 | + |
| 154 | +test('it collapses multiple artisan lines into single vendor frame', function () { |
| 155 | + $formatter = new StackTraceFormatter(); |
| 156 | + |
| 157 | + $stackTrace = <<<TRACE |
| 158 | +#0 /artisan(13): Illuminate\\Foundation\\Application->run() |
| 159 | +#1 /vendor/laravel/framework/src/Foundation/Application.php(1235): handle() |
| 160 | +#2 /artisan(37): require() |
| 161 | +TRACE; |
| 162 | + |
| 163 | + expect($formatter->format($stackTrace, true)) |
| 164 | + ->toBe('[Vendor frames]'); |
| 165 | +}); |
0 commit comments