Removed method isTestingOnOcisOrReva, and refactored its use

This commit is contained in:
Prarup Gurung
2023-03-09 14:25:15 +05:45
parent d9092d7e17
commit fab9bfc734
11 changed files with 147 additions and 1063 deletions
@@ -65,86 +65,9 @@ class LoggingContext implements Context {
int $ignoredLines = 0,
?TableNode $expectedLogEntries = null
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we don't interact with the log file on reva or OCIS
// So skip processing this test step.
return;
}
$ignoredLines = (int) $ignoredLines;
//-1 because getRows gives also the header
$linesToRead = \count($expectedLogEntries->getRows()) - 1 + $ignoredLines;
$logLines = LoggingHelper::getLogFileContent(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->featureContext->getStepLineRef(),
$linesToRead
);
$lineNo = 0;
foreach ($expectedLogEntries as $expectedLogEntry) {
$logEntry = \json_decode($logLines[$lineNo], true);
if ($logEntry === null) {
throw new Exception("the log line :\n{$logLines[$lineNo]} is not valid JSON");
}
foreach (\array_keys($expectedLogEntry) as $attribute) {
if ($comparingMode === 'matching') {
$expectedLogEntry[$attribute]
= $this->featureContext->substituteInLineCodes(
$expectedLogEntry[$attribute],
null,
['preg_quote' => ['/']]
);
} else {
$expectedLogEntry[$attribute]
= $this->featureContext->substituteInLineCodes(
$expectedLogEntry[$attribute]
);
}
if ($expectedLogEntry[$attribute] !== "") {
Assert::assertArrayHasKey(
$attribute,
$logEntry,
"could not find attribute: '$attribute' in log entry: '{$logLines[$lineNo]}'"
);
$message = "log entry:\n{$logLines[$lineNo]}\n";
if (!\is_string($logEntry[$attribute])) {
$logEntry[$attribute] = \json_encode(
$logEntry[$attribute],
JSON_UNESCAPED_SLASHES
);
}
if ($comparingMode === 'with') {
Assert::assertEquals(
$expectedLogEntry[$attribute],
$logEntry[$attribute],
$message
);
} elseif ($comparingMode === 'containing') {
Assert::assertStringContainsString(
$expectedLogEntry[$attribute],
$logEntry[$attribute],
$message
);
} elseif ($comparingMode === 'matching') {
Assert::assertMatchesRegularExpression(
$expectedLogEntry[$attribute],
$logEntry[$attribute],
$message
);
} else {
throw new \InvalidArgumentException(
"$comparingMode is not a valid mode"
);
}
}
}
$lineNo++;
if (($lineNo + $ignoredLines) >= $linesToRead) {
break;
}
}
// Currently we don't interact with the log file on reva or OCIS
// So skip processing this test step.
return;
}
/**
@@ -279,90 +202,9 @@ class LoggingContext implements Context {
TableNode $expectedLogEntries,
bool $regexCompare = false
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we don't interact with the log file on reva or OCIS
// So skip processing this test step.
return;
}
$logLines = LoggingHelper::getLogFileContent(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->featureContext->getStepLineRef()
);
$expectedLogEntries = $expectedLogEntries->getHash();
foreach ($logLines as $logLine) {
$logEntry = \json_decode($logLine, true);
if ($logEntry === null) {
throw new Exception("the log line :\n{$logLine} is not valid JSON");
}
//reindex the array, we might have deleted entries
$expectedLogEntries = \array_values($expectedLogEntries);
for ($entryNo = 0; $entryNo < \count($expectedLogEntries); $entryNo++) {
$count = 0;
$expectedLogEntry = $expectedLogEntries[$entryNo];
$foundLine = true;
foreach (\array_keys($expectedLogEntry) as $attribute) {
if ($expectedLogEntry[$attribute] === "") {
//don't check empty table entries
continue;
}
if (!\array_key_exists($attribute, $logEntry)) {
//this line does not have the attribute we are looking for
$foundLine = false;
break;
}
if (!\is_string($logEntry[$attribute])) {
$logEntry[$attribute] = \json_encode(
$logEntry[$attribute],
JSON_UNESCAPED_SLASHES
);
}
if ($regexCompare === true) {
$expectedLogEntry[$attribute]
= $this->featureContext->substituteInLineCodes(
$expectedLogEntry[$attribute],
null,
['preg_quote' => ['/']]
);
$matchAttribute = \preg_match(
$expectedLogEntry[$attribute],
$logEntry[$attribute]
);
} else {
$expectedLogEntry[$attribute]
= $this->featureContext->substituteInLineCodes(
$expectedLogEntry[$attribute]
);
$matchAttribute
= ($expectedLogEntry[$attribute] === $logEntry[$attribute]);
}
if (!$matchAttribute) {
$foundLine = false;
break;
}
if ($matchAttribute and !$shouldOrNot) {
$count += 1;
Assert::assertNotEquals(
$count,
\count($expectedLogEntry),
"The entry matches"
);
}
}
if ($foundLine === true) {
unset($expectedLogEntries[$entryNo]);
}
}
}
$notFoundLines = \print_r($expectedLogEntries, true);
if ($shouldOrNot) {
Assert::assertEmpty(
$expectedLogEntries,
"could not find these expected line(s):\n $notFoundLines"
);
}
// Currently we don't interact with the log file on reva or OCIS
// So skip processing this test step.
return;
}
/**
@@ -387,50 +229,9 @@ class LoggingContext implements Context {
$withOrContaining,
TableNode $logEntriesExpectedNotToExist
):void {
if (OcisHelper::isTestingOnOcisOrReva()) {
// Currently we don't interact with the log file on reva or OCIS
// So skip processing this test step.
return;
}
$logLines = LoggingHelper::getLogFileContent(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->featureContext->getStepLineRef()
);
foreach ($logLines as $logLine) {
$logEntry = \json_decode($logLine, true);
if ($logEntry === null) {
throw new Exception("the log line :\n$logLine is not valid JSON");
}
foreach ($logEntriesExpectedNotToExist as $logEntryExpectedNotToExist) {
$match = true; // start by assuming the worst, we match the unwanted log entry
foreach (\array_keys($logEntryExpectedNotToExist) as $attribute) {
$logEntryExpectedNotToExist[$attribute]
= $this->featureContext->substituteInLineCodes(
$logEntryExpectedNotToExist[$attribute]
);
if (isset($logEntry[$attribute]) && ($logEntryExpectedNotToExist[$attribute] !== "")) {
if ($withOrContaining === 'with') {
$match = ($logEntryExpectedNotToExist[$attribute] === $logEntry[$attribute]);
} else {
$match = (\strpos($logEntry[$attribute], $logEntryExpectedNotToExist[$attribute]) !== false);
}
}
if (!isset($logEntry[$attribute])) {
$match = false;
}
if (!$match) {
break;
}
}
}
Assert::assertFalse(
$match,
"found a log entry that should not be there\n$logLine\n"
);
}
// Currently we don't interact with the log file on reva or OCIS
// So skip processing this test step.
return;
}
/**