5 use Symfony\Component\Console\Application;
6 use Symfony\Component\Console\Tester\CommandTester;
8 class TestCase extends \PHPUnit\Framework\TestCase
11 * Reset the current working directory to the application directory.
13 protected function resetCurrentDirectory(): void
15 chdir(dirname(__DIR__));
18 protected function getTestDataDirectory(): string
20 return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'test-data';
23 protected function getEmptyTestDir(): string
25 $dir = $this->getTestDataDirectory() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(10));
30 protected function deleteDirectory(string $dir): void
32 // For safety, only delete directories within our test data dir.
33 if (!str_starts_with($dir, $this->getTestDataDirectory())) {
37 $files = array_diff(scandir($dir), ['.', '..']);
39 foreach ($files as $file) {
40 (is_dir("$dir/$file")) ? $this->deleteDirectory("$dir/$file") : unlink("$dir/$file");
46 protected function getApp(): Application
48 return require dirname(__DIR__) . '/src/app.php';
51 protected function runCommand(string $command, array $args = [], array $inputs = [], array $env = []): CommandResult
53 $app = $this->getApp();
54 $command = $app->find($command);
57 $commandTester = new CommandTester($command);
59 if (!empty($inputs)) {
60 $commandTester->setInputs($inputs);
63 foreach ($env as $name => $value) {
64 $_SERVER[$name] = $value;
68 $commandTester->execute($args);
69 } catch (\Exception $exception) {
73 foreach ($env as $name => $value) {
74 unset($_SERVER[$name]);
77 return new CommandResult($commandTester, $err);
81 * Create a clone of a BookStack install folder, then pass that path
82 * and run the given callback.
83 * Destroys the folder afterwards.
85 protected function withOwnBookStackFolder(callable $callback)
87 $path = '/var/www/bookstack-' . md5(random_bytes(5));
88 exec("cp -r /var/www/bookstack $path");