Passed
Pull Request — master (#42)
by
unknown
04:53 queued 43s
created

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