Completed
Push — master ( f60e47...74fc93 )
by Sandro
02:28
created

ConfigurationTraitTest::testOptionsWithObjects()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
nop 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) 2015-2016 Sandro Keil
7
 * @license   http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types = 1);
11
12
namespace InteropTest\Config;
13
14
use Interop\Config\Exception\InvalidArgumentException;
15
use Interop\Config\Exception\MandatoryOptionNotFoundException;
16
use Interop\Config\Exception\OptionNotFoundException;
17
use Interop\Config\Exception\UnexpectedValueException;
18
use InteropTest\Config\TestAsset\ConnectionConfiguration;
19
use InteropTest\Config\TestAsset\ConnectionContainerIdConfiguration;
20
use InteropTest\Config\TestAsset\ConnectionDefaultOptionsConfiguration;
21
use InteropTest\Config\TestAsset\ConnectionDefaultOptionsMandatoryContainetIdConfiguration;
22
use InteropTest\Config\TestAsset\ConnectionMandatoryConfiguration;
23
use InteropTest\Config\TestAsset\ConnectionMandatoryContainerIdConfiguration;
24
use InteropTest\Config\TestAsset\ConnectionMandatoryRecursiveArrayIteratorContainerIdConfiguration;
25
use InteropTest\Config\TestAsset\ConnectionMandatoryRecursiveContainerIdConfiguration;
26
use InteropTest\Config\TestAsset\FlexibleConfiguration;
27
use InteropTest\Config\TestAsset\PackageDefaultAndMandatoryOptionsConfiguration;
28
use InteropTest\Config\TestAsset\PackageDefaultOptionsConfiguration;
29
use InteropTest\Config\TestAsset\PlainConfiguration;
30
use InteropTest\Config\TestAsset\UniversalContainerIdConfiguration;
31
use PHPUnit_Framework_TestCase as TestCase;
32
33
/**
34
 * ConfigurationTraitTest
35
 *
36
 * Tests integrity of \Interop\Config\ConfigurationTrait
37
 */
38
class ConfigurationTraitTest extends TestCase
39
{
40
    /**
41
     * Class under test
42
     *
43
     * @var string
44
     */
45
    protected $cut = 'Interop\Config\ConfigurationTrait';
46
47
    /**
48
     * Tests options() should throw exception if config parameter is not an array
49
     *
50
     * @covers \Interop\Config\ConfigurationTrait::options
51
     */
52
    public function testOptionsThrowsInvalidArgumentExceptionIfConfigIsNotAnArray(): void
53
    {
54
        $stub = new ConnectionConfiguration();
55
56
        $this->assertException(UnexpectedValueException::class, 'position is "doctrine"');
57
58
        $stub->options(['doctrine' => new \stdClass()]);
59
    }
60
61
    /**
62
     * Tests canRetrieveOptions() : void
63
     *
64
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
65
     */
66
    public function testCanRetrieveOptions(): void
67
    {
68
        $stub = new ConnectionConfiguration();
69
70
        self::assertSame(false, $stub->canRetrieveOptions(''));
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array|object<ArrayAccess>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
        self::assertSame(false, $stub->canRetrieveOptions(new \stdClass()));
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a array|object<ArrayAccess>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
        self::assertSame(false, $stub->canRetrieveOptions(null));
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object<ArrayAccess>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
74
        self::assertSame(
75
            false,
76
            $stub->canRetrieveOptions(['doctrine' => ['invalid' => ['default' => ['params' => '']]]])
77
        );
78
79
        self::assertSame(
80
            false,
81
            $stub->canRetrieveOptions(['doctrine' => ['connection' => new \stdClass()]])
82
        );
83
84
        self::assertSame(true, $stub->canRetrieveOptions($this->getTestConfig()));
85
    }
86
87
    /**
88
     * Tests canRetrieveOptions() : void
89
     *
90
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
91
     */
92
    public function testCanRetrieveOptionsWithContainerId(): void
93
    {
94
        $stub = new ConnectionContainerIdConfiguration();
95
96
        self::assertSame(false, $stub->canRetrieveOptions(['doctrine' => ['connection' => null]]), 'orm_default');
97
98
        self::assertSame(
99
            false,
100
            $stub->canRetrieveOptions(['doctrine' => ['connection' => ['invalid' => ['test' => 1]]]], 'orm_default')
101
        );
102
103
        self::assertSame(
104
            false,
105
            $stub->canRetrieveOptions(
106
                [
107
                    'doctrine' => [
108
                        'connection' => [
109
                            'orm_default' => new \stdClass(),
110
                        ],
111
                    ],
112
                ],
113
                'orm_default'
114
            )
115
        );
116
117
        self::assertSame(true, $stub->canRetrieveOptions($this->getTestConfig(), 'orm_default'));
118
    }
119
120
    /**
121
     * Tests options() should throw exception if config id is provided but RequiresConfigId interface is not implemented
122
     *
123
     * @covers \Interop\Config\ConfigurationTrait::options
124
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
125
     */
126
    public function testOptionsThrowsInvalidArgumentExIfConfigIdIsProvidedButRequiresConfigIdIsNotImplemented(): void
127
    {
128
        $stub = new ConnectionConfiguration();
129
130
        self::assertFalse($stub->canRetrieveOptions(['doctrine' => []], 'configId'));
131
132
        $this->assertException(InvalidArgumentException::class, 'The factory');
133
134
        $stub->options(['doctrine' => []], 'configId');
135
    }
136
137
    /**
138
     * Tests options() should throw exception if config id is missing but RequiresConfigId interface is implemented
139
     *
140
     * @dataProvider providerConfig
141
     * @covers       \Interop\Config\ConfigurationTrait::options
142
     * @covers       \Interop\Config\Exception\OptionNotFoundException::missingOptions
143
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
144
     */
145
    public function testOptionsThrowsOptionNotFoundExceptionIfConfigIdIsMissingWithRequiresConfigId($config): void
146
    {
147
        $stub = new ConnectionContainerIdConfiguration();
148
149
        $this->assertException(OptionNotFoundException::class, 'The configuration');
150
151
        self::assertFalse($stub->canRetrieveOptions($config, null));
152
153
        $stub->options($config, null);
154
    }
155
156
    /**
157
     * Tests options() should throw exception if no vendor config is available
158
     *
159
     * @covers \Interop\Config\ConfigurationTrait::options
160
     * @covers \Interop\Config\Exception\OptionNotFoundException::missingOptions
161
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
162
     */
163
    public function testOptionsThrowsOptionNotFoundExceptionIfNoVendorConfigIsAvailable(): void
164
    {
165
        $stub = new ConnectionConfiguration();
166
167
        $config = ['doctrine' => []];
168
169
        self::assertFalse($stub->canRetrieveOptions($config));
170
171
        $this->assertException(OptionNotFoundException::class, 'doctrine');
172
173
        $stub->options($config);
174
    }
175
176
    /**
177
     * Tests options() should throw exception if no package option is available
178
     *
179
     * @covers \Interop\Config\ConfigurationTrait::options
180
     * @covers \Interop\Config\Exception\OptionNotFoundException::missingOptions
181
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
182
     */
183
    public function testOptionsThrowsOptionNotFoundExceptionIfNoPackageOptionIsAvailable(): void
184
    {
185
        $stub = new ConnectionConfiguration();
186
187
        $config = ['doctrine' => ['connection' => null]];
188
189
        self::assertFalse($stub->canRetrieveOptions($config));
190
191
        $this->assertException(
192
            OptionNotFoundException::class,
193
            'No options set for configuration "doctrine.connection"'
194
        );
195
196
        $stub->options($config);
197
    }
198
199
    /**
200
     * Tests options() should throw exception if no container id option is available
201
     *
202
     * @covers \Interop\Config\ConfigurationTrait::options
203
     * @covers \Interop\Config\Exception\OptionNotFoundException::missingOptions
204
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
205
     */
206
    public function testOptionsThrowsOptionNotFoundExceptionIfNoContainerIdOptionIsAvailable(): void
207
    {
208
        $stub = new ConnectionContainerIdConfiguration();
209
210
        $config = ['doctrine' => ['connection' => ['orm_default' => null]]];
211
212
        self::assertFalse($stub->canRetrieveOptions($config, 'orm_default'));
213
214
        $this->assertException(OptionNotFoundException::class, '"doctrine.connection.orm_default"');
215
216
        $stub->options($config, 'orm_default');
217
    }
218
219
    /**
220
     * Tests options() should throw exception if a dimension is not available
221
     *
222
     * @covers \Interop\Config\ConfigurationTrait::options
223
     * @covers \Interop\Config\Exception\OptionNotFoundException::missingOptions
224
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
225
     */
226
    public function testOptionsThrowsOptionNotFoundExceptionIfDimensionIsNotAvailable(): void
227
    {
228
        $stub = new FlexibleConfiguration();
229
230
        $config = ['one' => ['two' => ['three' => ['invalid' => ['dimension']]]]];
231
232
        self::assertFalse($stub->canRetrieveOptions($config));
233
234
        $this->assertException(OptionNotFoundException::class, '"one.two.three.four"');
235
236
        $stub->options(['one' => ['two' => ['three' => ['invalid' => ['dimension']]]]]);
237
    }
238
239
    /**
240
     * Tests if options() works with dimensions, default options and mandatory options if no config is available
241
     *
242
     * @covers \Interop\Config\ConfigurationTrait::options
243
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
244
     */
245
    public function testOptionsThrowsExceptionIfMandatoryOptionsWithDefaultOptionsSetAndNoConfigurationIsSet(): void
246
    {
247
        $stub = new PackageDefaultAndMandatoryOptionsConfiguration();
248
249
        self::assertFalse($stub->canRetrieveOptions([]));
250
251
        $this->assertException(OptionNotFoundException::class, '"vendor"');
252
253
        $stub->options([]);
254
    }
255
256
    /**
257
     * Tests options() should throw exception if retrieved options not an array or \ArrayAccess
258
     *
259
     * @covers \Interop\Config\ConfigurationTrait::options
260
     * @covers \Interop\Config\Exception\UnexpectedValueException::invalidOptions
261
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
262
     */
263
    public function testOptionsThrowsUnexpectedValueExceptionIfRetrievedOptionsNotAnArrayOrArrayAccess(): void
264
    {
265
        $stub = new ConnectionContainerIdConfiguration();
266
267
        $config = ['doctrine' => ['connection' => ['orm_default' => new \stdClass()]]];
268
269
        self::assertFalse($stub->canRetrieveOptions($config, 'orm_default'));
270
271
        $this->assertException(UnexpectedValueException::class, 'Configuration must either be of');
272
273
        $stub->options($config, 'orm_default');
274
    }
275
276
    /**
277
     * Tests if options() works with container id
278
     *
279
     * @dataProvider providerConfig
280
     * @covers       \Interop\Config\ConfigurationTrait::options
281
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
282
     */
283
    public function testOptionsReturnsDataWithContainerId($config): void
284
    {
285
        $stub = new ConnectionContainerIdConfiguration();
286
287
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
288
289
        $options = $stub->options($config, 'orm_default');
290
291
        self::assertArrayHasKey('driverClass', $options);
292
        self::assertArrayHasKey('params', $options);
293
    }
294
295
    /**
296
     * Tests if options() works without container id
297
     *
298
     * @dataProvider providerConfig
299
     * @covers       \Interop\Config\ConfigurationTrait::options
300
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
301
     */
302
    public function testOptionsReturnsData($config): void
303
    {
304
        $stub = new ConnectionConfiguration();
305
306
        self::assertTrue($stub->canRetrieveOptions($config));
307
308
        $options = $stub->options($config);
309
310
        self::assertArrayHasKey('orm_default', $options);
311
    }
312
313
    /**
314
     * Tests if options() works with flexible dimensions
315
     *
316
     * @dataProvider providerConfig
317
     * @covers       \Interop\Config\ConfigurationTrait::options
318
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
319
     */
320
    public function testOptionsReturnsDataWithFlexibleDimensions($config): void
321
    {
322
        $stub = new FlexibleConfiguration();
323
324
        self::assertTrue($stub->canRetrieveOptions($config));
325
326
        $options = $stub->options($config);
327
328
        self::assertArrayHasKey('name', $options);
329
        self::assertArrayHasKey('class', $options);
330
    }
331
332
    /**
333
     * Tests if options() works with no dimensions
334
     *
335
     * @dataProvider providerConfig
336
     * @covers       \Interop\Config\ConfigurationTrait::options
337
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
338
     */
339
    public function testOptionsReturnsDataWithNoDimensions($config): void
340
    {
341
        $stub = new PlainConfiguration();
342
343
        self::assertTrue($stub->canRetrieveOptions($config));
344
345
        $options = $stub->options($config);
346
347
        self::assertArrayHasKey('doctrine', $options);
348
        self::assertArrayHasKey('one', $options);
349
    }
350
351
    /**
352
     * Tests if options() works with default options
353
     *
354
     * @dataProvider providerConfig
355
     * @covers       \Interop\Config\ConfigurationTrait::options
356
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
357
     */
358
    public function testOptionsReturnsDataWithDefaultOptions($config): void
359
    {
360
        $stub = new ConnectionDefaultOptionsMandatoryContainetIdConfiguration();
361
362
        unset($config['doctrine']['connection']['orm_default']['params']['host']);
363
        unset($config['doctrine']['connection']['orm_default']['params']['port']);
364
365
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
366
        $options = $stub->options($config, 'orm_default');
367
        $defaultOptions = $stub->defaultOptions();
368
369
        self::assertCount(2, $options);
370
        self::assertArrayHasKey('params', $options);
371
        self::assertSame($options['params']['host'], $defaultOptions['params']['host']);
372
        self::assertSame($options['params']['port'], $defaultOptions['params']['port']);
373
        self::assertSame(
374
            $options['params']['user'],
375
            $config['doctrine']['connection']['orm_default']['params']['user']
376
        );
377
378
        $config = $this->getTestConfig();
379
380
        # remove main index key
381
        unset($config['doctrine']['connection']['orm_default']['params']);
382
383
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
384
        $options = $stub->options($config, 'orm_default');
385
386
        self::assertCount(2, $options);
387
        self::assertArrayHasKey('params', $options);
388
        self::assertSame($options['params']['host'], $defaultOptions['params']['host']);
389
        self::assertSame($options['params']['port'], $defaultOptions['params']['port']);
390
    }
391
392
    /**
393
     * Tests if options() works with dimensions and default options if no config is available
394
     *
395
     * @covers \Interop\Config\ConfigurationTrait::options
396
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
397
     */
398
    public function testOptionsReturnsPackageDataWithDefaultOptionsIfNoConfigurationIsSet(): void
399
    {
400
        $stub = new PackageDefaultOptionsConfiguration();
401
402
        self::assertTrue($stub->canRetrieveOptions([]));
403
404
        $expected = [
405
            'minLength' => 2,
406
            'maxLength' => 10,
407
        ];
408
409
        $options = $stub->options([]);
410
411
        self::assertCount(2, $options);
412
        self::assertSame($expected, $options);
413
    }
414
415
    /**
416
     * Tests if options() works default options and default options not override provided options
417
     *
418
     * @dataProvider providerConfig
419
     * @covers       \Interop\Config\ConfigurationTrait::options
420
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
421
     */
422
    public function testOptionsThatDefaultOptionsNotOverrideProvidedOptions($config): void
423
    {
424
        $stub = new ConnectionDefaultOptionsMandatoryContainetIdConfiguration();
425
426
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
427
        $options = $stub->options($config, 'orm_default');
428
429
        self::assertCount(2, $options);
430
        self::assertArrayHasKey('params', $options);
431
        self::assertSame(
432
            $options['params']['host'],
433
            $config['doctrine']['connection']['orm_default']['params']['host']
434
        );
435
        self::assertSame(
436
            $options['params']['port'],
437
            $config['doctrine']['connection']['orm_default']['params']['port']
438
        );
439
        self::assertSame(
440
            $options['params']['user'],
441
            $config['doctrine']['connection']['orm_default']['params']['user']
442
        );
443
    }
444
445
    /**
446
     * Tests if options() works with mandatory options interface
447
     *
448
     * @dataProvider providerConfig
449
     * @covers       \Interop\Config\ConfigurationTrait::options
450
     * @covers       \Interop\Config\ConfigurationTrait::checkMandatoryOptions
451
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
452
     */
453
    public function testOptionsChecksMandatoryOptions($config): void
454
    {
455
        $stub = new ConnectionMandatoryConfiguration();
456
457
        self::assertTrue($stub->canRetrieveOptions($config));
458
        $options = $stub->options($config);
459
460
        self::assertCount(1, $options);
461
        self::assertArrayHasKey('orm_default', $options);
462
    }
463
464
    /**
465
     * Tests if options() works with mandatory options interface
466
     *
467
     * @dataProvider providerConfig
468
     * @covers       \Interop\Config\ConfigurationTrait::options
469
     * @covers       \Interop\Config\ConfigurationTrait::checkMandatoryOptions
470
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
471
     */
472
    public function testOptionsChecksMandatoryOptionsWithContainerId($config): void
473
    {
474
        $stub = new ConnectionMandatoryContainerIdConfiguration();
475
476
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
477
        $options = $stub->options($config, 'orm_default');
478
479
        self::assertCount(2, $options);
480
        self::assertArrayHasKey('driverClass', $options);
481
        self::assertArrayHasKey('params', $options);
482
    }
483
484
    /**
485
     * Tests if options() throws a runtime exception if mandatory option is missing
486
     *
487
     * @dataProvider providerConfig
488
     * @covers       \Interop\Config\ConfigurationTrait::options
489
     * @covers       \Interop\Config\ConfigurationTrait::checkMandatoryOptions
490
     * @covers       \Interop\Config\Exception\MandatoryOptionNotFoundException::missingOption
491
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
492
     */
493
    public function testOptionsThrowsMandatoryOptionNotFoundExceptionIfMandatoryOptionIsMissing($config): void
494
    {
495
        $stub = new ConnectionMandatoryContainerIdConfiguration();
496
497
        unset($config['doctrine']['connection']['orm_default']['params']);
498
499
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
500
501
        $this->assertException(MandatoryOptionNotFoundException::class, 'Mandatory option "params"');
502
        $stub->options($config, 'orm_default');
503
    }
504
505
    /**
506
     * Tests if options() throws a runtime exception if a recursive mandatory option is missing
507
     *
508
     * @dataProvider providerConfig
509
     * @covers       \Interop\Config\ConfigurationTrait::options
510
     * @covers       \Interop\Config\ConfigurationTrait::checkMandatoryOptions
511
     * @covers       \Interop\Config\Exception\MandatoryOptionNotFoundException::missingOption
512
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
513
     */
514
    public function testOptionsThrowsMandatoryOptionNotFoundExceptionIfMandatoryOptionRecursiveIsMissing($config): void
515
    {
516
        $stub = new ConnectionMandatoryRecursiveContainerIdConfiguration();
517
518
        unset($config['doctrine']['connection']['orm_default']['params']['dbname']);
519
520
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
521
522
        $this->assertException(MandatoryOptionNotFoundException::class, 'Mandatory option "dbname"');
523
        $stub->options($config, 'orm_default');
524
    }
525
526
    /**
527
     * Tests options() with recursive mandatory options check
528
     *
529
     * @dataProvider providerConfig
530
     * @covers       \Interop\Config\ConfigurationTrait::options
531
     * @covers       \Interop\Config\ConfigurationTrait::checkMandatoryOptions
532
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
533
     */
534
    public function testOptionsWithRecursiveMandatoryOptionCheck($config): void
535
    {
536
        $stub = new ConnectionMandatoryRecursiveContainerIdConfiguration();
537
538
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
539
        self::assertArrayHasKey('params', $stub->options($config, 'orm_default'));
540
    }
541
542
    /**
543
     * Tests options() with recursive mandatory options as array iterator
544
     *
545
     * @dataProvider providerConfig
546
     * @covers       \Interop\Config\ConfigurationTrait::options
547
     * @covers       \Interop\Config\ConfigurationTrait::checkMandatoryOptions
548
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
549
     */
550
    public function testOptionsWithRecursiveArrayIteratorMandatoryOptionCheck($config): void
551
    {
552
        $stub = new ConnectionMandatoryRecursiveArrayIteratorContainerIdConfiguration();
553
554
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
555
        self::assertArrayHasKey('params', $stub->options($config, 'orm_default'));
556
    }
557
558
    /**
559
     * Tests if optionsWithFallback()
560
     *
561
     * @dataProvider providerConfig
562
     * @covers       \Interop\Config\ConfigurationTrait::optionsWithFallback
563
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
564
     */
565
    public function testOptionsWithFallback($config): void
566
    {
567
        $stub = new ConnectionDefaultOptionsMandatoryContainetIdConfiguration();
568
569
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
570
571
        self::assertArrayHasKey('params', $stub->optionsWithFallback([]));
572
        self::assertArrayHasKey('params', $stub->optionsWithFallback($config, 'orm_default'));
573
        self::assertArrayHasKey('driverClass', $stub->optionsWithFallback($config, 'orm_default'));
574
575
        unset($config['doctrine']['connection']['orm_default']['params']);
576
577
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
578
        self::assertArrayHasKey('params', $stub->optionsWithFallback($config));
579
    }
580
581
    /**
582
     * Tests if options() throws a runtime exception if a recursive mandatory option is missing
583
     *
584
     * @covers \Interop\Config\ConfigurationTrait::checkMandatoryOptions
585
     * @covers \Interop\Config\Exception\MandatoryOptionNotFoundException::missingOption
586
     * @covers \Interop\Config\ConfigurationTrait::canRetrieveOptions
587
     */
588
    public function testOptionsThrowsMandatoryOptionNotFoundExceptionIfOptionsAreEmpty(): void
589
    {
590
        $stub = new ConnectionMandatoryRecursiveContainerIdConfiguration();
591
592
        $config = ['doctrine' => ['connection' => ['orm_default' => []]]];
593
594
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
595
596
        $this->assertException(MandatoryOptionNotFoundException::class, 'Mandatory option "params"');
597
598
        $stub->options($config, 'orm_default');
599
    }
600
601
    /**
602
     * Tests if options() works with an empty \ArrayAccess object and default options.
603
     *
604
     * @covers \Interop\Config\ConfigurationTrait::options
605
     */
606
    public function testEmptyArrayAccessWithDefaultOptions()
607
    {
608
        $stub = new ConnectionDefaultOptionsConfiguration();
609
610
        $config = new \ArrayIterator([]);
611
612
        $options = $stub->options($config);
613
614
        self::assertCount(1, $options);
615
        self::assertArrayHasKey('params', $options);
616
617
        self::assertSame(
618
            $options['params']['host'],
619
            'awesomehost'
620
        );
621
        self::assertSame(
622
            $options['params']['port'],
623
            '4444'
624
        );
625
    }
626
627
    /**
628
     * Tests if options() works with iterable objects like \ArrayIterator or \ArrayObject
629
     *
630
     * @dataProvider providerConfigObjects
631
     * @covers       \Interop\Config\ConfigurationTrait::options
632
     * @covers       \Interop\Config\ConfigurationTrait::canRetrieveOptions
633
     */
634
    public function testOptionsWithObjects($config, $type): void
635
    {
636
        $stub = new UniversalContainerIdConfiguration($type);
637
638
        self::assertTrue($stub->canRetrieveOptions($config, 'orm_default'));
639
        $options = $stub->options($config, 'orm_default');
640
641
        self::assertCount(2, $options);
642
        self::assertArrayHasKey('driverClass', $options);
643
        self::assertArrayHasKey('params', $options);
644
645
        $driverClass = 'Doctrine\DBAL\Driver\PDOMySql\Driver';
646
        $host = 'localhost';
647
        $dbname = 'database';
648
        $user = 'username';
649
        $password = 'password';
650
        $port = '4444';
651
652
        if ($type !== UniversalContainerIdConfiguration::TYPE_ONLY_ITERATOR) {
653
            $driverClass = $config['doctrine']['connection']['orm_default']['driverClass'];
654
            $host = $config['doctrine']['connection']['orm_default']['params']['host'];
655
            $dbname = $config['doctrine']['connection']['orm_default']['params']['dbname'];
656
            $user = $config['doctrine']['connection']['orm_default']['params']['user'];
657
            $password = $config['doctrine']['connection']['orm_default']['params']['password'];
658
        }
659
660
        self::assertSame($options['driverClass'], $driverClass);
661
        self::assertSame($options['params']['host'], $host);
662
        self::assertSame($options['params']['port'], $port);
663
        self::assertSame($options['params']['dbname'], $dbname);
664
        self::assertSame($options['params']['user'], $user);
665
        self::assertSame($options['params']['password'], $password);
666
    }
667
668
    public function providerConfig(): array
669
    {
670
        return [
671
            [$this->getTestConfig()],
672
            [new \ArrayObject($this->getTestConfig())],
673
            [new \ArrayIterator($this->getTestConfig())],
674
        ];
675
    }
676
677
    public function providerConfigObjects(): array
678
    {
679
        return [
680
            [$this->getTestConfig(), UniversalContainerIdConfiguration::TYPE_ARRAY_ARRAY],
681
            [new \ArrayObject($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ARRAY_ARRAY],
682
            [new \ArrayIterator($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ARRAY_ARRAY],
683
684
            [$this->getTestConfig(), UniversalContainerIdConfiguration::TYPE_ARRAY_OBJECT],
685
            [new \ArrayObject($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ARRAY_OBJECT],
686
            [new \ArrayIterator($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ARRAY_OBJECT],
687
688
            [$this->getTestConfig(), UniversalContainerIdConfiguration::TYPE_ARRAY_ITERATOR],
689
            [new \ArrayObject($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ARRAY_ITERATOR],
690
            [new \ArrayIterator($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ARRAY_ITERATOR],
691
692
            [$this->getTestConfig(), UniversalContainerIdConfiguration::TYPE_ONLY_ITERATOR],
693
            [new \ArrayObject($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ONLY_ITERATOR],
694
            [new \ArrayIterator($this->getTestConfig()), UniversalContainerIdConfiguration::TYPE_ONLY_ITERATOR],
695
        ];
696
    }
697
698
    /**
699
     * Returns test config
700
     *
701
     * @return array
702
     */
703
    private function getTestConfig(): array
704
    {
705
        // Load the user-defined test configuration file, if it exists; otherwise, load default
706
        if (is_readable('test/TestConfig.php')) {
707
            $config = require 'test/testing.config.php';
708
        } else {
709
            $config = require 'test/testing.config.php.dist';
710
        }
711
712
        return $config;
713
    }
714
715
    /**
716
     * @param $exception
717
     * @param $message
718
     */
719
    private function assertException($exception, $message): void
720
    {
721
        $this->expectException($exception);
722
        $this->expectExceptionMessage($message);
723
    }
724
}
725