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

ConfigDumperTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 428
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 4
dl 0
loc 428
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 2
A tearDown() 0 6 1
A providerConfig() 0 16 1
A itDumpsConfigFromFactory() 0 8 1
A providerConfigId() 0 16 1
A itDumpsConfigFromFactoryByConfigId() 0 13 1
A itDumpsConfigFromFactoryByConfigIdWithDefault() 0 15 1
B providerDefaultOptions() 0 34 1
A itDumpsConfigFromFactoryWithDefaults() 0 13 1
A providerDefaultMandatoryOptions() 0 73 1
A itDumpsConfigFromFactoryWithDefaultsAndMandatory() 0 20 1
A providerRecursiveMandatoryOptions() 0 58 1
A itDumpsConfigFromFactoryWithRecursiveMandatory() 0 17 1
B itDumpsConfigFile() 0 40 1
A getTestConfig() 0 11 2
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\ConsoleHelper;
14
use InteropTest\Config\TestAsset;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * @covers \Interop\Config\Tool\AbstractConfig
19
 * @covers \Interop\Config\Tool\ConfigDumper
20
 * @covers \Interop\Config\Tool\ConsoleHelper
21
 */
22
class ConfigDumperTest extends TestCase
23
{
24
    /**
25
     * @var resource Exists only for testing.
26
     */
27
    private $errorStream = STDERR;
28
29
    /**
30
     * Input stream
31
     *
32
     * @var resource
33
     */
34
    private $inputStream;
35
36
    /**
37
     * Output stream
38
     *
39
     * @var resource
40
     */
41
    private $outputStream;
42
43
    /**
44
     * Console Helper
45
     *
46
     * @var ConsoleHelper
47
     */
48
    private $consoleHelper;
49
50
51
    public function setUp()
52
    {
53
        parent::setUp();
54
55
        if (!stream_wrapper_register("test", \InteropTest\Config\TestAsset\TestStream::class)) {
56
            throw new \RuntimeException('Failed to register protocol');
57
        }
58
59
        $this->inputStream = fopen('test://input', 'r+', false);
60
        $this->outputStream = fopen('test://output', 'r+', false);
61
        $this->errorStream = fopen('test://error', 'r+', false);
62
        $this->consoleHelper = new ConsoleHelper($this->inputStream, $this->outputStream, $this->errorStream);
63
    }
64
65
    public function tearDown()
66
    {
67
        stream_wrapper_unregister('test');
68
        TestAsset\TestStream::$inputStack = [];
69
        TestAsset\TestStream::$data = [];
70
    }
71
72
    public function providerConfig()
73
    {
74
        $testConfig = $this->getTestConfig();
75
76
        // order is expected, config from file
77
        return [
78
            [
79
                ['doctrine' => ['connection' => []]],
80
                [],
81
            ],
82
            [
83
                $testConfig,
84
                $testConfig,
85
            ],
86
        ];
87
    }
88
89
    /**
90
     * @test
91
     * @dataProvider providerConfig
92
     */
93
    public function itDumpsConfigFromFactory($expected, $configFromFile)
94
    {
95
        $cut = new ConfigDumper($this->consoleHelper);
96
97
        $config = $cut->createConfig($configFromFile, TestAsset\ConnectionConfiguration::class);
98
99
        self::assertSame($expected, $config);
100
    }
101
102
    public function providerConfigId()
103
    {
104
        $testConfig = $this->getTestConfig();
105
106
        // order is expected, config from file
107
        return [
108
            [
109
                ['doctrine' => ['connection' => ['orm_default' => []]]],
110
                [],
111
            ],
112
            [
113
                $testConfig,
114
                $testConfig,
115
            ],
116
        ];
117
    }
118
119
    /**
120
     * @test
121
     * @dataProvider providerConfigId
122
     */
123
    public function itDumpsConfigFromFactoryByConfigId($expected, $configFromFile)
124
    {
125
        TestAsset\TestStream::$inputStack = ['orm_default'];
126
        $cut = new ConfigDumper($this->consoleHelper);
127
128
        $config = $cut->createConfig($configFromFile, TestAsset\ConnectionContainerIdConfiguration::class);
129
130
        self::assertSame(
131
            'Multiple instances are supported, please enter a config id (default):',
132
            trim(TestAsset\TestStream::$data['output'])
133
        );
134
        self::assertSame($expected, $config);
135
    }
136
137
    /**
138
     * @test
139
     */
140
    public function itDumpsConfigFromFactoryByConfigIdWithDefault()
141
    {
142
        TestAsset\TestStream::$inputStack = [''];
143
        $cut = new ConfigDumper($this->consoleHelper);
144
145
        $expected =  ['doctrine' => ['connection' => ['default' => []]]];
146
147
        $config = $cut->createConfig([], TestAsset\ConnectionContainerIdConfiguration::class);
148
149
        self::assertSame(
150
            trim('Multiple instances are supported, please enter a config id (default):'),
151
            trim(TestAsset\TestStream::$data['output'])
152
        );
153
        self::assertSame($expected, $config);
154
    }
155
156
    public function providerDefaultOptions()
157
    {
158
        $testConfig = $this->getTestConfig();
159
160
        $defaultConfig = [
161
            'doctrine' => [
162
                'connection' => [
163
                    'params' => [
164
                        'host' => 'awesomehost',
165
                        'port' => 4444,
166
                    ],
167
                ],
168
            ],
169
        ];
170
171
        $output = 'Please enter a value for params.host (awesomehost): '
172
            . 'Please enter a value for params.port (4444):';
173
174
        // order is expected, config from file, input stack, output
175
        return [
176
            [
177
                $defaultConfig,
178
                [],
179
                [],
180
                $output,
181
            ],
182
            [
183
                array_replace_recursive($testConfig, $defaultConfig),
184
                $testConfig,
185
                [],
186
                $output,
187
            ],
188
        ];
189
    }
190
191
    /**
192
     * @test
193
     * @dataProvider providerDefaultOptions
194
     */
195
    public function itDumpsConfigFromFactoryWithDefaults($expected, $configFromFile, $inputStack, $output)
196
    {
197
        TestAsset\TestStream::$inputStack = $inputStack;
198
        $cut = new ConfigDumper($this->consoleHelper);
199
200
        $config = $cut->createConfig($configFromFile, TestAsset\ConnectionDefaultOptionsConfiguration::class);
201
202
        self::assertSame(
203
            trim($output),
204
            trim(TestAsset\TestStream::$data['output'])
205
        );
206
        self::assertSame($expected, $config);
207
    }
208
209
    public function providerDefaultMandatoryOptions()
210
    {
211
        $testConfig = $this->getTestConfig();
212
213
        $configDefault = [
214
            'doctrine' => [
215
                'connection' => [
216
                    'orm_default' => [
217
                        'params' => [
218
                            'host' => 'awesomehost',
219
                            'port' => 4444,
220
                        ],
221
                        'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver'
222
                    ],
223
                ],
224
            ],
225
        ];
226
227
        $configExisting = [
228
            'doctrine' => [
229
                'connection' => [
230
                    'orm_default' => [
231
                        'params' => [
232
                            'host' => 'localhost',
233
                            'port' => 3306,
234
                        ],
235
                        'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver'
236
                    ],
237
                ],
238
            ],
239
        ];
240
241
        $configInput = [
242
            'doctrine' => [
243
                'connection' => [
244
                    'orm_default' => [
245
                        'params' => [
246
                            'host' => 'myhost',
247
                            'port' => 1111,
248
                        ],
249
                        'driverClass' => 'PDO'
250
                    ],
251
                ],
252
            ],
253
        ];
254
255
        // @codingStandardsIgnoreStart
256
        $output = 'Multiple instances are supported, please enter a config id (default): Please enter a value for driverClass: Please enter a value for params.host (awesomehost): Please enter a value for params.port (4444):';
257
        $outputExistingConfig = 'Multiple instances are supported, please enter a config id (default): Please enter a value for driverClass (Doctrine\DBAL\Driver\PDOMySql\Driver): Please enter a value for params.host (localhost), current value awesomehost: Please enter a value for params.port (3306), current value 4444:';
258
        // @codingStandardsIgnoreEnd
259
260
        // order is expected, config from file, input stack, output
261
        return [
262
            [
263
                $configDefault,
264
                [],
265
                ['orm_default', 'Doctrine\DBAL\Driver\PDOMySql\Driver', '', ''],
266
                $output,
267
            ],
268
            [
269
                array_replace_recursive($testConfig, $configExisting),
270
                $testConfig,
271
                ['orm_default', '', '', ''],
272
                $outputExistingConfig,
273
            ],
274
            [
275
                array_replace_recursive($testConfig, $configInput),
276
                $testConfig,
277
                ['orm_default', 'PDO', 'myhost', 1111],
278
                $outputExistingConfig,
279
            ],
280
        ];
281
    }
282
283
    /**
284
     * @test
285
     * @dataProvider providerDefaultMandatoryOptions
286
     */
287
    public function itDumpsConfigFromFactoryWithDefaultsAndMandatory(
288
        $expected,
289
        $configFromFile,
290
        $inputStack,
291
        $output
292
    ) {
293
        TestAsset\TestStream::$inputStack = $inputStack;
294
        $cut = new ConfigDumper($this->consoleHelper);
295
296
        $config = $cut->createConfig(
297
            $configFromFile,
298
            TestAsset\ConnectionDefaultOptionsMandatoryContainetIdConfiguration::class
299
        );
300
301
        self::assertSame(
302
            trim($output),
303
            trim(TestAsset\TestStream::$data['output'])
304
        );
305
        self::assertSame($expected, $config);
306
    }
307
308
    public function providerRecursiveMandatoryOptions()
309
    {
310
        $testConfig = $this->getTestConfig();
311
312
        $config = [
313
            'doctrine' => [
314
                'universal' => [
315
                    'orm_default' => [
316
                        'params' => [
317
                            'host' => 'awesomehost',
318
                            'port' => 4444,
319
                            'user' => 'root',
320
                            'dbname' => 'database',
321
322
                        ],
323
                        'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver'
324
                    ],
325
                ],
326
            ],
327
        ];
328
329
        $configExisting = array_replace_recursive(
330
            $testConfig,
331
            [
332
                'doctrine' => [
333
                    'universal' => [
334
                        'orm_default' => [
335
                            'params' => [
336
                                'port' => 4444,
337
                                'user' => 'root',
338
                            ],
339
                        ],
340
                    ],
341
                ],
342
            ]
343
        );
344
345
        // @codingStandardsIgnoreStart
346
        $output = 'Multiple instances are supported, please enter a config id (default): Please enter a value for params.user: Please enter a value for params.dbname: Please enter a value for driverClass: Please enter a value for params.host (awesomehost): Please enter a value for params.port (4444):';
347
        $outputExistingConfig = 'Multiple instances are supported, please enter a config id (default): Please enter a value for params.user (username): Please enter a value for params.dbname (database): Please enter a value for driverClass (Doctrine\DBAL\Driver\PDOMySql\Driver): Please enter a value for params.host (localhost), current value awesomehost: Please enter a value for params.port (4444):';
348
        // @codingStandardsIgnoreEnd
349
350
        // order is expected, config from file, input stack, output
351
        return [
352
            [
353
                $config,
354
                [],
355
                ['orm_default', 'root', 'database', 'Doctrine\DBAL\Driver\PDOMySql\Driver', '', ''],
356
                $output,
357
            ],
358
            [
359
                $configExisting,
360
                $testConfig,
361
                ['orm_default', 'root', 'database', 'Doctrine\DBAL\Driver\PDOMySql\Driver', '', ''],
362
                $outputExistingConfig,
363
            ],
364
        ];
365
    }
366
367
    /**
368
     * @test
369
     * @dataProvider providerRecursiveMandatoryOptions
370
     */
371
    public function itDumpsConfigFromFactoryWithRecursiveMandatory(
372
        $expected,
373
        $configFromFile,
374
        $inputStack,
375
        $output
376
    ) {
377
        TestAsset\TestStream::$inputStack = $inputStack;
378
        $cut = new ConfigDumper($this->consoleHelper);
379
380
        $config = $cut->createConfig($configFromFile, TestAsset\UniversalContainerIdConfiguration::class);
381
382
        self::assertSame(
383
            trim($output),
384
            trim(TestAsset\TestStream::$data['output'])
385
        );
386
        self::assertSame($expected, $config);
387
    }
388
389
    /**
390
     * @test
391
     */
392
    public function itDumpsConfigFile()
393
    {
394
        $cut = new ConfigDumper($this->consoleHelper);
395
396
        $config = [
397
            // vendor name
398
            'doctrine' => [
399
                // package name
400
                'connection' => [
401
                    // container id
402
                    'orm_default' => [
403
                        // mandatory params
404
                        'driverClass' => 'PDO',
405
                        'params' => [
406
                            'host'     => 'localhost',
407
                            'port'     => 3306,
408
                        ],
409
                    ],
410
                ]
411
            ]
412
        ];
413
414
        $configFile = <<<EOF
415
return [
416
    'doctrine' => [
417
        'connection' => [
418
            'orm_default' => [
419
                'driverClass' => \PDO::class,
420
                'params' => [
421
                    'host' => 'localhost',
422
                    'port' => 3306,
423
                ],
424
            ],
425
        ],
426
    ],
427
];
428
EOF;
429
430
        self::assertSame($configFile, $cut->dumpConfigFile($config));
431
    }
432
433
    /**
434
     * Returns test config
435
     *
436
     * @return array
437
     */
438
    private function getTestConfig(): array
439
    {
440
        // Load the user-defined test configuration file, if it exists; otherwise, load default
441
        if (is_readable('test/TestConfig.php')) {
442
            $config = require 'test/testing.config.php';
443
        } else {
444
            $config = require 'test/testing.config.php.dist';
445
        }
446
447
        return $config;
448
    }
449
}
450