Completed
Push — master ( 7fdc78...830b42 )
by Jonathan
11s
created

ConsoleTest::testGenerateFile()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 24
rs 8.9713
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 testCreateStreamOutput() : void
153
    {
154
        $generateChangelogCommand = new GenerateChangelogCommandStub($this->changelogGenerator);
155
156
        $file = sprintf('%s/test.md', sys_get_temp_dir());
157
158
        self::assertInstanceOf(StreamOutput::class, $generateChangelogCommand->createStreamOutputTest($file, true));
159
        self::assertInstanceOf(StreamOutput::class, $generateChangelogCommand->createStreamOutputTest($file, false));
160
161
        unlink($file);
162
    }
163
164
    public function testCreateStreamOutputCouldNotOpenHandleInvalidArgumentException() : void
165
    {
166
        $this->expectException(InvalidArgumentException::class);
167
        $this->expectExceptionMessage('Could not open handle for /tmp/test.md');
168
169
        $file = sprintf('%s/test.md', sys_get_temp_dir());
170
171
        /** @var \PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommandStub $generateChangelogCommand */
172
        $generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommandStub::class)
173
            ->setConstructorArgs([$this->changelogGenerator])
174
            ->setMethods(['fopen'])
175
            ->getMock();
176
177
        $generateChangelogCommand->expects($this->once())
178
            ->method('fopen')
179
            ->with($file, 'a+')
180
            ->willReturn(false);
181
182
        $generateChangelogCommand->createStreamOutputTest($file, true);
183
    }
184
185
    protected function setUp() : void
186
    {
187
        $this->changelogGenerator = $this->createMock(ChangelogGenerator::class);
188
189
        $this->application = new Application('Changelog Generator', Versions::getVersion('jwage/changelog-generator'));
190
        $this->application->setAutoExit(false);
191
192
        $this->generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommand::class)
193
            ->setConstructorArgs([$this->changelogGenerator])
194
            ->setMethods(['createStreamOutput'])
195
            ->getMock();
196
197
        $this->application->add($this->generateChangelogCommand);
198
    }
199
}
200
201
class GenerateChangelogCommandStub extends GenerateChangelogCommand
202
{
203
    public function createStreamOutputTest(string $file, bool $append) : StreamOutput
204
    {
205
        return $this->createStreamOutput($file, $append);
206
    }
207
}
208