]> BookStack Code Mirror - system-cli/blob - tests/CommandResult.php
Removed old imports
[system-cli] / tests / CommandResult.php
1 <?php
2
3 namespace Tests;
4
5 use PHPUnit\Framework\Assert;
6 use Symfony\Component\Console\Tester\CommandTester;
7
8 class CommandResult
9 {
10
11     public function __construct(
12         protected CommandTester $tester,
13         protected ?\Exception $error
14     ) { }
15
16     public function getStderr(): string
17     {
18         return $this->error?->getMessage() ?? '';
19     }
20
21     public function assertSuccessfulExit(): void
22     {
23         try {
24             $statusCode = $this->tester->getStatusCode();
25         } catch (\Exception $exception) {
26             $statusCode = 1;
27         }
28
29         Assert::assertEquals(0, $statusCode);
30     }
31
32     public function dumpError(): void
33     {
34         if ($this->error) {
35             echo $this->error->getMessage() . "\n" .
36                 $this->error->getTraceAsString();
37         }
38     }
39
40     public function assertErrorExit(): void
41     {
42         Assert::assertTrue($this->error !== null);
43     }
44
45     public function assertStdoutContains(string $needle): void
46     {
47         Assert::assertStringContainsString($needle, $this->tester->getDisplay());
48     }
49
50     public function assertStderrContains(string $needle): void
51     {
52         Assert::assertStringContainsString($needle, $this->getStderr());
53     }
54
55
56 }