Passed
Push — main ( 241083...5efb85 )
by Sebastian
04:13
created

ThatIsTest::testPreCommitRestriction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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\Hook\Condition\FileChanged;
13
14
use PHPUnit\Framework\TestCase;
15
use CaptainHook\App\Mockery as AppMockery;
16
use CaptainHook\App\Console\IO\Mockery as IOMockery;
17
18
class ThatIsTest extends TestCase
19
{
20
    use AppMockery;
21
    use IOMockery;
22
23
    public function testPreCommitRestriction(): void
24
    {
25
        $this->assertTrue(ThatIs::getRestriction()->isApplicableFor('post-checkout'));
26
        $this->assertFalse(ThatIs::getRestriction()->isApplicableFor('pre-commit'));
27
    }
28
29
    public function testChangedTrueType(): void
30
    {
31
        $io = $this->createIOMock();
32
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
33
        $operator = $this->createGitDiffOperator(['foo.php', 'bar.php']);
34
        $repo     = $this->createRepositoryMock('');
35
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
36
37
        $thatIs = new ThatIs(['ofType' => 'php']);
38
        $this->assertTrue($thatIs->isTrue($io, $repo));
39
    }
40
41
    public function testStagedTrueMultipleType(): void
42
    {
43
        $io = $this->createIOMock();
44
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
45
        $operator = $this->createGitDiffOperator(['foo.php', 'bar.php']);
46
        $repo     = $this->createRepositoryMock('');
47
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
48
49
        $thatIs = new ThatIs(['ofType' => ['php', 'js']]);
50
        $this->assertTrue($thatIs->isTrue($io, $repo));
51
    }
52
53
    public function testStagedFalseMultipleType(): void
54
    {
55
        $io = $this->createIOMock();
56
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
57
        $operator = $this->createGitDiffOperator(['foo.php', 'bar.php']);
58
        $repo     = $this->createRepositoryMock('');
59
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
60
61
        $thatIs = new ThatIs(['ofType' => ['ts', 'js']]);
62
        $this->assertFalse($thatIs->isTrue($io, $repo));
63
    }
64
65
    public function testStagedTrueDirectory(): void
66
    {
67
        $io = $this->createIOMock();
68
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
69
        $operator = $this->createGitDiffOperator(['foo/foo.php', 'bar/bar.js', 'fiz/baz.txt']);
70
        $repo     = $this->createRepositoryMock('');
71
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
72
73
        $thatIs = new ThatIs(['inDirectory' => 'bar/']);
74
        $this->assertTrue($thatIs->isTrue($io, $repo));
75
    }
76
77
    public function testStagedFalsePartialDirectory(): void
78
    {
79
        $io = $this->createIOMock();
80
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
81
        $operator = $this->createGitDiffOperator(['foo/foo.php', 'foo/bar/bar.js', 'fiz/baz.txt']);
82
        $repo     = $this->createRepositoryMock('');
83
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
84
85
        $thatIs = new ThatIs(['inDirectory' => 'bar/']);
86
        $this->assertFalse($thatIs->isTrue($io, $repo));
87
    }
88
89
    public function testStagedTrueMultipleDirectory(): void
90
    {
91
        $io = $this->createIOMock();
92
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
93
        $operator = $this->createGitDiffOperator(['foo/foo.php', 'foo/bar/bar.js', 'fiz/baz.txt']);
94
        $repo     = $this->createRepositoryMock('');
95
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
96
97
        $thatIs = new ThatIs(['inDirectory' => ['foo/bar/', 'baz/']]);
98
        $this->assertTrue($thatIs->isTrue($io, $repo));
99
    }
100
101
    public function testStagedFalseMultipleDirectory(): void
102
    {
103
        $io = $this->createIOMock();
104
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
105
        $operator = $this->createGitDiffOperator(['foo/foo.php', 'foo/bar/bar.js', 'fiz/baz.txt']);
106
        $repo     = $this->createRepositoryMock('');
107
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
108
109
        $thatIs = new ThatIs(['inDirectory' => ['foobar/', 'baz/'], 'diffFilter' => ['A', 'C']]);
110
        $this->assertFalse($thatIs->isTrue($io, $repo));
111
    }
112
113
    public function testStagedFalseDirectoryAndType(): void
114
    {
115
        $io = $this->createIOMock();
116
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
117
        $operator = $this->createGitDiffOperator(['foo/foo.php', 'bar/bar.js']);
118
        $repo     = $this->createRepositoryMock('');
119
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
120
121
        $thatIs = new ThatIs(['inDirectory' => 'bar/', 'ofType' => 'php']);
122
        $this->assertFalse($thatIs->isTrue($io, $repo));
123
    }
124
125
    public function testStagedFalse(): void
126
    {
127
        $io = $this->createIOMock();
128
        $io->expects($this->exactly(2))->method('getArgument')->willReturn('');
129
        $operator = $this->createGitDiffOperator(['foo.php']);
130
        $repo     = $this->createRepositoryMock('');
131
        $repo->expects($this->once())->method('getDiffOperator')->willReturn($operator);
132
133
        $thatIs = new ThatIs(['ofType' => 'js']);
134
        $this->assertFalse($thatIs->isTrue($io, $repo));
135
    }
136
}
137