diff --git a/src/Service/RedisFailureHandler/FailureHandlingThrottler.php b/src/Service/RedisFailureHandler/FailureHandlingThrottler.php index 2969ce4..a21803c 100644 --- a/src/Service/RedisFailureHandler/FailureHandlingThrottler.php +++ b/src/Service/RedisFailureHandler/FailureHandlingThrottler.php @@ -2,9 +2,11 @@ namespace Maba\Bundle\GentleForceBundle\Service\RedisFailureHandler; +use Exception; use Maba\GentleForce\IncreaseResult; use Maba\GentleForce\RateLimitProvider; use Maba\GentleForce\ThrottlerInterface; +use Predis\ClientException; use Predis\Connection\ConnectionException; use Psr\Log\LoggerInterface; @@ -29,9 +31,9 @@ public function checkAndIncrease($useCaseKey, $identifier) try { return $this->throttler->checkAndIncrease($useCaseKey, $identifier); } catch (ConnectionException $exception) { - $this->logger->error('Connection to redis failed while checking rate limits', [ - 'exception' => $exception, - ]); + $this->logException($exception); + } catch (ClientException $clientException) { + $this->logException($clientException); } $usagesAvailable = 0; @@ -51,4 +53,11 @@ public function reset($useCaseKey, $identifier) { return $this->throttler->reset($useCaseKey, $identifier); } + + private function logException(Exception $exception) + { + $this->logger->error('Connection to redis failed while checking rate limits', [ + 'exception' => $exception, + ]); + } } diff --git a/tests/Functional/FunctionalRecaptchaTemplateStrategyTest.php b/tests/Functional/FunctionalRecaptchaTemplateStrategyTest.php index ed8e41f..78bffa3 100644 --- a/tests/Functional/FunctionalRecaptchaTemplateStrategyTest.php +++ b/tests/Functional/FunctionalRecaptchaTemplateStrategyTest.php @@ -16,25 +16,32 @@ public function testRecaptchaTemplateStrategy() $request = $this->createRequest(self::PATH_API1, self::DEFAULT_IP); $request->setMethod('POST'); $response = $this->handleRequest($request); - $this->assertStringContainsString( + $this->checkIfStringExists( 'getContent(), 'Expected response to contain tag' ); - $this->assertStringContainsString( + $this->checkIfStringExists( 'my_recaptcha_site_key', $response->getContent(), 'Expected response to contain recaptcha site key' ); - $this->assertStringContainsString( + $this->checkIfStringExists( '/prefix/gentle-force/recaptcha/unlock', $response->getContent(), 'Expected response to contain unlock URL' ); - $this->assertStringContainsString( + $this->checkIfStringExists( 'Custom template', $response->getContent(), 'Expected response to be generated from customized template' ); } + + private function checkIfStringExists(string $needle, string $haystack, string $message) + { + if (mb_strpos($haystack, $needle) === false) { + $this->fail($message); + } + } }