1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Console\Command; |
4
|
|
|
|
5
|
|
|
use Psr\Log\NullLogger; |
6
|
|
|
use Symfony\Component\Console\Application; |
7
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
8
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
9
|
|
|
use uuf6429\ElderBrother\Config\Config; |
10
|
|
|
use uuf6429\ElderBrother\Console\Command; |
11
|
|
|
use uuf6429\ElderBrother\Action; |
12
|
|
|
use uuf6429\ElderBrother\Change; |
13
|
|
|
use Symfony\Component\Finder\SplFileInfo as SfyFileInfo; |
14
|
|
|
|
15
|
|
|
class RunTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
const CUSTOM_EVENT = 'custom:test'; |
18
|
|
|
|
19
|
|
|
public function testRunWithoutActions() |
20
|
|
|
{ |
21
|
|
|
$inp = $this->getInput(); |
22
|
|
|
$out = new BufferedOutput(); |
23
|
|
|
$cmd = $this->getRunCommand([]); |
24
|
|
|
|
25
|
|
|
$exit = $cmd->run($inp, $out); |
26
|
|
|
$this->assertSame(0, $exit, 'Command output: ' . $out->fetch()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGoodCode() |
30
|
|
|
{ |
31
|
|
|
$testFile = tempnam(sys_get_temp_dir(), 'test'); |
32
|
|
|
$this->assertNotFalse(file_put_contents($testFile, '<?php echo "Hi!";')); |
33
|
|
|
|
34
|
|
|
$inp = $this->getInput(); |
35
|
|
|
$out = new BufferedOutput(); |
36
|
|
|
$cmd = $this->getRunCommand( |
37
|
|
|
[ |
38
|
|
|
new Action\PhpLinter( |
39
|
|
|
new Change\FileList( |
40
|
|
|
'good-code-test', |
41
|
|
|
function () use ($testFile) { |
42
|
|
|
return [new SfyFileInfo($testFile, dirname($testFile), basename($testFile))]; |
43
|
|
|
} |
44
|
|
|
) |
45
|
|
|
) |
46
|
|
|
] |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$exit = $cmd->run($inp, $out); |
50
|
|
|
$this->assertSame(0, $exit, 'Command output: ' . $out->fetch()); |
51
|
|
|
|
52
|
|
|
$this->assertNotFalse(unlink($testFile)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testBadCode() |
56
|
|
|
{ |
57
|
|
|
$testFile = tempnam(sys_get_temp_dir(), 'test'); |
58
|
|
|
$this->assertNotFalse(file_put_contents($testFile, '<?php 3ch"o')); |
59
|
|
|
|
60
|
|
|
$inp = $this->getInput(); |
61
|
|
|
$out = new BufferedOutput(); |
62
|
|
|
$cmd = $this->getRunCommand( |
63
|
|
|
[ |
64
|
|
|
new Action\PhpLinter( |
65
|
|
|
new Change\FileList( |
66
|
|
|
'bad-code-test', |
67
|
|
|
function () use ($testFile) { |
68
|
|
|
return [new SfyFileInfo($testFile, dirname($testFile), basename($testFile))]; |
69
|
|
|
} |
70
|
|
|
) |
71
|
|
|
) |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$exit = $cmd->run($inp, $out); |
76
|
|
|
$this->assertSame(1, $exit, 'Command output: ' . $out->fetch()); |
77
|
|
|
|
78
|
|
|
$this->assertNotFalse(unlink($testFile)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Action\ActionAbstract[] $actions |
83
|
|
|
* @return Command\Run |
84
|
|
|
*/ |
85
|
|
|
protected function getRunCommand($actions) |
86
|
|
|
{ |
87
|
|
|
$cfg = new Config(); |
88
|
|
|
$log = new NullLogger(); |
89
|
|
|
$cfg->loadFromArray([self::CUSTOM_EVENT => $actions], $log); |
90
|
|
|
$cmd = new Command\Run(); |
91
|
|
|
$cmd->setConfig($cfg); |
92
|
|
|
$cmd->setLogger($log); |
93
|
|
|
$cmd->setApplication(new Application()); |
94
|
|
|
return $cmd; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return ArgvInput |
99
|
|
|
*/ |
100
|
|
|
protected function getInput() |
101
|
|
|
{ |
102
|
|
|
return new ArgvInput(['run', '-e', self::CUSTOM_EVENT, '--no-progress', '--no-ansi']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|