From 81fc1d3a4481a3553d9a3e854bc1c915487dadba Mon Sep 17 00:00:00 2001 From: Samuel Gfeller Date: Mon, 18 Dec 2023 18:39:28 +0100 Subject: [PATCH] Added monolog TestHandler [SLE-192] --- config/container.php | 20 ++++++++++--------- config/env/env.test.php | 7 ++----- .../Integration/User/UserCreateActionTest.php | 15 +++++++------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/config/container.php b/config/container.php index 39975972..29c6d42e 100644 --- a/config/container.php +++ b/config/container.php @@ -48,17 +48,19 @@ $logger = new Logger('app'); - // Check if config file contains the key 'path' env.test.php removes this key to prevent logging - if (isset($loggerSettings['path'])) { - $filename = sprintf('%s/app.log', $loggerSettings['path']); - $level = $loggerSettings['level']; - $rotatingFileHandler = new RotatingFileHandler($filename, 0, $level, true, 0777); - // The last "true" here tells monolog to remove empty []'s - $rotatingFileHandler->setFormatter(new LineFormatter(null, 'Y-m-d H:i:s', false, true)); - $logger->pushHandler($rotatingFileHandler); + // When testing, 'test' value is true which means the monolog test handler should be used + if (isset($loggerSettings['test']) && $loggerSettings['test'] === true) { + return $logger->pushHandler(new \Monolog\Handler\TestHandler()); } - return $logger; + // Instantiate logger with rotating file handler + $filename = sprintf('%s/app.log', $loggerSettings['path']); + $level = $loggerSettings['level']; + // With the RotatingFileHandler, a new log file is created every day + $rotatingFileHandler = new RotatingFileHandler($filename, 0, $level, true, 0777); + // The last "true" here tells monolog to remove empty []'s + $rotatingFileHandler->setFormatter(new LineFormatter(null, 'Y-m-d H:i:s', false, true)); + return $logger->pushHandler($rotatingFileHandler); }, // HTTP factories diff --git a/config/env/env.test.php b/config/env/env.test.php index cd52e534..8ff31c8f 100644 --- a/config/env/env.test.php +++ b/config/env/env.test.php @@ -16,8 +16,5 @@ // Add example.com to allowed origin to test out CORS $settings['api']['allowed_origin'] = 'https://example.com/'; -// Overwrite logger settings with missing 'path' so that container can't create a proper instance with the log file -$settings['logger'] = [ - // Key "path" must not exist - 'test' => new \Monolog\Handler\TestHandler(), -]; +// Enable test mode for the logger +$settings['logger']['test'] = true; diff --git a/tests/Integration/User/UserCreateActionTest.php b/tests/Integration/User/UserCreateActionTest.php index 0e4e336c..17f0d1d8 100644 --- a/tests/Integration/User/UserCreateActionTest.php +++ b/tests/Integration/User/UserCreateActionTest.php @@ -40,8 +40,8 @@ class UserCreateActionTest extends TestCase * @dataProvider \App\Test\Provider\User\UserCreateProvider::userCreateAuthorizationCases() * * @param array $authenticatedUserAttr authenticated user attributes containing the user_role_id - * @param UserRole $newUserRole user role id of new user - * @param array $expectedResult HTTP status code, bool if db_entry_created and json_response + * @param UserRole|null $newUserRole user role id of new user + * @param array $expectedResult HTTP status code, bool if db entry is created and json_response * * @return void */ @@ -63,7 +63,9 @@ public function testUserSubmitCreateAuthorization( 'email' => 'daniel.riccardo@notmclaren.com', 'password' => '12345678', 'password2' => '12345678', - 'user_role_id' => $newUserRole ? $userRoleFinderRepository->findUserRoleIdByName($newUserRole->value) : $newUserRole, + 'user_role_id' => $newUserRole ? $userRoleFinderRepository->findUserRoleIdByName( + $newUserRole->value + ) : $newUserRole, 'status' => 'unverified', 'language' => 'en_US', ]; @@ -168,7 +170,7 @@ public function testUserSubmitCreateInvalid(array $requestBody, array $jsonRespo $response = $this->app->handle($request); // Assert 422 Unprocessable Entity, which means validation error if request body contains user_role_id // even if it's an empty string - self::assertSame(StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY, $response->getStatusCode()); + self::assertSame(StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY, $response->getStatusCode()); // Database must be unchanged - only one row (authenticated user) expected in user table @@ -177,10 +179,7 @@ public function testUserSubmitCreateInvalid(array $requestBody, array $jsonRespo } /** - * Test that user with same email as existing user cannot be created. - * - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * Test that user with the same email as existing user cannot be created. * * @return void */