getTestDataDirectory() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(10)); mkdir($dir); return $dir; } protected function deleteDirectory(string $dir): void { // For safety, only delete directories within our test data dir. if (!str_starts_with($dir, $this->getTestDataDirectory())) { return; } $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { (is_dir("$dir/$file")) ? $this->deleteDirectory("$dir/$file") : unlink("$dir/$file"); } rmdir($dir); } protected function getApp(): Application { return require dirname(__DIR__) . '/src/app.php'; } protected function runCommand(string $command, array $args = [], array $inputs = [], array $env = []): CommandResult { $app = $this->getApp(); $command = $app->find($command); $err = null; $commandTester = new CommandTester($command); if (!empty($inputs)) { $commandTester->setInputs($inputs); } foreach ($env as $name => $value) { $_SERVER[$name] = $value; } try { $commandTester->execute($args); } catch (\Exception $exception) { $err = $exception; } foreach ($env as $name => $value) { unset($_SERVER[$name]); } return new CommandResult($commandTester, $err); } /** * Create a clone of a BookStack install folder, then pass that path * and run the given callback. * Destroys the folder afterwards. */ protected function withOwnBookStackFolder(callable $callback) { $path = '/var/www/bookstack-' . md5(random_bytes(5)); exec("cp -r /var/www/bookstack $path"); chdir($path); $callback($path); exec("rm -rf $path"); } }