Skip to content

Commit

Permalink
removed code duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 committed May 8, 2024
1 parent f717afa commit af1d4b9
Showing 1 changed file with 57 additions and 83 deletions.
140 changes: 57 additions & 83 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@

class LoggerTest extends TestCase
{
private const REQUEST_FORMAT = 'Request {method} {url} {contentType}';
private const REQUEST_BODY_FORMAT = 'Request Body {body}';
private const REQUEST_HEADERS_FORMAT = 'Request Headers {headers}';
private const RESPONSE_FORMAT = 'Response {statusCode} {contentLength} {contentType}';
private const RESPONSE_BODY_FORMAT = 'Response Body {body}';
private const RESPONSE_HEADERS_FORMAT = 'Response Headers {headers}';

private const TEST_URL = 'https://some/path';
private const TEST_HEADERS = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];

public function testLogLevels()
{
MockHelper::getMockLogger()->assertLastEntries($this->logAndGetEntry(LogLevel::INFO));
Expand Down Expand Up @@ -53,32 +68,32 @@ public function testConsoleLogger()
$consoleLoggerMock = new ConsoleLogger([$printer, 'printMessage']);
$loggingConfig = MockHelper::getLoggingConfiguration(null, null, null, null, $consoleLoggerMock);
$apiLogger = new ApiLogger($loggingConfig);
$apiLogger->logRequest(new Request('https://some/path', MockHelper::getClient()));
$apiLogger->logRequest(new Request(self::TEST_URL, MockHelper::getClient()));

$this->assertEquals(["%s: %s\n", 'info', 'Request Get https://some/path '], $printer->args);
$this->assertEquals(["%s: %s\n", LogLevel::INFO, 'Request Get https://some/path '], $printer->args);
}

public function testDefaultLoggingConfiguration()
{
$apiLogger = MockHelper::getClient()->getApiLogger();
$this->assertInstanceOf(ApiLoggerInterface::class, $apiLogger);

$request = new Request('https://some/path');
$request = new Request(self::TEST_URL);
$response = MockHelper::getClient()->getHttpClient()->execute($request);
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path',
'url' => self::TEST_URL,
'contentType' => null
])
);
$apiLogger->logResponse($response);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Response {statusCode} {contentLength} {contentType}', [
new LogEntry(LogLevel::INFO, self::RESPONSE_FORMAT, [
'statusCode' => 200,
'contentLength' => null,
'contentType' => 'application/json'
'contentType' => Format::JSON
])
);
}
Expand All @@ -88,15 +103,15 @@ public function testLoggingRequestShouldIncludeInQuery()
$requestParams = MockHelper::getClient()->validateParameters([
QueryParam::init('key', 'value')
]);
$request = new Request('https://some/path', MockHelper::getClient(), $requestParams);
$request = new Request(self::TEST_URL, MockHelper::getClient(), $requestParams);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
null,
null,
MockHelper::getRequestLoggingConfiguration(true)
));
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path?key=value',
'contentType' => null
Expand All @@ -109,13 +124,13 @@ public function testLoggingRequestContentType()
$requestParams = MockHelper::getClient()->validateParameters([
HeaderParam::init('Content-Type', 'my-content-type')
]);
$request = new Request('https://some/path', MockHelper::getClient(), $requestParams);
$request = new Request(self::TEST_URL, MockHelper::getClient(), $requestParams);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration());
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path',
'url' => self::TEST_URL,
'contentType' => 'my-content-type'
])
);
Expand All @@ -126,7 +141,7 @@ public function testLoggingRequestFileAsBody()
$requestParams = MockHelper::getClient()->validateParameters([
BodyParam::init(MockHelper::getFileWrapper()),
]);
$request = new Request('https://some/path', MockHelper::getClient(), $requestParams);
$request = new Request(self::TEST_URL, MockHelper::getClient(), $requestParams);
$request->setBodyFormat(Format::JSON, [CoreHelper::class, 'serialize']);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
null,
Expand All @@ -138,12 +153,12 @@ public function testLoggingRequestFileAsBody()
));
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path',
'url' => self::TEST_URL,
'contentType' => 'application/octet-stream'
]),
new LogEntry('info', 'Request Body {body}', [
new LogEntry(LogLevel::INFO, self::REQUEST_BODY_FORMAT, [
'body' => 'This test file is created to test CoreFileWrapper functionality'
])
);
Expand All @@ -156,7 +171,7 @@ public function testLoggingRequestBody()
'key' => 'value'
]),
]);
$request = new Request('https://some/path', MockHelper::getClient(), $requestParams);
$request = new Request(self::TEST_URL, MockHelper::getClient(), $requestParams);
$request->setBodyFormat(Format::JSON, [CoreHelper::class, 'serialize']);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
null,
Expand All @@ -168,12 +183,12 @@ public function testLoggingRequestBody()
));
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path',
'contentType' => 'application/json'
'url' => self::TEST_URL,
'contentType' => Format::JSON
]),
new LogEntry('info', 'Request Body {body}', [
new LogEntry(LogLevel::INFO, self::REQUEST_BODY_FORMAT, [
'body' => '{"key":"value"}'
])
);
Expand All @@ -184,7 +199,7 @@ public function testLoggingRequestFormParams()
$requestParams = MockHelper::getClient()->validateParameters([
FormParam::init('key', 'value')
]);
$request = new Request('https://some/path', MockHelper::getClient(), $requestParams);
$request = new Request(self::TEST_URL, MockHelper::getClient(), $requestParams);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
null,
null,
Expand All @@ -195,12 +210,12 @@ public function testLoggingRequestFormParams()
));
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path',
'url' => self::TEST_URL,
'contentType' => null
]),
new LogEntry('info', 'Request Body {body}', [
new LogEntry(LogLevel::INFO, self::REQUEST_BODY_FORMAT, [
'body' => [
'key' => 'value'
]
Expand All @@ -216,7 +231,7 @@ public function testLoggingRequestHeaders()
HeaderParam::init('HeaderB', 'value B'),
HeaderParam::init('Expires', '2345ms')
]);
$request = new Request('https://some/path', MockHelper::getClient(), $requestParams);
$request = new Request(self::TEST_URL, MockHelper::getClient(), $requestParams);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
null,
null,
Expand All @@ -228,12 +243,12 @@ public function testLoggingRequestHeaders()
));
$apiLogger->logRequest($request);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Request {method} {url} {contentType}', [
new LogEntry(LogLevel::INFO, self::REQUEST_FORMAT, [
'method' => 'Get',
'url' => 'https://some/path',
'url' => self::TEST_URL,
'contentType' => 'my-content-type'
]),
new LogEntry('info', 'Request Headers {headers}', [
new LogEntry(LogLevel::INFO, self::REQUEST_HEADERS_FORMAT, [
'headers' => [
'Content-Type' => 'my-content-type',
'HeaderA' => '**Redacted**',
Expand All @@ -253,7 +268,7 @@ public function testLoggingResponseBody()
'key' => 'value'
]);
$response->setHeaders([
'content-type' => 'application/json',
'content-type' => Format::JSON,
'content-length' => '45'
]);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
Expand All @@ -264,12 +279,12 @@ public function testLoggingResponseBody()
));
$apiLogger->logResponse($response);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('info', 'Response {statusCode} {contentLength} {contentType}', [
new LogEntry(LogLevel::INFO, self::RESPONSE_FORMAT, [
'statusCode' => 200,
'contentLength' => '45',
'contentType' => 'application/json'
'contentType' => Format::JSON
]),
new LogEntry('info', 'Response Body {body}', [
new LogEntry(LogLevel::INFO, self::RESPONSE_BODY_FORMAT, [
'body' => '{"key":"value"}'
])
);
Expand All @@ -279,12 +294,7 @@ public function testLoggingResponseHeaders()
{
$response = new MockResponse();
$response->setStatusCode(400);
$response->setHeaders([
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
]);
$response->setHeaders(self::TEST_HEADERS);
$apiLogger = new ApiLogger(MockHelper::getLoggingConfiguration(
LogLevel::ERROR,
null,
Expand All @@ -296,12 +306,12 @@ public function testLoggingResponseHeaders()
));
$apiLogger->logResponse($response);
MockHelper::getMockLogger()->assertLastEntries(
new LogEntry('error', 'Response {statusCode} {contentLength} {contentType}', [
new LogEntry('error', self::RESPONSE_FORMAT, [
'statusCode' => 400,
'contentLength' => null,
'contentType' => 'my-content-type'
]),
new LogEntry('error', 'Response Headers {headers}', [
new LogEntry('error', self::RESPONSE_HEADERS_FORMAT, [
'headers' => [
'Content-Type' => 'my-content-type',
'HeaderA' => '**Redacted**',
Expand All @@ -315,31 +325,19 @@ public function testLoggingResponseHeaders()
public function testLoggableHeaders()
{
$responseConfig = MockHelper::getResponseLoggingConfiguration(false, true);
$headers = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$expectedHeaders = [
'Content-Type' => 'my-content-type',
'HeaderA' => '**Redacted**',
'HeaderB' => '**Redacted**',
'Expires' => '2345ms'
];
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders($headers, true));
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders(self::TEST_HEADERS, true));
}

public function testAllUnMaskedLoggableHeaders()
{
$responseConfig = MockHelper::getResponseLoggingConfiguration(false, true);
$headers = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$this->assertEquals($headers, $responseConfig->getLoggableHeaders($headers, false));
$this->assertEquals(self::TEST_HEADERS, $responseConfig->getLoggableHeaders(self::TEST_HEADERS, false));
}

public function testIncludedLoggableHeaders()
Expand All @@ -349,17 +347,11 @@ public function testIncludedLoggableHeaders()
true,
['HeaderB', 'Expires']
);
$headers = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$expectedHeaders = [
'HeaderB' => '**Redacted**',
'Expires' => '2345ms'
];
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders($headers, true));
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders(self::TEST_HEADERS, true));
}

public function testExcludedLoggableHeaders()
Expand All @@ -370,17 +362,11 @@ public function testExcludedLoggableHeaders()
[],
['HeaderB', 'Expires']
);
$headers = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$expectedHeaders = [
'HeaderA' => '**Redacted**',
'Content-Type' => 'my-content-type',
];
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders($headers, true));
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders(self::TEST_HEADERS, true));
}

public function testIncludeAndExcludeLoggableHeaders()
Expand All @@ -391,18 +377,12 @@ public function testIncludeAndExcludeLoggableHeaders()
['HEADERB', 'EXPIRES'],
['EXPIRES']
);
$headers = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$expectedHeaders = [
'HeaderB' => '**Redacted**',
'Expires' => '2345ms'
];
// If both include and exclude headers are provided then only includeHeaders will work
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders($headers, true));
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders(self::TEST_HEADERS, true));
}

public function testUnMaskedLoggableHeaders()
Expand All @@ -414,18 +394,12 @@ public function testUnMaskedLoggableHeaders()
[],
['HeaderB']
);
$headers = [
'Content-Type' => 'my-content-type',
'HeaderA' => 'value A',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$expectedHeaders = [
'Content-Type' => 'my-content-type',
'HeaderA' => '**Redacted**',
'HeaderB' => 'value B',
'Expires' => '2345ms'
];
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders($headers, true));
$this->assertEquals($expectedHeaders, $responseConfig->getLoggableHeaders(self::TEST_HEADERS, true));
}
}

0 comments on commit af1d4b9

Please sign in to comment.