Failed Conditions
Push — master ( 41301b...74c660 )
by Jonathan
19s queued 11s
created

ConsoleTest::testGenerateShowContributors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
rs 9.8333
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 PHPUnit_Framework_MockObject_MockObject;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Input\ArrayInput;
16
use Symfony\Component\Console\Output\BufferedOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Output\StreamOutput;
19
use function file_exists;
20
use function sprintf;
21
use function sys_get_temp_dir;
22
use function tempnam;
23
use function uniqid;
24
use function unlink;
25
26
final class ConsoleTest extends TestCase
27
{
28
    /** @var PHPUnit_Framework_MockObject_MockObject|ChangelogGenerator */
29
    private $changelogGenerator;
30
31
    /** @var PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommand */
32
    private $generateChangelogCommand;
33
34
    /** @var Application */
35
    private $application;
36
37
    public function testGenerate() : void
38
    {
39
        $input = new ArrayInput([
40
            'command'       => 'generate',
41
            '--user'        => 'jwage',
42
            '--repository'  => 'changelog-generator',
43
            '--milestone'   => '1.0',
44
        ]);
45
46
        $output = $this->createMock(OutputInterface::class);
47
48
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
49
50
        $this->changelogGenerator->expects(self::once())
51
            ->method('generate')
52
            ->with($changelogConfig, $output);
53
54
        $this->application->run($input, $output);
55
    }
56
57
    public function testGenerateShowContributors() : void
58
    {
59
        $input = new ArrayInput([
60
            'command'             => 'generate',
61
            '--user'              => 'jwage',
62
            '--repository'        => 'changelog-generator',
63
            '--milestone'         => '1.0',
64
            '--show-contributors' => null,
65
        ]);
66
67
        $output = $this->createMock(OutputInterface::class);
68
69
        $changelogConfig = (new ChangelogConfig('jwage', 'changelog-generator', '1.0', []))
70
            ->setShowContributors(true);
71
72
        $this->changelogGenerator->expects(self::once())
73
            ->method('generate')
74
            ->with($changelogConfig, $output);
75
76
        $this->application->run($input, $output);
77
    }
78
79
    public function testGenerateInvalidConfig() : void
80
    {
81
        $this->expectException(InvalidArgumentException::class);
82
        $this->expectExceptionMessage('You must pass a config file with the --config option or manually specify the --user --repository and --milestone options.');
83
84
        $input = new ArrayInput(['command' => 'generate']);
85
86
        $output = $this->createMock(OutputInterface::class);
87
88
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
89
90
        $this->changelogGenerator->expects(self::never())
91
            ->method('generate')
92
            ->with($changelogConfig, $output);
93
94
        $this->application->run($input, $output);
95
    }
96
97
    public function testGenerateFile() : void
98
    {
99
        $input = new ArrayInput([
100
            'command'       => 'generate',
101
            '--user'        => 'jwage',
102
            '--repository'  => 'changelog-generator',
103
            '--milestone'   => '1.0',
104
            '--file'        => null,
105
        ]);
106
107
        $output       = $this->createMock(OutputInterface::class);
108
        $streamOutput = $this->createMock(StreamOutput::class);
109
110
        $this->generateChangelogCommand->expects(self::once())
111
            ->method('createOutput')
112
            ->willReturn($streamOutput);
113
114
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
115
116
        $this->changelogGenerator->expects(self::once())
117
            ->method('generate')
118
            ->with($changelogConfig, $streamOutput);
119
120
        $this->application->run($input, $output);
121
    }
122
123
    public function testGenerateFilePathGiven() : void
124
    {
125
        $input = new ArrayInput([
126
            'command'       => 'generate',
127
            '--user'        => 'jwage',
128
            '--repository'  => 'changelog-generator',
129
            '--milestone'   => '1.0',
130
            '--file'        => tempnam(sys_get_temp_dir(), 'CHANGELOG.md'),
131
        ]);
132
133
        $output       = $this->createMock(OutputInterface::class);
134
        $streamOutput = $this->createMock(StreamOutput::class);
135
136
        $this->generateChangelogCommand->expects(self::once())
137
            ->method('createOutput')
138
            ->willReturn($streamOutput);
139
140
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
141
142
        $this->changelogGenerator->expects(self::once())
143
            ->method('generate')
144
            ->with($changelogConfig, $streamOutput);
145
146
        $this->application->run($input, $output);
147
    }
148
149
    public function testGenerateFileAppend() : void
150
    {
151
        $input = new ArrayInput([
152
            'command'       => 'generate',
153
            '--user'        => 'jwage',
154
            '--repository'  => 'changelog-generator',
155
            '--milestone'   => '1.0',
156
            '--file'        => tempnam(sys_get_temp_dir(), 'CHANGELOG.md'),
157
            '--append'      => true,
158
        ]);
159
160
        $output       = $this->createMock(OutputInterface::class);
161
        $streamOutput = $this->createMock(StreamOutput::class);
162
163
        $this->generateChangelogCommand->expects(self::once())
164
            ->method('createOutput')
165
            ->willReturn($streamOutput);
166
167
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
168
169
        $this->changelogGenerator->expects(self::once())
170
            ->method('generate')
171
            ->with($changelogConfig, $streamOutput);
172
173
        $this->application->run($input, $output);
174
    }
175
176
    public function testGenerateFilePrepend() : void
177
    {
178
        $input = new ArrayInput([
179
            'command'       => 'generate',
180
            '--user'        => 'jwage',
181
            '--repository'  => 'changelog-generator',
182
            '--milestone'   => '1.0',
183
            '--file'        => tempnam(sys_get_temp_dir(), 'CHANGELOG.md'),
184
            '--prepend'     => true,
185
        ]);
186
187
        $output         = $this->createMock(OutputInterface::class);
188
        $bufferedOutput = $this->createMock(BufferedOutput::class);
189
190
        $this->generateChangelogCommand->expects(self::once())
191
            ->method('createOutput')
192
            ->willReturn($bufferedOutput);
193
194
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
195
196
        $this->changelogGenerator->expects(self::once())
197
            ->method('generate')
198
            ->with($changelogConfig, $bufferedOutput);
199
200
        $this->application->run($input, $output);
201
    }
202
203
    public function testGenerateFilePrependStreamOutput() : void
204
    {
205
        $input = new ArrayInput([
206
            'command'       => 'generate',
207
            '--user'        => 'jwage',
208
            '--repository'  => 'changelog-generator',
209
            '--milestone'   => '1.0',
210
            '--file'        => tempnam(sys_get_temp_dir(), 'CHANGELOG.md'),
211
            '--prepend'     => true,
212
        ]);
213
214
        $output       = $this->createMock(OutputInterface::class);
215
        $streamOutput = $this->createMock(StreamOutput::class);
216
217
        $this->generateChangelogCommand->expects(self::once())
218
            ->method('createOutput')
219
            ->willReturn($streamOutput);
220
221
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
222
223
        $this->changelogGenerator->expects(self::once())
224
            ->method('generate')
225
            ->with($changelogConfig, $streamOutput);
226
227
        $this->application->run($input, $output);
228
    }
229
230
    public function testGenerateFilePrependCreatesFileThatDoesNotExist() : void
231
    {
232
        $file = sprintf('%s/%sCHANGELOG.md', sys_get_temp_dir(), uniqid());
233
234
        $input = new ArrayInput([
235
            'command'       => 'generate',
236
            '--user'        => 'jwage',
237
            '--repository'  => 'changelog-generator',
238
            '--milestone'   => '1.0',
239
            '--file'        => $file,
240
            '--prepend'     => true,
241
        ]);
242
243
        $output         = $this->createMock(OutputInterface::class);
244
        $bufferedOutput = $this->createMock(BufferedOutput::class);
245
246
        $this->generateChangelogCommand->expects(self::once())
247
            ->method('createOutput')
248
            ->willReturn($bufferedOutput);
249
250
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', []);
251
252
        $this->changelogGenerator->expects(self::once())
253
            ->method('generate')
254
            ->with($changelogConfig, $bufferedOutput);
255
256
        $this->application->run($input, $output);
257
258
        $exists = file_exists($file);
259
260
        unlink($file);
261
262
        self::assertTrue($exists);
263
    }
264
265
    public function testGenerateLabel() : void
266
    {
267
        $input = new ArrayInput([
268
            'command'       => 'generate',
269
            '--user'        => 'jwage',
270
            '--repository'  => 'changelog-generator',
271
            '--milestone'   => '1.0',
272
            '--label'       => ['Enhancement', 'Bug'],
273
        ]);
274
275
        $output = $this->createMock(OutputInterface::class);
276
277
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
278
279
        $this->changelogGenerator->expects(self::once())
280
            ->method('generate')
281
            ->with($changelogConfig, $output);
282
283
        $this->application->run($input, $output);
284
    }
285
286
    public function testGenerateConfig() : void
287
    {
288
        $input = new ArrayInput([
289
            'command'       => 'generate',
290
            '--user'        => 'jwage',
291
            '--repository'  => 'changelog-generator',
292
            '--milestone'   => '1.0',
293
            '--label'       => ['Enhancement', 'Bug'],
294
            '--config'      => __DIR__ . '/_files/config.php',
295
            '--project'     => 'changelog-generator',
296
        ]);
297
298
        $output = $this->createMock(OutputInterface::class);
299
300
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
301
302
        $this->changelogGenerator->expects(self::once())
303
            ->method('generate')
304
            ->with($changelogConfig, $output);
305
306
        $this->application->run($input, $output);
307
    }
308
309
    public function testGenerateConfigDoesNotExist() : void
310
    {
311
        $this->expectException(InvalidArgumentException::class);
312
        $this->expectExceptionMessage('Configuration file "unknown.php" does not exist.');
313
314
        $input = new ArrayInput([
315
            'command'       => 'generate',
316
            '--user'        => 'jwage',
317
            '--repository'  => 'changelog-generator',
318
            '--milestone'   => '1.0',
319
            '--label'       => ['Enhancement', 'Bug'],
320
            '--config'      => 'unknown.php',
321
        ]);
322
323
        $output = $this->createMock(OutputInterface::class);
324
325
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
326
327
        $this->changelogGenerator->expects(self::never())
328
            ->method('generate')
329
            ->with($changelogConfig, $output);
330
331
        $this->application->run($input, $output);
332
    }
333
334
    public function testGenerateConfigEmpty() : void
335
    {
336
        $configFile = __DIR__ . '/_files/empty.php';
337
338
        $this->expectException(InvalidArgumentException::class);
339
        $this->expectExceptionMessage(sprintf('Configuration file "%s" did not return anything.', $configFile));
340
341
        $input = new ArrayInput([
342
            'command'       => 'generate',
343
            '--user'        => 'jwage',
344
            '--repository'  => 'changelog-generator',
345
            '--milestone'   => '1.0',
346
            '--label'       => ['Enhancement', 'Bug'],
347
            '--config'      => $configFile,
348
        ]);
349
350
        $output = $this->createMock(OutputInterface::class);
351
352
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
353
354
        $this->changelogGenerator->expects(self::never())
355
            ->method('generate')
356
            ->with($changelogConfig, $output);
357
358
        $this->application->run($input, $output);
359
    }
360
361
    public function testGenerateConfigInvalidProject() : void
362
    {
363
        $this->expectException(InvalidArgumentException::class);
364
        $this->expectExceptionMessage('Could not find project named "unknown" configured');
365
366
        $input = new ArrayInput([
367
            'command'       => 'generate',
368
            '--user'        => 'jwage',
369
            '--repository'  => 'changelog-generator',
370
            '--milestone'   => '1.0',
371
            '--label'       => ['Enhancement', 'Bug'],
372
            '--config'      => __DIR__ . '/_files/config.php',
373
            '--project'     => 'unknown',
374
        ]);
375
376
        $output = $this->createMock(OutputInterface::class);
377
378
        $changelogConfig = new ChangelogConfig('jwage', 'changelog-generator', '1.0', ['Enhancement', 'Bug']);
379
380
        $this->changelogGenerator->expects(self::never())
381
            ->method('generate')
382
            ->with($changelogConfig, $output);
383
384
        $this->application->run($input, $output);
385
    }
386
387
    public function testGenerateIncludeOpenOptionNotProvided() : void
388
    {
389
        $input = new ArrayInput([
390
            'command'        => 'generate',
391
            '--user'         => 'doctrine',
392
            '--repository'   => 'migrations',
393
            '--milestone'    => '2.0',
394
            '--label'        => ['Improvement', 'Bug'],
395
            '--config'       => __DIR__ . '/_files/config.php',
396
            '--project'      => 'changelog-generator',
397
        ]);
398
399
        $output = $this->createMock(OutputInterface::class);
400
401
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Improvement', 'Bug'], false);
402
403
        $this->changelogGenerator->expects(self::once())
404
            ->method('generate')
405
            ->with($changelogConfig, $output);
406
407
        $this->application->run($input, $output);
408
    }
409
410
    public function testGenerateIncludeOpenDefault() : void
411
    {
412
        $input = new ArrayInput([
413
            'command'        => 'generate',
414
            '--user'         => 'doctrine',
415
            '--repository'   => 'migrations',
416
            '--milestone'    => '2.0',
417
            '--label'        => ['Improvement', 'Bug'],
418
            '--config'       => __DIR__ . '/_files/config.php',
419
            '--project'      => 'changelog-generator',
420
            '--include-open' => null,
421
        ]);
422
423
        $output = $this->createMock(OutputInterface::class);
424
425
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Improvement', 'Bug'], true);
426
427
        $this->changelogGenerator->expects(self::once())
428
            ->method('generate')
429
            ->with($changelogConfig, $output);
430
431
        $this->application->run($input, $output);
432
    }
433
434
    public function testGenerateIncludeOpenTrue() : void
435
    {
436
        $input = new ArrayInput([
437
            'command'        => 'generate',
438
            '--user'         => 'doctrine',
439
            '--repository'   => 'migrations',
440
            '--milestone'    => '2.0',
441
            '--label'        => ['Improvement', 'Bug'],
442
            '--config'       => __DIR__ . '/_files/config.php',
443
            '--project'      => 'changelog-generator',
444
            '--include-open' => '1',
445
        ]);
446
447
        $output = $this->createMock(OutputInterface::class);
448
449
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Improvement', 'Bug'], true);
450
451
        $this->changelogGenerator->expects(self::once())
452
            ->method('generate')
453
            ->with($changelogConfig, $output);
454
455
        $this->application->run($input, $output);
456
    }
457
458
    public function testGenerateIncludeOpenFalse() : void
459
    {
460
        $input = new ArrayInput([
461
            'command'        => 'generate',
462
            '--user'         => 'doctrine',
463
            '--repository'   => 'migrations',
464
            '--milestone'    => '2.0',
465
            '--label'        => ['Improvement', 'Bug'],
466
            '--config'       => __DIR__ . '/_files/config.php',
467
            '--project'      => 'changelog-generator',
468
            '--include-open' => '0',
469
        ]);
470
471
        $output = $this->createMock(OutputInterface::class);
472
473
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Improvement', 'Bug'], false);
474
475
        $this->changelogGenerator->expects(self::once())
476
            ->method('generate')
477
            ->with($changelogConfig, $output);
478
479
        $this->application->run($input, $output);
480
    }
481
482
    public function testGenerateConfigOverrideNoLabels() : void
483
    {
484
        $input = new ArrayInput([
485
            'command'       => 'generate',
486
            '--user'        => 'doctrine',
487
            '--repository'  => 'migrations',
488
            '--milestone'   => '2.0',
489
            '--config'      => __DIR__ . '/_files/config.php',
490
            '--project'     => 'changelog-generator',
491
        ]);
492
493
        $output = $this->createMock(OutputInterface::class);
494
495
        $changelogConfig = new ChangelogConfig('doctrine', 'migrations', '2.0', ['Enhancement', 'Bug']);
496
497
        $this->changelogGenerator->expects(self::once())
498
            ->method('generate')
499
            ->with($changelogConfig, $output);
500
501
        $this->application->run($input, $output);
502
    }
503
504
    public function testCreateOutput() : void
505
    {
506
        $generateChangelogCommand = new GenerateChangelogCommandStub($this->changelogGenerator);
507
508
        $file = sprintf('%s/test.md', sys_get_temp_dir());
509
510
        self::assertInstanceOf(
511
            StreamOutput::class,
512
            $generateChangelogCommand->createOutputTest($file, GenerateChangelogCommand::WRITE_STRATEGY_APPEND)
513
        );
514
515
        self::assertInstanceOf(
516
            BufferedOutput::class,
517
            $generateChangelogCommand->createOutputTest($file, GenerateChangelogCommand::WRITE_STRATEGY_PREPEND)
518
        );
519
520
        self::assertInstanceOf(
521
            StreamOutput::class,
522
            $generateChangelogCommand->createOutputTest($file, GenerateChangelogCommand::WRITE_STRATEGY_REPLACE)
523
        );
524
525
        unlink($file);
526
    }
527
528
    public function testCreateOutputCouldNotOpenHandleInvalidArgumentException() : void
529
    {
530
        $this->expectException(InvalidArgumentException::class);
531
        $this->expectExceptionMessage('Could not open handle for /tmp/test.md');
532
533
        $file = sprintf('%s/test.md', sys_get_temp_dir());
534
535
        /** @var PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommandStub $generateChangelogCommand */
536
        $generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommandStub::class)
537
            ->setConstructorArgs([$this->changelogGenerator])
538
            ->setMethods(['fopen'])
539
            ->getMock();
540
541
        $generateChangelogCommand->expects(self::once())
542
            ->method('fopen')
543
            ->with($file, 'a+')
544
            ->willReturn(false);
545
546
        $generateChangelogCommand->createOutputTest($file, GenerateChangelogCommand::WRITE_STRATEGY_APPEND);
547
    }
548
549
    protected function setUp() : void
550
    {
551
        $this->changelogGenerator = $this->createMock(ChangelogGenerator::class);
552
553
        $this->application = new Application('Changelog Generator', Versions::getVersion('jwage/changelog-generator'));
554
        $this->application->setAutoExit(false);
555
        $this->application->setCatchExceptions(false);
556
557
        $this->generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommand::class)
558
            ->setConstructorArgs([$this->changelogGenerator])
559
            ->setMethods(['createOutput'])
560
            ->getMock();
561
562
        $this->application->add($this->generateChangelogCommand);
563
    }
564
}
565
566
class GenerateChangelogCommandStub extends GenerateChangelogCommand
567
{
568
    public function createOutputTest(string $file, string $fileWriteStrategy) : OutputInterface
569
    {
570
        return $this->createOutput($file, $fileWriteStrategy);
571
    }
572
}
573