From d611f8e75e56aa345abf8cdddf11cad59a44a9a2 Mon Sep 17 00:00:00 2001 From: hectorhammett Date: Wed, 11 Dec 2024 00:34:57 +0000 Subject: [PATCH] Update max lenght to be in a variable --- src/HttpHandler/Guzzle6HttpHandler.php | 7 ++++++- src/Logging/LoggingTrait.php | 6 ++++-- tests/ApplicationDefaultCredentialsTest.php | 5 +++++ tests/HttpHandler/Guzzle7HttpHandlerTest.php | 6 ++---- tests/Logging/LoggingTraitTest.php | 16 ++++++++++++++-- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/HttpHandler/Guzzle6HttpHandler.php b/src/HttpHandler/Guzzle6HttpHandler.php index 012d34388..cfdbcb183 100644 --- a/src/HttpHandler/Guzzle6HttpHandler.php +++ b/src/HttpHandler/Guzzle6HttpHandler.php @@ -65,7 +65,7 @@ public function __invoke(RequestInterface $request, array $options = []) $response = $this->client->send($request, $options); if ($this->logger) { - $this->responseLog($response, $requestEvent); + $this->responseLog($response, $requestEvent); } return $response; @@ -120,6 +120,11 @@ public function requestLog(RequestInterface $request, array $options): RpcLogEve return $requestEvent; } + /** + * @internal + * @param RequestInterface $request + * @param array $options + */ public function responseLog(ResponseInterface $response, RpcLogEvent $requestEvent): void { $responseEvent = new RpcLogEvent($requestEvent->milliseconds); diff --git a/src/Logging/LoggingTrait.php b/src/Logging/LoggingTrait.php index 97c51368a..2441a9bd7 100644 --- a/src/Logging/LoggingTrait.php +++ b/src/Logging/LoggingTrait.php @@ -126,10 +126,12 @@ private function getJwtToken(array $headers): null|array */ private function truncatePayload(null|string $payload): null|string { - if (is_null($payload) || strlen($payload) <= 500) { + $maxLength = 500; + + if (is_null($payload) || strlen($payload) <= $maxLength) { return $payload; } - return substr($payload, 0, 500) . '...'; + return substr($payload, 0, $maxLength) . '...'; } } diff --git a/tests/ApplicationDefaultCredentialsTest.php b/tests/ApplicationDefaultCredentialsTest.php index 89a4afafa..cf6b542d5 100644 --- a/tests/ApplicationDefaultCredentialsTest.php +++ b/tests/ApplicationDefaultCredentialsTest.php @@ -793,6 +793,11 @@ public function testGetDefaultLoggerReturnsNullIfNotEnvVar() $logger = ApplicationDefaultCredentials::getDefaultLogger(); $this->assertNull($logger); + + putenv($this::SDK_DEBUG_ENV_VAR . '='); + $logger = ApplicationDefaultCredentials::getDefaultLogger(); + + $this->assertNull($logger); } public function testGetDefaultLoggerRaiseAWarningIfMisconfiguredAndReturnsNull() diff --git a/tests/HttpHandler/Guzzle7HttpHandlerTest.php b/tests/HttpHandler/Guzzle7HttpHandlerTest.php index 97cb8688d..53147be3a 100644 --- a/tests/HttpHandler/Guzzle7HttpHandlerTest.php +++ b/tests/HttpHandler/Guzzle7HttpHandlerTest.php @@ -74,11 +74,9 @@ public function testLoggerDoesNotGetsCalledIfLoggerIsNotPassed() $request = new Request('GET', 'https://domain.tld'); $options = ['key' => 'value']; - /** - * @var LoggerInterface $mockLogger - * @var ClientInterface $mockClient - */ $handler = new Guzzle7HttpHandler($this->client->reveal()); $handler->async($request, $options)->wait(); + + $this->expectOutputString(''); } } diff --git a/tests/Logging/LoggingTraitTest.php b/tests/Logging/LoggingTraitTest.php index edc85b5bd..211fc4d0f 100644 --- a/tests/Logging/LoggingTraitTest.php +++ b/tests/Logging/LoggingTraitTest.php @@ -25,11 +25,23 @@ class LoggingTraitTest extends BaseTest { - private MockClassWithLogger $loggerContainer; + private $loggerContainer; public function setUp(): void { - $this->loggerContainer = new MockClassWithLogger(); + $this->loggerContainer = new class() { + use LoggingTrait { + logRequest as public; + logResponse as public; + } + + private LoggerInterface $logger; + + public function __construct() + { + $this->logger = new StdOutLogger(); + } + }; } public function testLogRequest()