-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bd0967
commit 77ce319
Showing
4 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace ShipSaasUniqueRequestLogger\Tests\Integration; | ||
|
||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Str; | ||
use ShipSaasUniqueRequestLogger\Constants\GlobalConstants; | ||
use ShipSaasUniqueRequestLogger\Tests\TestCase; | ||
|
||
class ShipSaasLoggerTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->createFakeApp(); | ||
} | ||
|
||
public function testWriteLogShouldContainsRequestIdNormalView() | ||
{ | ||
$res = $this->json('GET', 'write-logs') | ||
->assertOk() | ||
->assertSee('ok') | ||
->assertHeader(GlobalConstants::REQUEST_ID_HEADER); | ||
|
||
$headerValue = $res->headers->get(GlobalConstants::REQUEST_ID_HEADER); | ||
|
||
// header is valid | ||
$this->assertNotNull($headerValue); | ||
$this->assertTrue(Str::isUlid($headerValue)); | ||
|
||
// log file assertions | ||
$filePath = __DIR__ . "/logs/$headerValue.log"; | ||
$this->assertFileExists($filePath); | ||
|
||
// read the logs | ||
$logs = file_get_contents($filePath); | ||
$lines = explode("\n", $logs); | ||
|
||
// we write 2 lines, but the last \n count too => 3 | ||
$this->assertCount(3, $lines); | ||
|
||
$this->assertStringContainsString('{"requestId":"'.$headerValue.'"}', $lines[0]); | ||
$this->assertStringContainsString('{"requestId":"'.$headerValue.'"}', $lines[1]); | ||
} | ||
|
||
public function testWriteLogShouldContainsRequestIdJsonView() | ||
{ | ||
config([ | ||
'logging.channels.shipsaas-logger.use_json_format' => true, | ||
]); | ||
|
||
$res = $this->json('GET', 'write-logs') | ||
->assertOk() | ||
->assertSee('ok') | ||
->assertHeader(GlobalConstants::REQUEST_ID_HEADER); | ||
|
||
$headerValue = $res->headers->get(GlobalConstants::REQUEST_ID_HEADER); | ||
|
||
// header is valid | ||
$this->assertNotNull($headerValue); | ||
$this->assertTrue(Str::isUlid($headerValue)); | ||
|
||
// log file assertions | ||
$filePath = __DIR__ . "/logs/$headerValue.log"; | ||
$this->assertFileExists($filePath); | ||
|
||
// read the logs | ||
$logs = file_get_contents($filePath); | ||
$lines = explode("\n", $logs); | ||
|
||
// we write 2 lines, but the last \n count too => 3 | ||
$this->assertCount(3, $lines); | ||
|
||
$this->assertJson($lines[0]); | ||
$this->assertJson($lines[1]); | ||
|
||
$line0Json = json_decode($lines[0], true); | ||
$line1Json = json_decode($lines[1], true); | ||
|
||
$this->assertSame($headerValue, $line0Json['extra']['requestId']); | ||
$this->assertSame($headerValue, $line1Json['extra']['requestId']); | ||
} | ||
|
||
private function createFakeApp(): void | ||
{ | ||
config([ | ||
'logging.default' => 'shipsaas-logger', | ||
'logging.channels.shipsaas-logger' => [ | ||
'driver' => 'shipsaas-logger', | ||
'path' => __DIR__ . '/logs/hehe.log', | ||
'default_log_file' => __DIR__ . '/logs/hehe.log', | ||
'id-type' => 'ulid', | ||
'use_json_format' => false, | ||
], | ||
]); | ||
|
||
Route::get('write-logs', function () { | ||
Log::info('Test info log', [ | ||
'id' => 'this-is-id', | ||
]); | ||
|
||
Log::warning('Test warning pro', [ | ||
'id' => 'this-is-id-warning', | ||
]); | ||
|
||
return 'ok'; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace ShipSaasUniqueRequestLogger\Tests\Unit\EventHandlers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Events\Routing; | ||
use Illuminate\Support\Str; | ||
use ShipSaasUniqueRequestLogger\Constants\GlobalConstants; | ||
use ShipSaasUniqueRequestLogger\EventHandlers\GenerateUniqueRequestId; | ||
use ShipSaasUniqueRequestLogger\Tests\TestCase; | ||
|
||
class GenerateUniqueRequestIdTest extends TestCase | ||
{ | ||
public function testHandlerGeneratesAndSetsTheUniqueRequestIdUsingUuid() | ||
{ | ||
$request = $this->app->make(Request::class); | ||
|
||
config([ | ||
'logging.channels.shipsaas-logger.id-type' => 'uuid', | ||
]); | ||
|
||
$event = new Routing($request); | ||
|
||
$handler = new GenerateUniqueRequestId(); | ||
$handler->handle($event); | ||
|
||
$this->assertNotNull($request->getRequestId()); | ||
$this->assertTrue(Str::isUuid($request->getRequestId())); | ||
} | ||
|
||
public function testHandlerGeneratesAndSetsTheUniqueRequestIdUsingOrderedUuid() | ||
{ | ||
$request = $this->app->make(Request::class); | ||
|
||
config([ | ||
'logging.channels.shipsaas-logger.id-type' => 'orderedUuid', | ||
]); | ||
|
||
$event = new Routing($request); | ||
|
||
$handler = new GenerateUniqueRequestId(); | ||
$handler->handle($event); | ||
|
||
$this->assertNotNull($request->getRequestId()); | ||
$this->assertTrue(Str::isUuid($request->getRequestId())); | ||
} | ||
|
||
public function testHandlerGeneratesAndSetsTheUniqueRequestIdUsingUlid() | ||
{ | ||
$request = $this->app->make(Request::class); | ||
|
||
config([ | ||
'logging.channels.shipsaas-logger.id-type' => 'ulid', | ||
]); | ||
|
||
$event = new Routing($request); | ||
|
||
$handler = new GenerateUniqueRequestId(); | ||
$handler->handle($event); | ||
|
||
$this->assertNotNull($request->getRequestId()); | ||
$this->assertTrue(Str::isUlid($request->getRequestId())); | ||
} | ||
|
||
public function testHandlerSetsTheUniqueRequestIdFromHeader() | ||
{ | ||
$request = $this->app->make(Request::class); | ||
$request->headers->set(GlobalConstants::REQUEST_ID_HEADER, 'test'); | ||
|
||
$event = new Routing($request); | ||
|
||
$handler = new GenerateUniqueRequestId(); | ||
$handler->handle($event); | ||
|
||
$this->assertSame('test', $request->getRequestId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace ShipSaasUniqueRequestLogger\Tests\Unit\Helpers; | ||
|
||
use ShipSaasUniqueRequestLogger\Helpers\Helpers; | ||
use ShipSaasUniqueRequestLogger\Tests\TestCase; | ||
|
||
class HelpersTest extends TestCase | ||
{ | ||
public function testGetRequestIdReturnsRequestId() | ||
{ | ||
request()->setRequestId('hehe'); | ||
|
||
$rqId = Helpers::getUniqueRequestId(); | ||
|
||
$this->assertSame('hehe', $rqId); | ||
} | ||
|
||
public function testGetRequestIdReturnsNull() | ||
{ | ||
$rqId = Helpers::getUniqueRequestId(); | ||
|
||
$this->assertNull($rqId); | ||
} | ||
} |