Passed
Push — feature/config-shorthands ( e682c8 )
by Sebastian
05:07
created

PHPTest::testExecuteByShorthand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
rs 9.9
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\Runner\Action;
13
14
use CaptainHook\App\Config\Mockery as ConfigMockery;
15
use CaptainHook\App\Console\IO\Mockery as IOMockery;
16
use CaptainHook\App\Event\Dispatcher;
17
use CaptainHook\App\Mockery as CHMockery;
18
use Exception;
19
use PHPUnit\Framework\TestCase;
20
21
class PHPTest extends TestCase
22
{
23
    use ConfigMockery;
24
    use IOMockery;
25
    use CHMockery;
26
27
    /**
28
     * Tests PHP::execute
29
     */
30
    public function testExecuteSuccess(): void
31
    {
32
        $config = $this->createConfigMock();
33
        $io     = $this->createIOMock();
34
        $repo   = $this->createRepositoryMock();
35
        $action = $this->createActionConfigMock();
36
        $events = new Dispatcher($io, $config, $repo);
37
        $class  = DummyPHPSuccess::class;
38
39
        $action->expects($this->once())->method('getAction')->willReturn($class);
40
41
        $php = new PHP('pre-commit', $events);
42
        $php->execute($config, $io, $repo, $action);
43
    }
44
45
    /**
46
     * Tests PHP::execute
47
     */
48
    public function testExecuteEventSubscriber(): void
49
    {
50
        $config = $this->createConfigMock();
51
        $io     = $this->createIOMock();
52
        $repo   = $this->createRepositoryMock();
53
        $action = $this->createActionConfigMock();
54
        $events = new Dispatcher($io, $config, $repo);
55
        $class  = DummyPHPSubscriber::class;
56
57
        $action->expects($this->once())->method('getAction')->willReturn($class);
58
59
        $php = new PHP('pre-commit', $events);
60
        $php->execute($config, $io, $repo, $action);
61
    }
62
63
    /**
64
     * Tests PHP::execute
65
     */
66
    public function testExecuteConstraintApplicable(): void
67
    {
68
        $config = $this->createConfigMock();
69
        $io     = $this->createIOMock();
70
        $repo   = $this->createRepositoryMock();
71
        $action = $this->createActionConfigMock();
72
        $events = new Dispatcher($io, $config, $repo);
73
        $class  = DummyPHPConstraint::class;
74
75
        $action->expects($this->once())->method('getAction')->willReturn($class);
76
77
        $php = new PHP('pre-commit', $events);
78
        $php->execute($config, $io, $repo, $action);
79
    }
80
81
    /**
82
     * Tests PHP::execute
83
     */
84
    public function testExecuteConstraintNotApplicable(): void
85
    {
86
        $config = $this->createConfigMock();
87
        $io     = $this->createIOMock();
88
        $repo   = $this->createRepositoryMock();
89
        $action = $this->createActionConfigMock();
90
        $events = new Dispatcher($io, $config, $repo);
91
        $class  = DummyPHPConstraint::class;
92
93
        $action->expects($this->once())->method('getAction')->willReturn($class);
94
95
        $php = new PHP('pre-push', $events);
96
        $php->execute($config, $io, $repo, $action);
97
    }
98
99
    /**
100
     * Tests PHP::execute
101
     *
102
     * @throws \CaptainHook\App\Exception\ActionFailed
103
     */
104
    public function testExecuteFailure(): void
105
    {
106
        $this->expectException(Exception::class);
107
108
        $config = $this->createConfigMock();
109
        $io     = $this->createIOMock();
110
        $repo   = $this->createRepositoryMock();
111
        $action = $this->createActionConfigMock();
112
        $events = new Dispatcher($io, $config, $repo);
113
        $class  = DummyPHPFailure::class;
114
115
        $action->expects($this->once())->method('getAction')->willReturn($class);
116
117
        $php = new PHP('pre-commit', $events);
118
        $php->execute($config, $io, $repo, $action);
119
    }
120
121
    /**
122
     * Tests PHP::execute
123
     *
124
     * @throws \CaptainHook\App\Exception\ActionFailed
125
     */
126
    public function testExecuteError(): void
127
    {
128
        $this->expectException(Exception::class);
129
130
        $config = $this->createConfigMock();
131
        $io     = $this->createIOMock();
132
        $repo   = $this->createRepositoryMock();
133
        $action = $this->createActionConfigMock();
134
        $events = new Dispatcher($io, $config, $repo);
135
        $class  = DummyPHPError::class;
136
137
        $action->expects($this->once())->method('getAction')->willReturn($class);
138
139
        $php = new PHP('pre-commit', $events);
140
        $php->execute($config, $io, $repo, $action);
141
    }
142
143
    /**
144
     * Check if the action shorthand works
145
     */
146
    public function testExecuteByShorthand(): void
147
    {
148
        $this->expectException(Exception::class);
149
        $this->expectExceptionMessageMatches('/debugging/i');
150
151
        $config = $this->createConfigMock();
152
        $io     = $this->createIOMock();
153
        $repo   = $this->createRepositoryMock();
154
        $action = $this->createActionConfigMock();
155
        $events = new Dispatcher($io, $config, $repo);
156
        $class  = 'CaptainHook.Debug.fail';
157
158
        $action->expects($this->once())->method('getAction')->willReturn($class);
159
160
        $php = new PHP('pre-commit', $events);
161
        $php->execute($config, $io, $repo, $action);
162
    }
163
164
    /**
165
     * Tests PHP::execute
166
     *
167
     * @throws \CaptainHook\App\Exception\ActionFailed
168
     */
169
    public function testExecuteNoAction(): void
170
    {
171
        $this->expectException(Exception::class);
172
173
        $config = $this->createConfigMock();
174
        $io     = $this->createIOMock();
175
        $repo   = $this->createRepositoryMock();
176
        $action = $this->createActionConfigMock();
177
        $events = new Dispatcher($io, $config, $repo);
178
        $class  = DummyNoAction::class;
179
180
        $action->expects($this->once())->method('getAction')->willReturn($class);
181
182
        $php = new PHP('pre-commit', $events);
183
        $php->execute($config, $io, $repo, $action);
184
    }
185
186
    /**
187
     * Tests PHP::executeStatic
188
     *
189
     * @throws \CaptainHook\App\Exception\ActionFailed
190
     */
191
    public function testExecuteStaticClassNotFound(): void
192
    {
193
        $this->expectException(Exception::class);
194
195
        $config = $this->createConfigMock();
196
        $io     = $this->createIOMock();
197
        $repo   = $this->createRepositoryMock();
198
        $action = $this->createActionConfigMock();
199
        $events = new Dispatcher($io, $config, $repo);
200
201
        $class = '\\Fiz::baz';
202
203
        $action->expects($this->once())->method('getAction')->willReturn($class);
204
205
        $php = new PHP('pre-commit', $events);
206
        $php->execute($config, $io, $repo, $action);
207
    }
208
209
    /**
210
     * Tests PHP::executeStatic
211
     *
212
     * @throws \CaptainHook\App\Exception\ActionFailed
213
     */
214
    public function testExecuteStaticMethodNotFound(): void
215
    {
216
        $this->expectException(Exception::class);
217
218
        $config = $this->createConfigMock();
219
        $io     = $this->createIOMock();
220
        $repo   = $this->createRepositoryMock();
221
        $action = $this->createActionConfigMock();
222
        $events = new Dispatcher($io, $config, $repo);
223
224
        $class = '\\CaptainHook\\App\\Runner\\Action\\DummyNoAction::foo';
225
226
        $action->expects($this->once())->method('getAction')->willReturn($class);
227
228
        $php = new PHP('pre-commit', $events);
229
        $php->execute($config, $io, $repo, $action);
230
    }
231
232
233
    /**
234
     * Tests PHP::executeStatic
235
     *
236
     * @throws \CaptainHook\App\Exception\ActionFailed
237
     */
238
    public function testExecuteStaticSuccess(): void
239
    {
240
        $config = $this->createConfigMock();
241
        $io     = $this->createIOMock();
242
        $repo   = $this->createRepositoryMock();
243
        $action = $this->createActionConfigMock();
244
        $events = new Dispatcher($io, $config, $repo);
245
246
        $class = '\\CaptainHook\\App\\Runner\\Action\\DummyPHPSuccess::executeStatic';
247
248
        $action->expects($this->once())->method('getAction')->willReturn($class);
249
250
        $php = new PHP('pre-commit', $events);
251
        $php->execute($config, $io, $repo, $action);
252
    }
253
}
254