1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CaptainHook\App\Runner\Action\Cli\Command; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Mockery as AppMockery; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class FormatterTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
use AppMockery; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tests Formatter::format |
23
|
|
|
*/ |
24
|
|
|
public function testFormatArgumentPlaceholders(): void |
25
|
|
|
{ |
26
|
|
|
$args = ['foo' => 'bar']; |
27
|
|
|
$repo = $this->createRepositoryMock(); |
28
|
|
|
|
29
|
|
|
$formatter = new Formatter($repo, $args); |
30
|
|
|
$command = $formatter->format('cmd argument {$FOO}'); |
31
|
|
|
|
32
|
|
|
$this->assertEquals('cmd argument bar', $command); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tests Formatter::format |
37
|
|
|
*/ |
38
|
|
|
public function testFormatInvalidPlaceholderReplacedWithEmptyString(): void |
39
|
|
|
{ |
40
|
|
|
$args = []; |
41
|
|
|
$repo = $this->createRepositoryMock(); |
42
|
|
|
|
43
|
|
|
$formatter = new Formatter($repo, $args); |
44
|
|
|
$command = $formatter->format('cmd argument {$FOO}'); |
45
|
|
|
|
46
|
|
|
$this->assertEquals('cmd argument ', $command); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Tests Formatter::format |
51
|
|
|
*/ |
52
|
|
|
public function testCachedPlaceholder(): void |
53
|
|
|
{ |
54
|
|
|
$args = []; |
55
|
|
|
$repo = $this->createRepositoryMock(); |
56
|
|
|
$index = $this->createGitIndexOperator(['file1.php', 'file2.php']); |
57
|
|
|
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index); |
58
|
|
|
|
59
|
|
|
$formatter = new Formatter($repo, $args); |
60
|
|
|
$command1 = $formatter->format('cmd1 argument {$STAGED_FILES}'); |
61
|
|
|
$command2 = $formatter->format('cmd2 argument {$STAGED_FILES}'); |
62
|
|
|
|
63
|
|
|
$this->assertEquals('cmd1 argument file1.php file2.php', $command1); |
64
|
|
|
$this->assertEquals('cmd2 argument file1.php file2.php', $command2); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Tests Formatter::format |
70
|
|
|
*/ |
71
|
|
|
public function testComplexPlaceholder(): void |
72
|
|
|
{ |
73
|
|
|
$args = []; |
74
|
|
|
$repo = $this->createRepositoryMock(); |
75
|
|
|
$index = $this->createGitIndexOperator(['file1.php', 'file2.php', 'README.md']); |
76
|
|
|
$repo->expects($this->exactly(2))->method('getIndexOperator')->willReturn($index); |
77
|
|
|
$index->expects($this->exactly(2))->method('getStagedFilesOfType')->willReturn(['file1.php', 'file2.php']); |
78
|
|
|
|
79
|
|
|
$formatter = new Formatter($repo, $args); |
80
|
|
|
$command1 = $formatter->format('cmd1 argument {$STAGED_FILES|of-type:php|separated-by:,}'); |
81
|
|
|
$command2 = $formatter->format('cmd2 argument {$STAGED_FILES|of-type:php}'); |
82
|
|
|
|
83
|
|
|
$this->assertEquals('cmd1 argument file1.php,file2.php', $command1); |
84
|
|
|
$this->assertEquals('cmd2 argument file1.php file2.php', $command2); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|