Completed
Push — master ( 62cea3...109027 )
by Jonathan
11s
created

testCreateStreamOutputCouldNotOpenHandleInvalidArgumentException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator\Tests\Functional;
6
7
use ChangelogGenerator\ChangelogGenerator;
8
use ChangelogGenerator\Command\GenerateChangelogCommand;
9
use InvalidArgumentException;
10
use PackageVersions\Versions;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Input\ArrayInput;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Output\StreamOutput;
16
use function sprintf;
17
use function sys_get_temp_dir;
18
use function unlink;
19
20
final class ConsoleTest extends TestCase
21
{
22
    /** @var \PHPUnit_Framework_MockObject_MockObject|ChangelogGenerator */
23
    private $changelogGenerator;
24
25
    /** @var \PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommand */
26
    private $generateChangelogCommand;
27
28
    /** @var Application */
29
    private $application;
30
31
    public function testGenerate() : void
32
    {
33
        $input = new ArrayInput([
34
            'command'       => 'generate',
35
            '--user'        => 'jwage',
36
            '--repository'  => 'changelog-generator',
37
            '--milestone'   => '1.0',
38
        ]);
39
40
        $output = $this->createMock(OutputInterface::class);
41
42
        $this->changelogGenerator->expects($this->once())
43
            ->method('generate')
44
            ->with('jwage', 'changelog-generator', '1.0', $output);
45
46
        $this->application->run($input, $output);
47
    }
48
49
    public function testGenerateFile() : void
50
    {
51
        $input = new ArrayInput([
52
            'command'       => 'generate',
53
            '--user'        => 'jwage',
54
            '--repository'  => 'changelog-generator',
55
            '--milestone'   => '1.0',
56
            '--file'        => null,
57
        ]);
58
59
        $output       = $this->createMock(OutputInterface::class);
60
        $streamOutput = $this->createMock(StreamOutput::class);
61
62
        $this->generateChangelogCommand->expects($this->once())
63
            ->method('createStreamOutput')
64
            ->willReturn($streamOutput);
65
66
        $this->changelogGenerator->expects($this->once())
67
            ->method('generate')
68
            ->with('jwage', 'changelog-generator', '1.0', $streamOutput);
69
70
        $this->application->run($input, $output);
71
    }
72
73
    public function testGenerateFilePathGiven() : void
74
    {
75
        $input = new ArrayInput([
76
            'command'       => 'generate',
77
            '--user'        => 'jwage',
78
            '--repository'  => 'changelog-generator',
79
            '--milestone'   => '1.0',
80
            '--file'        => 'CHANGELOG.md',
81
        ]);
82
83
        $output       = $this->createMock(OutputInterface::class);
84
        $streamOutput = $this->createMock(StreamOutput::class);
85
86
        $this->generateChangelogCommand->expects($this->once())
87
            ->method('createStreamOutput')
88
            ->willReturn($streamOutput);
89
90
        $this->changelogGenerator->expects($this->once())
91
            ->method('generate')
92
            ->with('jwage', 'changelog-generator', '1.0', $streamOutput);
93
94
        $this->application->run($input, $output);
95
    }
96
97
    public function testGenerateFileAppend() : void
98
    {
99
        $input = new ArrayInput([
100
            'command'       => 'generate',
101
            '--user'        => 'jwage',
102
            '--repository'  => 'changelog-generator',
103
            '--milestone'   => '1.0',
104
            '--file'        => 'CHANGELOG.md',
105
            '--append'      => true,
106
        ]);
107
108
        $output       = $this->createMock(OutputInterface::class);
109
        $streamOutput = $this->createMock(StreamOutput::class);
110
111
        $this->generateChangelogCommand->expects($this->once())
112
            ->method('createStreamOutput')
113
            ->willReturn($streamOutput);
114
115
        $this->changelogGenerator->expects($this->once())
116
            ->method('generate')
117
            ->with('jwage', 'changelog-generator', '1.0', $streamOutput);
118
119
        $this->application->run($input, $output);
120
    }
121
122
    public function testCreateStreamOutput() : void
123
    {
124
        $generateChangelogCommand = new GenerateChangelogCommandStub($this->changelogGenerator);
125
126
        $file = sprintf('%s/test.md', sys_get_temp_dir());
127
128
        self::assertInstanceOf(StreamOutput::class, $generateChangelogCommand->createStreamOutputTest($file, true));
129
        self::assertInstanceOf(StreamOutput::class, $generateChangelogCommand->createStreamOutputTest($file, false));
130
131
        unlink($file);
132
    }
133
134
    public function testCreateStreamOutputCouldNotOpenHandleInvalidArgumentException() : void
135
    {
136
        $this->expectException(InvalidArgumentException::class);
137
        $this->expectExceptionMessage('Could not open handle for /tmp/test.md');
138
139
        $file = sprintf('%s/test.md', sys_get_temp_dir());
140
141
        /** @var \PHPUnit_Framework_MockObject_MockObject|GenerateChangelogCommandStub $generateChangelogCommand */
142
        $generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommandStub::class)
143
            ->setConstructorArgs([$this->changelogGenerator])
144
            ->setMethods(['fopen'])
145
            ->getMock();
146
147
        $generateChangelogCommand->expects($this->once())
148
            ->method('fopen')
149
            ->with($file, 'a+')
150
            ->willReturn(false);
151
152
        $generateChangelogCommand->createStreamOutputTest($file, true);
153
    }
154
155
    protected function setUp() : void
156
    {
157
        $this->changelogGenerator = $this->createMock(ChangelogGenerator::class);
158
159
        $this->application = new Application('Changelog Generator', Versions::getVersion('jwage/changelog-generator'));
160
        $this->application->setAutoExit(false);
161
162
        $this->generateChangelogCommand = $this->getMockBuilder(GenerateChangelogCommand::class)
163
            ->setConstructorArgs([$this->changelogGenerator])
164
            ->setMethods(['createStreamOutput'])
165
            ->getMock();
166
167
        $this->application->add($this->generateChangelogCommand);
168
    }
169
}
170
171
class GenerateChangelogCommandStub extends GenerateChangelogCommand
172
{
173
    public function createStreamOutputTest(string $file, bool $append) : StreamOutput
174
    {
175
        return $this->createStreamOutput($file, $append);
176
    }
177
}
178