Passed
Pull Request — master (#103)
by Sebastian
02:29
created

IsNotEmptyTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 70
c 1
b 0
f 0
dl 0
loc 167
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testFile() 0 19 1
A testEmptyDirectoryFail() 0 24 2
A testDirectory() 0 19 1
A testGlob() 0 23 1
A testFileEmptyFail() 0 18 1
A testFileDoesNotExistFail() 0 18 1
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 PHPUnit\Framework\TestCase;
19
20
class IsNotEmptyTest extends TestCase
21
{
22
    use GitMockery;
23
    use IOMockery;
24
25
    /**
26
     * Tests IsNotEmpty::execute
27
     *
28
     * @throws \Exception
29
     */
30
    public function testFile(): void
31
    {
32
        $io     = new NullIO();
33
        $repo   = $this->createRepositoryMock();
34
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
35
        $action = new Config\Action(
36
            IsNotEmpty::class,
37
            [
38
                'files' => [
39
                    CH_PATH_FILES . '/storage/test.json'
40
                ]
41
            ]
42
        );
43
44
        $isNotEmpty = new IsNotEmpty();
45
        $isNotEmpty->execute($config, $io, $repo, $action);
46
47
        // no error should happen
48
        $this->assertTrue(true);
49
    }
50
51
    /**
52
     * Tests IsNotEmpty::execute
53
     *
54
     * @throws \Exception
55
     */
56
    public function testDirectory(): void
57
    {
58
        $io     = new NullIO();
59
        $repo   = $this->createRepositoryMock();
60
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
61
        $action = new Config\Action(
62
            IsNotEmpty::class,
63
            [
64
                'files' => [
65
                    CH_PATH_FILES . '/storage/',
66
                ]
67
            ]
68
        );
69
70
        $isNotEmpty = new IsNotEmpty();
71
        $isNotEmpty->execute($config, $io, $repo, $action);
72
73
        // no error should happen
74
        $this->assertTrue(true);
75
    }
76
77
    /**
78
     * Tests IsNotEmpty::execute
79
     *
80
     * @throws \Exception
81
     */
82
    public function testEmptyDirectoryFail(): void
83
    {
84
        // create empty directory
85
        $emptyDir = sys_get_temp_dir() . '/emptyDir-' . random_int(100, 999);
86
        mkdir($emptyDir, 0777);
87
88
        $io     = new NullIO();
89
        $repo   = $this->createRepositoryMock();
90
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
91
        $action = new Config\Action(
92
            IsNotEmpty::class,
93
            [
94
                'files' => [
95
                    $emptyDir,
96
                ]
97
            ]
98
        );
99
100
        try {
101
            $isNotEmpty = new IsNotEmpty();
102
            $isNotEmpty->execute($config, $io, $repo, $action);
103
        } catch (\Exception $e) {
104
            rmdir($emptyDir);
105
            $this->assertTrue(true);
106
        }
107
    }
108
109
    /**
110
     * Tests IsNotEmpty::execute
111
     *
112
     * @throws \Exception
113
     */
114
    public function testGlob(): void
115
    {
116
        $io     = $this->createIOMock();
117
        $repo   = $this->createRepositoryMock();
118
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
119
        $action = new Config\Action(
120
            IsNotEmpty::class,
121
            [
122
                'files' => [
123
                    CH_PATH_FILES . '/storage/*.txt',
124
                    CH_PATH_FILES . '/storage/*.json',
125
                ]
126
            ]
127
        );
128
129
        // with this configuration the Cap'n should find 4 files for 2 patterns
130
        $io->expects($this->exactly(3))->method('write');
131
132
        $isNotEmpty = new IsNotEmpty();
133
        $isNotEmpty->execute($config, $io, $repo, $action);
134
135
        // no error should happen
136
        $this->assertTrue(true);
137
    }
138
139
    /**
140
     * Tests IsNotEmpty::execute
141
     *
142
     * @throws \Exception
143
     */
144
    public function testFileDoesNotExistFail(): void
145
    {
146
        $this->expectException(\Exception::class);
147
148
        $io     = new NullIO();
149
        $repo   = $this->createRepositoryMock();
150
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
151
        $action = new Config\Action(
152
            IsNotEmpty::class,
153
            [
154
                'files' => [
155
                    CH_PATH_FILES . '/doesNotExist.txt',
156
                ]
157
            ]
158
        );
159
160
        $isNotEmpty = new IsNotEmpty();
161
        $isNotEmpty->execute($config, $io, $repo, $action);
162
    }
163
164
    /**
165
     * Tests IsNotEmpty::execute
166
     *
167
     * @throws \Exception
168
     */
169
    public function testFileEmptyFail(): void
170
    {
171
        $this->expectException(\Exception::class);
172
173
        $io     = new NullIO();
174
        $repo   = $this->createRepositoryMock();
175
        $config = new Config(CH_PATH_FILES . '/captainhook.json');
176
        $action = new Config\Action(
177
            IsNotEmpty::class,
178
            [
179
                'files' => [
180
                    CH_PATH_FILES . '/storage/empty.log',
181
                ]
182
            ]
183
        );
184
185
        $isNotEmpty = new IsNotEmpty();
186
        $isNotEmpty->execute($config, $io, $repo, $action);
187
    }
188
}
189