105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace HerrleinIT\LogHandler\Tests\Feature;
|
|
|
|
use ErrorException;
|
|
use HerrleinIT\LogHandler\Handlers\LogForwardingHandler;
|
|
use HerrleinIT\LogHandler\LogForwarder;
|
|
use HerrleinIT\LogHandler\Tests\Fakes\FakeLoghandlerClient;
|
|
use HerrleinIT\LogHandler\Tests\TestCase;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
|
|
|
class LogForwardingTest extends TestCase
|
|
{
|
|
public function test_exception_payload_is_forwarded(): void
|
|
{
|
|
$client = new FakeLoghandlerClient();
|
|
$this->app->instance(LogForwarder::class, new LogForwarder($this->app['config'], $client));
|
|
|
|
$exception = new ErrorException('Testerror', 0, E_WARNING, '/var/test/testfile.php', 1337);
|
|
|
|
$channel = Log::channel('loghandler');
|
|
|
|
$this->assertContains(
|
|
LogForwardingHandler::class,
|
|
array_map(fn ($handler) => $handler::class, $channel->getLogger()->getHandlers())
|
|
);
|
|
|
|
$channel->warning('Ignored message', ['exception' => $exception]);
|
|
|
|
$this->assertCount(1, $client->requests);
|
|
|
|
$request = $client->requests[0];
|
|
$this->assertSame('POST', $request['method']);
|
|
$this->assertSame('http://test.example/api/error/create', $request['uri']);
|
|
$this->assertSame('Bearer test-token', $request['options']['headers']['Authorization']);
|
|
|
|
AssertableJson::fromArray($request['options']['json'])
|
|
->where('source', config('loghandler.source'))
|
|
->where('error', 'Testerror')
|
|
->where('type', 'E_WARNING')
|
|
->where('file', '/var/test/testfile.php')
|
|
->where('line', '1337')
|
|
->where('trace', fn ($trace) => is_string($trace) && $trace !== '')
|
|
->etc();
|
|
}
|
|
|
|
public function test_disabled_handler_skips_forwarding(): void
|
|
{
|
|
$client = new FakeLoghandlerClient();
|
|
config()->set('loghandler.enabled', false);
|
|
|
|
$this->app->instance(LogForwarder::class, new LogForwarder($this->app['config'], $client));
|
|
|
|
Log::channel('loghandler')->error('Testerror');
|
|
|
|
$this->assertCount(0, $client->requests);
|
|
}
|
|
|
|
public function test_missing_token_skips_forwarding(): void
|
|
{
|
|
$client = new FakeLoghandlerClient();
|
|
config()->set('loghandler.token', '');
|
|
|
|
$this->app->instance(LogForwarder::class, new LogForwarder($this->app['config'], $client));
|
|
|
|
Log::channel('loghandler')->error('Testerror');
|
|
|
|
$this->assertCount(0, $client->requests);
|
|
}
|
|
|
|
public function test_stack_channel_includes_handler(): void
|
|
{
|
|
$client = new FakeLoghandlerClient();
|
|
$this->app->instance(LogForwarder::class, new LogForwarder($this->app['config'], $client));
|
|
|
|
$stack = Log::channel('stack');
|
|
|
|
$this->assertContains(
|
|
LogForwardingHandler::class,
|
|
array_map(fn ($handler) => $handler::class, $stack->getHandlers())
|
|
);
|
|
|
|
$stack->error('Stack error', [
|
|
'file' => '/var/test/testfile.php',
|
|
'line' => 99,
|
|
]);
|
|
|
|
$this->assertCount(1, $client->requests);
|
|
|
|
$request = $client->requests[0];
|
|
$this->assertSame('POST', $request['method']);
|
|
$this->assertSame('http://test.example/api/error/create', $request['uri']);
|
|
|
|
AssertableJson::fromArray($request['options']['json'])
|
|
->where('source', config('loghandler.source'))
|
|
->where('error', 'Stack error')
|
|
->where('type', 'E_ERROR')
|
|
->where('file', '/var/test/testfile.php')
|
|
->where('line', '99')
|
|
->where('trace', '')
|
|
->etc();
|
|
}
|
|
}
|