Skip to content

Commit

Permalink
Added monolog TestHandler [SLE-192]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Dec 18, 2023
1 parent 8f22073 commit 81fc1d3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
20 changes: 11 additions & 9 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions config/env/env.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 7 additions & 8 deletions tests/Integration/User/UserCreateActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -63,7 +63,9 @@ public function testUserSubmitCreateAuthorization(
'email' => '[email protected]',
'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',
];
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down

0 comments on commit 81fc1d3

Please sign in to comment.