5 use Symfony\Component\Console\Application;
6 use Symfony\Component\Console\Tester\CommandTester;
8 class TestCase extends \PHPUnit\Framework\TestCase
10 protected function getTestDataDirectory(): string
12 return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'test-data';
15 protected function getEmptyTestDir(): string
17 $dir = $this->getTestDataDirectory() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(10));
22 protected function deleteDirectory(string $dir): void
24 // For safety, only delete directories within our test data dir.
25 if (!str_starts_with($dir, $this->getTestDataDirectory())) {
29 $files = array_diff(scandir($dir), ['.', '..']);
31 foreach ($files as $file) {
32 (is_dir("$dir/$file")) ? $this->deleteDirectory("$dir/$file") : unlink("$dir/$file");
38 protected function getApp(): Application
40 return require dirname(__DIR__) . '/src/app.php';
43 protected function runCommand(string $command, array $args = [], array $inputs = []): CommandResult
45 $app = $this->getApp();
46 $command = $app->find($command);
49 $commandTester = new CommandTester($command);
51 if (!empty($inputs)) {
52 $commandTester->setInputs($inputs);
56 $commandTester->execute($args);
57 } catch (\Exception $exception) {
61 return new CommandResult($commandTester, $err);