Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LoggingPostRequestSender decorator
- Loading branch information
1 parent
3fdd723
commit ca7b73c
Showing
3 changed files
with
101 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace Jeroen\PostRequestSender; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
class LoggingPostRequestSender implements PostRequestSender { | ||
|
||
/** | ||
* @param string[] $fieldsToOmit | ||
*/ | ||
public function __construct( | ||
private PostRequestSender $requestSender, | ||
private LoggerInterface $logger, | ||
private readonly string $logLevel = LogLevel::INFO, | ||
private readonly array $fieldsToOmit = [] | ||
) { | ||
} | ||
|
||
public function post( string $url, array $fields ): ResponseInterface { | ||
$this->logger->log( | ||
$this->logLevel, | ||
'Post request to ' . $url, | ||
array_diff_key( $fields, array_flip( $this->fieldsToOmit ) ) | ||
); | ||
|
||
return $this->requestSender->post( $url, $fields ); | ||
} | ||
|
||
} |
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,64 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace Jeroen\PostRequestSender\Tests; | ||
|
||
use Jeroen\PostRequestSender\LoggingPostRequestSender; | ||
use Jeroen\PostRequestSender\StubPostRequestSender; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LogLevel; | ||
use WMDE\PsrLogTestDoubles\LoggerSpy; | ||
|
||
/** | ||
* @covers \Jeroen\PostRequestSender\LoggingPostRequestSender | ||
* @covers \Jeroen\PostRequestSender\StubPostRequestSender | ||
* @covers \Jeroen\PostRequestSender\PostRequest | ||
*/ | ||
class LoggingPostRequestSenderTest extends TestCase { | ||
|
||
public function testLogsRequests(): void { | ||
$loggerSpy = new LoggerSpy(); | ||
|
||
$sender = new LoggingPostRequestSender( | ||
new StubPostRequestSender(), | ||
$loggerSpy | ||
); | ||
|
||
$sender->post( 'https://first.url', [ 'foo' => 'bar' ] ); | ||
$sender->post( 'https://second.url', [ 'baz' => 'bah', 'pew' => 1337 ] ); | ||
|
||
$this->assertSame( | ||
[ 'Post request to https://first.url', 'Post request to https://second.url' ], | ||
$loggerSpy->getLogCalls()->getMessages() | ||
); | ||
|
||
$this->assertSame( | ||
[ 'baz' => 'bah', 'pew' => 1337 ], | ||
$loggerSpy->getLogCalls()->getLastCall()?->getContext() | ||
); | ||
|
||
$this->assertSame( | ||
LogLevel::INFO, | ||
$loggerSpy->getLogCalls()->getLastCall()?->getLevel() | ||
); | ||
} | ||
|
||
public function testOmitsExcludedFields(): void { | ||
$loggerSpy = new LoggerSpy(); | ||
|
||
$sender = new LoggingPostRequestSender( | ||
new StubPostRequestSender(), | ||
$loggerSpy, | ||
fieldsToOmit: [ 'secret' ] | ||
); | ||
|
||
$sender->post( 'https://example.url', [ 'foo' => 'bar', 'secret' => 1337, 'baz' => 'bah' ] ); | ||
|
||
$this->assertSame( | ||
[ 'foo' => 'bar', 'baz' => 'bah' ], | ||
$loggerSpy->getLogCalls()->getLastCall()?->getContext() | ||
); | ||
} | ||
|
||
} |