Completed
Push — master ( 830b42...366775 )
by Jonathan
10s
created

ConsoleTest::testGenerateConfigEmpty()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator\Tests\Functional;
6
7
use ChangelogGenerator\ChangelogConfig;
8
use ChangelogGenerator\ChangelogGenerator;
9
use ChangelogGenerator\Command\GenerateChangelogCommand;
10
use InvalidArgumentException;
11
use PackageVersions\Versions;
12
use PHPUnit\Framework\TestCase;
13
use Symfony\Component\Console\Application;
14
use Symfony\Component\Console\Input\ArrayInput;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Output\StreamOutput;
17
use function sprintf;
18
use function sys_get_temp_dir;
19
use function unlink;
20
21
final class ConsoleTest extends TestCase
22
{
23
    /** @var \PHPUnit_Framework_MockObject_MockObject|ChangelogGenerator */
24
    private $changelogGenerator;
25
26
    /** @var \PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommand */
27
    private $generateChangelogCommand;
28
29
    /** @var Application */
30
    private $application;
31
32
    public function testGenerate() : void
33
    {
34
        $input = new ArrayInput([
35
            'command'       => 'generate',
36
            '--user'        => 'jwage',
37
            '--repository'  => 'changelog-generator',
38
            '--milestone'   => '1.0',
39
        ]);
40
41
        $output = $this->createMock(OutputInterface::class);
42
43
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
44
45
        $this->changelogGenerator->expects($this->once())
46
            ->method('generate')
47
            ->with($changelogConfig, $output);
48
49
        $this->application->run($input, $output);
50
    }
51
52
    public function testGenerateFile() : void
53
    {
54
        $input = new ArrayInput([
55
            'command'       => 'generate',
56
            '--user'        => 'jwage',
57
            '--repository'  => 'changelog-generator',
58
            '--milestone'   => '1.0',
59
            '--file'        => null,
60
        ]);
61
62
        $output       = $this->createMock(OutputInterface::class);
63
        $streamOutput = $this->createMock(StreamOutput::class);
64
65
        $this->generateChangelogCommand->expects($this->once())
66
            ->method('createStreamOutput')
67
            ->willReturn($streamOutput);
68
69
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
70
71
        $this->changelogGenerator->expects($this->once())
72
            ->method('generate')
73
            ->with($changelogConfig, $streamOutput);
74
75
        $this->application->run($input, $output);
76
    }
77
78
    public function testGenerateFilePathGiven() : void
79
    {
80
        $input = new ArrayInput([
81
            'command'       => 'generate',
82
            '--user'        => 'jwage',
83
            '--repository'  => 'changelog-generator',
84
            '--milestone'   => '1.0',
85
            '--file'        => 'CHANGELOG.md',
86
        ]);
87
88
        $output       = $this->createMock(OutputInterface::class);
89
        $streamOutput = $this->createMock(StreamOutput::class);
90
91
        $this->generateChangelogCommand->expects($this->once())
92
            ->method('createStreamOutput')
93
            ->willReturn($streamOutput);
94
95
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
96
97
        $this->changelogGenerator->expects($this->once())
98
            ->method('generate')
99
            ->with($changelogConfig, $streamOutput);
100
101
        $this->application->run($input, $output);
102
    }
103
104
    public function testGenerateFileAppend() : void
105
    {
106
        $input = new ArrayInput([
107
            'command'       => 'generate',
108
            '--user'        => 'jwage',
109
            '--repository'  => 'changelog-generator',
110
            '--milestone'   => '1.0',
111
            '--file'        => 'CHANGELOG.md',
112
            '--append'      => true,
113
        ]);
114
115
        $output       = $this->createMock(OutputInterface::class);
116
        $streamOutput = $this->createMock(StreamOutput::class);
117
118
        $this->generateChangelogCommand->expects($this->once())
119
            ->method('createStreamOutput')
120
            ->willReturn($streamOutput);
121
122
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
123
124
        $this->changelogGenerator->expects($this->once())
125
            ->method('generate')
126
            ->with($changelogConfig, $streamOutput);
127
128
        $this->application->run($input, $output);
129
    }
130
131
    public function testGenerateLabel() : void
132
    {
133
        $input = new ArrayInput([
134
            'command'       => 'generate',
135
            '--user'        => 'jwage',
136
            '--repository'  => 'changelog-generator',
137
            '--milestone'   => '1.0',
138
            '--label'       => ['Enhancement', 'Bug'],
139
        ]);
140
141
        $output = $this->createMock(OutputInterface::class);
142
143
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
144
145
        $this->changelogGenerator->expects($this->once())
146
            ->method('generate')
147
            ->with($changelogConfig, $output);
148
149
        $this->application->run($input, $output);
150
    }
151
152
    public function testGenerateConfig() : void
153
    {
154
        $input = new ArrayInput([
155
            'command'       => 'generate',
156
            '--user'        => 'jwage',
157
            '--repository'  => 'changelog-generator',
158
            '--milestone'   => '1.0',
159
            '--label'       => ['Enhancement', 'Bug'],
160
            '--config'      => __DIR__ . '/_files/config.php',
161
            '--project'     => 'changelog-generator',
162
        ]);
163
164
        $output = $this->createMock(OutputInterface::class);
165
166
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
167
168
        $this->changelogGenerator->expects($this->once())
169
            ->method('generate')
170
            ->with($changelogConfig, $output);
171
172
        $this->application->run($input, $output);
173
    }
174
175
    public function testGenerateConfigDoesNotExist() : void
176
    {
177
        $this->expectException(InvalidArgumentException::class);
178
        $this->expectExceptionMessage('Configuration file "unknown.php" does not exist.');
179
180
        $input = new ArrayInput([
181
            'command'       => 'generate',
182
            '--user'        => 'jwage',
183
            '--repository'  => 'changelog-generator',
184
            '--milestone'   => '1.0',
185
            '--label'       => ['Enhancement', 'Bug'],
186
            '--config'      => 'unknown.php',
187
        ]);
188
189
        $output = $this->createMock(OutputInterface::class);
190
191
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
192
193
        $this->changelogGenerator->expects($this->never())
194
            ->method('generate')
195
            ->with($changelogConfig, $output);
196
197
        $this->application->run($input, $output);
198
    }
199
200
    public function testGenerateConfigEmpty() : void
201
    {
202
        $configFile = __DIR__ . '/_files/empty.php';
203
204
        $this->expectException(InvalidArgumentException::class);
205
        $this->expectExceptionMessage(sprintf('Configuration file "%s" did not return anything.', $configFile));
206
207
        $input = new ArrayInput([
208
            'command'       => 'generate',
209
            '--user'        => 'jwage',
210
            '--repository'  => 'changelog-generator',
211
            '--milestone'   => '1.0',
212
            '--label'       => ['Enhancement', 'Bug'],
213
            '--config'      => $configFile,
214
        ]);
215
216
        $output = $this->createMock(OutputInterface::class);
217
218
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
219
220
        $this->changelogGenerator->expects($this->never())
221
            ->method('generate')
222
            ->with($changelogConfig, $output);
223
224
        $this->application->run($input, $output);
225
    }
226
227
    public function testGenerateConfigInvalidProject() : void
228
    {
229
        $this->expectException(InvalidArgumentException::class);
230
        $this->expectExceptionMessage('Could not find project named "unknown" configured');
231
232
        $input = new ArrayInput([
233
            'command'       => 'generate',
234
            '--user'        => 'jwage',
235
            '--repository'  => 'changelog-generator',
236
            '--milestone'   => '1.0',
237
            '--label'       => ['Enhancement', 'Bug'],
238
            '--config'      => __DIR__ . '/_files/config.php',
239
            '--project'     => 'unknown',
240
        ]);
241
242
        $output = $this->createMock(OutputInterface::class);
243
244
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
245
246
        $this->changelogGenerator->expects($this->never())
247
            ->method('generate')
248
            ->with($changelogConfig, $output);
249
250
        $this->application->run($input, $output);
251
    }
252
253
    public function testGenerateConfigOverride() : void
254
    {
255
        $input = new ArrayInput([
256
            'command'       => 'generate',
257
            '--user'        => 'doctrine',
258
            '--repository'  => 'migrations',
259
            '--milestone'   => '2.0',
260
            '--label'       => ['Improvement', 'Bug'],
261
            '--config'      => __DIR__ . '/_files/config.php',
262
            '--project'     => 'changelog-generator',
263
        ]);
264
265
        $output = $this->createMock(OutputInterface::class);
266
267
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Improvement', 'Bug']);
268
269
        $this->changelogGenerator->expects($this->once())
270
            ->method('generate')
271
            ->with($changelogConfig, $output);
272
273
        $this->application->run($input, $output);
274
    }
275
276
277
    public function testGenerateConfigOverrideNoLabels() : void
278
    {
279
        $input = new ArrayInput([
280
            'command'       => 'generate',
281
            '--user'        => 'doctrine',
282
            '--repository'  => 'migrations',
283
            '--milestone'   => '2.0',
284
            '--config'      => __DIR__ . '/_files/config.php',
285
            '--project'     => 'changelog-generator',
286
        ]);
287
288
        $output = $this->createMock(OutputInterface::class);
289
290
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Enhancement', 'Bug']);
291
292
        $this->changelogGenerator->expects($this->once())
293
            ->method('generate')
294
            ->with($changelogConfig, $output);
295
296
        $this->application->run($input, $output);
297
    }
298
299
    public function testCreateStreamOutput() : void
300
    {
301
        $generateChangelogCommand = new GenerateChangelogCommandStub($this->changelogGenerator);
302
303
        $file = sprintf('%s/test.md', sys_get_temp_dir());
304
305
        self::assertInstanceOf(StreamOutput::class, $generateChangelogCommand->createStreamOutputTest($file, true));
306
        self::assertInstanceOf(StreamOutput::class, $generateChangelogCommand->createStreamOutputTest($file, false));
307
308
        unlink($file);
309
    }
310
311
    public function testCreateStreamOutputCouldNotOpenHandleInvalidArgumentException() : void
312
    {
313
        $this->expectException(InvalidArgumentException::class);
314
        $this->expectExceptionMessage('Could not open handle for /tmp/test.md');
315
316
        $file = sprintf('%s/test.md', sys_get_temp_dir());
317
318
        /** @var \PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommandStub $generateChangelogCommand */
319
        $generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommandStub::class)
320
            ->setConstructorArgs([$this->changelogGenerator])
321
            ->setMethods(['fopen'])
322
            ->getMock();
323
324
        $generateChangelogCommand->expects($this->once())
325
            ->method('fopen')
326
            ->with($file, 'a+')
327
            ->willReturn(false);
328
329
        $generateChangelogCommand->createStreamOutputTest($file, true);
330
    }
331
332
    protected function setUp() : void
333
    {
334
        $this->changelogGenerator = $this->createMock(ChangelogGenerator::class);
335
336
        $this->application = new Application('Changelog Generator', Versions::getVersion('jwage/changelog-generator'));
337
        $this->application->setAutoExit(false);
338
        $this->application->setCatchExceptions(false);
339
340
        $this->generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommand::class)
341
            ->setConstructorArgs([$this->changelogGenerator])
342
            ->setMethods(['createStreamOutput'])
343
            ->getMock();
344
345
        $this->application->add($this->generateChangelogCommand);
346
    }
347
}
348
349
class GenerateChangelogCommandStub extends GenerateChangelogCommand
350
{
351
    public function createStreamOutputTest(string $file, bool $append) : StreamOutput
352
    {
353
        return $this->createStreamOutput($file, $append);
354
    }
355
}
356