]> BookStack Code Mirror - system-cli/blob - tests/TestCase.php
Added main-path restore command testing
[system-cli] / tests / TestCase.php
1 <?php
2
3 namespace Tests;
4
5 use Symfony\Component\Console\Application;
6 use Symfony\Component\Console\Tester\CommandTester;
7
8 class TestCase extends \PHPUnit\Framework\TestCase
9 {
10     protected function getTestDataDirectory(): string
11     {
12         return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'test-data';
13     }
14
15     protected function getEmptyTestDir(): string
16     {
17         $dir = $this->getTestDataDirectory() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(10));
18         mkdir($dir);
19         return $dir;
20     }
21
22     protected function deleteDirectory(string $dir): void
23     {
24         // For safety, only delete directories within our test data dir.
25         if (!str_starts_with($dir, $this->getTestDataDirectory())) {
26             return;
27         }
28
29         $files = array_diff(scandir($dir), ['.', '..']);
30
31         foreach ($files as $file) {
32             (is_dir("$dir/$file")) ? $this->deleteDirectory("$dir/$file") : unlink("$dir/$file");
33         }
34
35         rmdir($dir);
36     }
37
38     protected function getApp(): Application
39     {
40         return require dirname(__DIR__) . '/src/app.php';
41     }
42
43     protected function runCommand(string $command, array $args = [], array $inputs = []): CommandResult
44     {
45         $app = $this->getApp();
46         $command = $app->find($command);
47
48         $err = null;
49         $commandTester = new CommandTester($command);
50
51         if (!empty($inputs)) {
52             $commandTester->setInputs($inputs);
53         }
54
55         try {
56             $commandTester->execute($args);
57         } catch (\Exception $exception) {
58             $err = $exception;
59         }
60
61         return new CommandResult($commandTester, $err);
62     }
63 }