IsNotEmptyTest::testConfigWithGlobs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 27
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\Hook\File\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO\NullIO;
16
use CaptainHook\App\Mockery as GitMockery;
17
use CaptainHook\App\Console\IO\Mockery as IOMockery;
18
use Exception;
19
use PHPUnit\Framework\TestCase;
20
21
class IsNotEmptyTest extends TestCase
22
{
23
    use GitMockery;
24
    use IOMockery;
25
26
    public function testRestrictionValid(): void
27
    {
28
        $restriction = IsNotEmpty::getRestriction();
29
        $this->assertTrue($restriction->isApplicableFor('pre-commit'));
30
    }
31
32
    public function testRestrictionInvalid(): void
33
    {
34
        $restriction = IsNotEmpty::getRestriction();
35
        $this->assertFalse($restriction->isApplicableFor('pre-push'));
36
    }
37
38
    public function testCommitNotEmptyFile(): void
39
    {
40
41
        $io     = new NullIO();
42
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
43
        $action = new Config\Action(
44
            IsNotEmpty::class,
45
            [
46
                'files' => [
47
                    CH_PATH_FILES . '/storage/test.json'
48
                ]
49
            ]
50
        );
51
52
        $stagedFiles = [CH_PATH_FILES . '/storage/test.json'];
53
        $repo        = $this->createRepositoryMock();
54
        $repo->method('getIndexOperator')->willReturn($this->createGitIndexOperator($stagedFiles));
55
56
        $isNotEmpty = new IsNotEmpty();
57
        $isNotEmpty->execute($config, $io, $repo, $action);
58
59
        // no error should happen
60
        $this->assertTrue(true);
61
    }
62
63
    public function testConfigWithGlobs(): void
64
    {
65
        $io     = $this->createIOMock();
66
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
67
        $action = new Config\Action(
68
            IsNotEmpty::class,
69
            [
70
                'files' => [
71
                    CH_PATH_FILES . '/storage/*.txt',
72
                    CH_PATH_FILES . '/storage/*.json',
73
                ]
74
            ]
75
        );
76
77
        // with this configuration the Captain should find 4 files for 2 patterns
78
        $io->expects($this->atLeast(1))->method('write');
79
80
        // two of those files should be in the commit
81
        $stagedFiles = [CH_PATH_FILES . '/storage/regextest1.txt', CH_PATH_FILES . '/storage/test.json'];
82
        $repo        = $this->createRepositoryMock();
83
        $repo->method('getIndexOperator')->willReturn($this->createGitIndexOperator($stagedFiles));
84
85
        $isNotEmpty = new IsNotEmpty();
86
        $isNotEmpty->execute($config, $io, $repo, $action);
87
88
        // no error should happen
89
        $this->assertTrue(true);
90
    }
91
92
    public function testFailCommitEmptyFile(): void
93
    {
94
        $this->expectException(Exception::class);
95
96
        $io     = new NullIO();
97
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
98
        $action = new Config\Action(
99
            IsNotEmpty::class,
100
            [
101
                'files' => [
102
                    CH_PATH_FILES . '/storage/empty.log',
103
                ]
104
            ]
105
        );
106
107
        $stagedFiles = [CH_PATH_FILES . '/storage/empty.log'];
108
        $repo        = $this->createRepositoryMock();
109
        $repo->method('getIndexOperator')->willReturn($this->createGitIndexOperator($stagedFiles));
110
111
        $isNotEmpty = new IsNotEmpty();
112
        $isNotEmpty->execute($config, $io, $repo, $action);
113
    }
114
}
115