1 | <?php namespace Tarsana\Tester; |
||
2 | |||
3 | use PHPUnit\Framework\TestCase; |
||
4 | use Tarsana\Command\Command; |
||
5 | use Tarsana\Command\Console\Console; |
||
6 | use Tarsana\IO\Filesystem; |
||
7 | use Tarsana\IO\Filesystem\Adapters\Memory; |
||
8 | use Tarsana\IO\Resource\Buffer; |
||
9 | use Tarsana\Tester\Mocks\Transformer; |
||
10 | |||
11 | abstract class CommandTestCase extends TestCase { |
||
12 | |||
13 | protected $fs; |
||
14 | protected $cmd; |
||
15 | protected $stdin; |
||
16 | protected $stdout; |
||
17 | protected $stderr; |
||
18 | |||
19 | public function setUp(): void |
||
20 | { |
||
21 | $adapter = new Memory; |
||
22 | $adapter->mkdir('.', 0777, true); |
||
23 | $this->fs = new Filesystem('.', $adapter); |
||
24 | } |
||
25 | |||
26 | protected function command( |
||
27 | Command $command, array $args = [], |
||
28 | array $options = [], bool $rawArgs = true |
||
29 | ) { |
||
30 | $stdin = new Buffer; |
||
31 | $stdin->write($this->stdin); |
||
32 | $console = (new Console) |
||
33 | ->stdin($stdin) |
||
34 | ->stdout(new Buffer) |
||
35 | ->stderr(new Buffer) |
||
36 | ->outTransformer(new Transformer); |
||
37 | |||
38 | $this->fs->dir($command->fs()->path(), true); |
||
39 | |||
40 | $command->console($console) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
41 | ->fs($this->fs) |
||
42 | ->run($args, $options, $rawArgs); |
||
43 | |||
44 | $this->cmd = $command; |
||
45 | $this->stdout = $console->stdout()->read(); |
||
46 | $this->stderr = $console->stderr()->read(); |
||
47 | |||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | protected function prints(string $text) |
||
52 | { |
||
53 | $this->assertTrue( |
||
54 | false !== strpos($this->stdout, $text), |
||
55 | "Failed asserting that '{$this->stdout}' Contains '{$text}'" |
||
56 | ); |
||
57 | return $this; |
||
58 | } |
||
59 | |||
60 | protected function printsExactly(string $text) |
||
61 | { |
||
62 | $this->assertEquals($text, $this->stdout); |
||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | protected function printsError(string $text) |
||
67 | { |
||
68 | $this->assertTrue( |
||
69 | false !== strpos($this->stderr, $text), |
||
70 | "Failed asserting that '{$this->stderr}' Contains '{$text}'" |
||
71 | ); |
||
72 | return $this; |
||
73 | } |
||
74 | |||
75 | protected function argsEqual($args) |
||
76 | { |
||
77 | $this->assertEquals($args, $this->cmd->args()); |
||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | protected function optionsEqual(array $options) |
||
82 | { |
||
83 | $this->assertEquals($options, $this->cmd->options()); |
||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | protected function withStdin(string $text) { |
||
88 | $this->stdin = $text; |
||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | protected function havingFile(string $path, string $content = '') |
||
93 | { |
||
94 | $this->fs->file($path, true)->content($content); |
||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | protected function havingDir(string $path) |
||
99 | { |
||
100 | $this->fs->dir($path, true); |
||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | } |
||
105 |