generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Morning-Train/feature/request-logger
Feature/request logger
- Loading branch information
Showing
8 changed files
with
140 additions
and
4 deletions.
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
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
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,14 @@ | ||
<?php | ||
|
||
namespace Morningtrain\LaravelEconomic\RequestLogger; | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use Illuminate\Http\Client\Request; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
interface RequestLogger | ||
{ | ||
public function onRequest(Request $request); | ||
|
||
public function onResponse(Response $response): ResponseInterface; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Morningtrain\LaravelEconomic\RequestLogger; | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use Illuminate\Http\Client\Request; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class VoidRequestLogger implements RequestLogger | ||
{ | ||
public function onRequest(Request $request) | ||
{ | ||
// | ||
} | ||
|
||
public function onResponse(Response $response): ResponseInterface | ||
{ | ||
return $response; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Morningtrain\LaravelEconomic\Tests\TestClasses; | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use Illuminate\Http\Client\Request; | ||
use Morningtrain\LaravelEconomic\RequestLogger\RequestLogger; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class TestRequestLogger implements RequestLogger | ||
{ | ||
public int $onRequestCalledTimes = 0; | ||
|
||
public int $onResponseCalledTimes = 0; | ||
|
||
public function onRequest(Request $request) | ||
{ | ||
$this->onRequestCalledTimes++; | ||
} | ||
|
||
public function onResponse(Response $response): ResponseInterface | ||
{ | ||
$this->onResponseCalledTimes++; | ||
|
||
return $response; | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Morningtrain\Economic\Resources\Customer; | ||
use Morningtrain\LaravelEconomic\RequestLogger\RequestLogger; | ||
use Morningtrain\LaravelEconomic\Tests\TestClasses\TestRequestLogger; | ||
|
||
beforeEach(function () { | ||
$this->instance(RequestLogger::class, new TestRequestLogger()); | ||
}); | ||
|
||
it('calls request logger before and after request', function () { | ||
$logger = app(RequestLogger::class); | ||
|
||
Http::fake([ | ||
'https://restapi.e-conomic.com/customers*' => Http::response([]), | ||
]); | ||
|
||
Customer::where('name', 'Morningtrain')->first(); | ||
|
||
expect($logger->onRequestCalledTimes)->toBe(1); | ||
expect($logger->onResponseCalledTimes)->toBe(1); | ||
}); |