Skip to content

Commit 1212b2f

Browse files
committed
test(behat): add stable nextcloud log reader step
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 882fd8a commit 1212b2f

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

tests/integration/features/bootstrap/MockWebServerContext.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ public function readTheLastRequestBodyFromMockWebServer(string $serverName): voi
5858
}
5959
}
6060

61+
/**
62+
* @When /^read the last Nextcloud log entry containing "([^"]*)"$/
63+
*/
64+
public function readTheLastNextcloudLogEntryContaining(string $needle): void {
65+
$logPath = static::findParentDirContainingFile('console.php') . '/data/nextcloud.log';
66+
67+
$deadline = microtime(true) + 4;
68+
do {
69+
$entry = $this->findLastLogEntryContaining($logPath, $needle);
70+
if ($entry !== null) {
71+
self::$commandOutput = $entry;
72+
return;
73+
}
74+
75+
usleep(200000);
76+
} while (microtime(true) < $deadline);
77+
78+
throw new RuntimeException('Nextcloud log does not contain: ' . $needle);
79+
}
80+
6181
#[AfterScenario()]
6282
public function stopMockWebServers(): void {
6383
foreach ($this->mockServers as $server) {
@@ -95,4 +115,23 @@ private function getLastRequest(string $serverName): RequestInfo {
95115

96116
return $request;
97117
}
118+
119+
private function findLastLogEntryContaining(string $logPath, string $needle): ?string {
120+
if (!is_file($logPath)) {
121+
throw new RuntimeException('Nextcloud log file not found at ' . $logPath);
122+
}
123+
124+
$lines = file($logPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
125+
if (!is_array($lines)) {
126+
throw new RuntimeException('Unable to read Nextcloud log file at ' . $logPath);
127+
}
128+
129+
for ($index = count($lines) - 1; $index >= 0; $index--) {
130+
if (str_contains($lines[$index], $needle)) {
131+
return $lines[$index];
132+
}
133+
}
134+
135+
return null;
136+
}
98137
}

0 commit comments

Comments
 (0)