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\Command\Interfaces\Console\TransformerInterface; |
7
|
|
|
use Tarsana\IO\Filesystem; |
8
|
|
|
use Tarsana\IO\Filesystem\Adapters\Memory; |
9
|
|
|
use Tarsana\IO\Resource\Buffer; |
10
|
|
|
|
11
|
|
|
class Transformer implements TransformerInterface { |
12
|
|
|
public function transform(string $text) : string |
13
|
|
|
{ |
14
|
|
|
return $text; |
15
|
|
|
} |
16
|
|
|
public function alias(string $name, string $value) : Transformer |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
return $this; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
class CommandTestCase extends TestCase { |
|
|
|
|
23
|
|
|
|
24
|
|
|
protected $fs; |
25
|
|
|
protected $cmd; |
26
|
|
|
protected $stdout; |
27
|
|
|
protected $stderr; |
28
|
|
|
|
29
|
|
|
public function setUp() { |
30
|
|
|
$adapter = new Memory; |
31
|
|
|
$adapter->mkdir('.', 0777, true); |
32
|
|
|
$this->fs = new Filesystem('.', $adapter); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function command( |
36
|
|
|
Command $command, array $args = [], |
37
|
|
|
array $options = [], bool $rawArgs = true |
38
|
|
|
) { |
39
|
|
|
$console = (new Console) |
|
|
|
|
40
|
|
|
->stdin(new Buffer) |
41
|
|
|
->stdout(new Buffer) |
42
|
|
|
->stderr(new Buffer) |
43
|
|
|
->outTransformer(new Transformer); |
44
|
|
|
|
45
|
|
|
$this->fs->dir($command->fs()->path(), true); |
46
|
|
|
|
47
|
|
|
$command->console($console) |
48
|
|
|
->fs($this->fs) |
49
|
|
|
->run($args, $options, $rawArgs); |
50
|
|
|
|
51
|
|
|
$this->cmd = $command; |
52
|
|
|
$this->stdout = $console->stdout()->read(); |
53
|
|
|
$this->stderr = $console->stderr()->read(); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function prints(string $text) { |
59
|
|
|
$this->assertTrue( |
60
|
|
|
false !== strpos($this->stdout, $text), |
61
|
|
|
"Failed asserting that '{$this->stdout}' Contains '{$text}'" |
62
|
|
|
); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function printsExactly(string $text) { |
67
|
|
|
$this->assertEquals($text, $this->stdout); |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function printsError(string $text) { |
72
|
|
|
$this->assertTrue( |
73
|
|
|
false !== strpos($this->stderr, $text), |
74
|
|
|
"Failed asserting that '{$this->stderr}' Contains '{$text}'" |
75
|
|
|
); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function argsEqual($args) { |
80
|
|
|
$this->assertEquals($args, $this->cmd->args()); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function optionsEqual($options) { |
85
|
|
|
$this->assertEquals($options, $this->cmd->options()); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.