FormatterTest::testCachedPlaceholder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7998
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\Console\IO\Mockery as IOMockery;
15
use CaptainHook\App\Config\Mockery as ConfigMockery;
16
use CaptainHook\App\Mockery as AppMockery;
17
use PHPUnit\Framework\TestCase;
18
19
class FormatterTest extends TestCase
20
{
21
    use IOMockery;
22
    use AppMockery;
23
    use ConfigMockery;
24
25
    public function testFormatArgumentPlaceholders(): void
26
    {
27
        $config = $this->createConfigMock();
28
        $repo   = $this->createRepositoryMock();
29
        $io     = $this->createIOMock();
30
        $io->method('getArgument')->with('message-file')->willReturn('bar');
31
32
        $formatter = new Formatter($io, $config, $repo);
33
        $command   = $formatter->format('cmd argument {$FILE}');
34
35
        $this->assertEquals('cmd argument bar', $command);
36
    }
37
38
    public function testFormatInvalidPlaceholderReplacedWithEmptyString(): void
39
    {
40
        $config = $this->createConfigMock();
41
        $repo   = $this->createRepositoryMock();
42
        $io     = $this->createIOMock();
43
        $io->method('getArguments')->willReturn([]);
44
45
        $formatter = new Formatter($io, $config, $repo);
46
        $command   = $formatter->format('cmd argument {$FOO}');
47
48
        $this->assertEquals('cmd argument ', $command);
49
    }
50
51
    public function testCachedPlaceholder(): void
52
    {
53
        $io     = $this->createIOMock();
54
        $config = $this->createConfigMock();
55
        $repo   = $this->createRepositoryMock();
56
        $index  = $this->createGitIndexOperator(['foo/file1.php', 'bar/file2.php', 'baz/file3.php']);
57
        $io->method('getArguments')->willReturn([]);
58
        $config->method('getGitDirectory')->willReturn('./');
59
        $repo->expects($this->exactly(3))->method('getIndexOperator')->willReturn($index);
60
61
        $formatter = new Formatter($io, $config, $repo);
62
        $command1  = $formatter->format('cmd1 argument {$STAGED_FILES|in-dir:foo} {$STAGED_FILES|in-dir:baz}');
63
        $command2  = $formatter->format('cmd2 argument {$STAGED_FILES}');
64
        $command3  = $formatter->format('cmd3 argument {$STAGED_FILES}');
65
66
        $this->assertEquals('cmd1 argument foo/file1.php baz/file3.php', $command1);
67
        $this->assertEquals('cmd2 argument foo/file1.php bar/file2.php baz/file3.php', $command2);
68
        $this->assertEquals('cmd3 argument foo/file1.php bar/file2.php baz/file3.php', $command3);
69
    }
70
71
    public function testComplexPlaceholder(): void
72
    {
73
        $io     = $this->createIOMock();
74
        $config = $this->createConfigMock();
75
        $repo   = $this->createRepositoryMock();
76
        $index  = $this->createGitIndexOperator(['file1.php', 'file2.php', 'README.md']);
77
        $io->method('getArguments')->willReturn([]);
78
        $repo->expects($this->exactly(2))->method('getIndexOperator')->willReturn($index);
79
        $index->expects($this->exactly(2))->method('getStagedFilesOfType')->willReturn(['file1.php', 'file2.php']);
80
81
        $formatter = new Formatter($io, $config, $repo);
82
        $command1  = $formatter->format('cmd1 argument {$STAGED_FILES|of-type:php|separated-by:,}');
83
        $command2  = $formatter->format('cmd2 argument {$STAGED_FILES|of-type:php}');
84
85
        $this->assertEquals('cmd1 argument file1.php,file2.php', $command1);
86
        $this->assertEquals('cmd2 argument file1.php file2.php', $command2);
87
    }
88
}
89