Completed
Push — master ( 74fc93...195e55 )
by Sandro
06:11
created

ConfigDumperCommandTest::itWritesConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/interop-config for the canonical source repository
6
 * @copyright Copyright (c) 2017-2017 Sandro Keil
7
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
8
 */
9
10
namespace InteropTest\Config\Tool;
11
12
use Interop\Config\Tool\ConfigDumper;
13
use Interop\Config\Tool\ConfigDumperCommand;
14
use Interop\Config\Tool\ConsoleHelper;
15
use InteropTest\Config\TestAsset;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @covers \Interop\Config\Tool\AbstractCommand
20
 * @covers \Interop\Config\Tool\ConfigDumperCommand
21
 * @covers \Interop\Config\Tool\ConsoleHelper
22
 */
23
class ConfigDumperCommandTest extends TestCase
24
{
25
    const CONFIG_FILE = 'build/config.php';
26
27
    /**
28
     * @var resource Exists only for testing.
29
     */
30
    private $errorStream = STDERR;
31
32
    /**
33
     * Input stream
34
     *
35
     * @var resource
36
     */
37
    private $inputStream;
38
39
    /**
40
     * Output stream
41
     *
42
     * @var resource
43
     */
44
    private $outputStream;
45
46
    /**
47
     * Console Helper
48
     *
49
     * @var ConsoleHelper
50
     */
51
    private $consoleHelper;
52
53
54
    public function setUp()
55
    {
56
        parent::setUp();
57
58
        if (!stream_wrapper_register("test", TestAsset\TestStream::class)) {
59
            throw new \RuntimeException('Failed to register protocol');
60
        }
61
62
        $this->inputStream = fopen('test://input', 'r+', false);
63
        $this->outputStream = fopen('test://output', 'r+', false);
64
        $this->errorStream = fopen('test://error', 'r+', false);
65
        $this->consoleHelper = new ConsoleHelper($this->inputStream, $this->outputStream, $this->errorStream);
66
    }
67
68
    public function tearDown()
69
    {
70
        stream_wrapper_unregister('test');
71
        TestAsset\TestStream::$inputStack = [];
72
        TestAsset\TestStream::$data = [];
73
    }
74
75
    /**
76
     * @test
77
     */
78
    public function itWritesConfig()
79
    {
80
        $argv = [self::CONFIG_FILE, TestAsset\ConnectionConfiguration::class];
81
82
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper());
83
84
        $cut($argv);
85
        self::assertTrue(file_exists(self::CONFIG_FILE));
86
        unlink(self::CONFIG_FILE);
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function itWritesConfigToExistingFile()
93
    {
94
        $testConfig = <<<EOF
95
<?php
96
/**
97
 * Sandro Keil (https://sandro-keil.de)
98
 *
99
 * @link      http://github.com/sandrokeil/interop-config for the canonical source repository
100
 * @copyright Copyright (c) 2017-2017 Sandro Keil
101
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
102
 */
103
104
use Interop\Config\RequiresConfig;
105
106
// my comment
107
return [
108
    'doctrine' => [
109
        'connection' => [
110
            'orm_default' => [
111
                'driverClass' => \PDO::class,
112
                'params' => [
113
                    'host' => 'localhost',
114
                    'port' => 3306,
115
                ],
116
            ],
117
        ],
118
    ],
119
];
120
121
EOF;
122
123
        file_put_contents(self::CONFIG_FILE, $testConfig);
124
125
        $argv = [self::CONFIG_FILE, TestAsset\ConnectionConfiguration::class];
126
127
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper());
128
129
        $cut($argv);
130
        self::assertSame($testConfig, file_get_contents(self::CONFIG_FILE));
131
        unlink(self::CONFIG_FILE);
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function itDisplaysHelp()
138
    {
139
        $argv = ['help'];
140
141
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
142
143
        $cut($argv);
144
145
        self::assertStringStartsWith('Usage:', TestAsset\TestStream::$data['error']);
146
    }
147
148
    /**
149
     * @test
150
     */
151
    public function itDisplaysHelpIfNoArguments()
152
    {
153
        $argv = [];
154
155
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
156
157
        $cut($argv);
158
159
        self::assertStringStartsWith('Usage:', TestAsset\TestStream::$data['error']);
160
    }
161
162
    /**
163
     * @test
164
     */
165
    public function itDisplaysErrorIfClassNotExists()
166
    {
167
        $argv = [self::CONFIG_FILE, 'UnknownClassName'];
168
169
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
170
171
        $cut($argv);
172
173
        self::assertStringStartsWith('Class "UnknownClassName"', TestAsset\TestStream::$data['error']);
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function itDisplaysErrorIfFileIsNotWriteable()
180
    {
181
        $argv = ['/unknown/place/config.php', TestAsset\ConnectionConfiguration::class];
182
183
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
184
185
        $cut($argv);
186
187
        self::assertStringStartsWith('Cannot create configuration', TestAsset\TestStream::$data['error']);
188
    }
189
190
    /**
191
     * @test
192
     */
193
    public function itDisplaysErrorIfFileReturnsNoArray()
194
    {
195
        $argv = [__DIR__ . '/_files/no_array.php', TestAsset\ConnectionConfiguration::class];
196
197
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
198
199
        $cut($argv);
200
201
        self::assertStringStartsWith('Configuration at path', TestAsset\TestStream::$data['error']);
202
    }
203
204
    /**
205
     * @test
206
     */
207
    public function itDisplaysErrorIfClassDoesNotImplementRequiresConfig()
208
    {
209
        $argv = [self::CONFIG_FILE, TestAsset\TestStream::class];
210
211
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
212
213
        $cut($argv);
214
215
        self::assertStringStartsWith(
216
            'Class "InteropTest\Config\TestAsset\TestStream" does not implement',
217
            TestAsset\TestStream::$data['error']
218
        );
219
    }
220
221
    /**
222
     * @test
223
     */
224
    public function itDisplaysError()
225
    {
226
        $argv = ['wrong'];
227
228
        $cut = new ConfigDumperCommand($this->consoleHelper, new ConfigDumper($this->consoleHelper));
229
230
        $cut($argv);
231
232
        self::assertStringStartsWith('Missing class name', TestAsset\TestStream::$data['error']);
233
    }
234
}
235