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

ConfigReaderTest::itDisplaysConfigFromFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

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 5
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\Exception\OptionNotFoundException;
13
use Interop\Config\Tool\ConfigReader;
14
use Interop\Config\Tool\ConsoleHelper;
15
use InteropTest\Config\TestAsset;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @covers \Interop\Config\Tool\AbstractConfig
20
 * @covers \Interop\Config\Tool\ConfigReader
21
 * @covers \Interop\Config\Tool\ConsoleHelper
22
 */
23
class ConfigReaderTest extends TestCase
24
{
25
    /**
26
     * @var resource Exists only for testing.
27
     */
28
    private $errorStream = STDERR;
29
30
    /**
31
     * Input stream
32
     *
33
     * @var resource
34
     */
35
    private $inputStream;
36
37
    /**
38
     * Output stream
39
     *
40
     * @var resource
41
     */
42
    private $outputStream;
43
44
    /**
45
     * Console Helper
46
     *
47
     * @var ConsoleHelper
48
     */
49
    private $consoleHelper;
50
51
52
    public function setUp()
53
    {
54
        parent::setUp();
55
56
        if (!stream_wrapper_register("test", \InteropTest\Config\TestAsset\TestStream::class)) {
57
            throw new \RuntimeException('Failed to register protocol');
58
        }
59
60
        $this->inputStream = fopen('test://input', 'r+', false);
61
        $this->outputStream = fopen('test://output', 'r+', false);
62
        $this->errorStream = fopen('test://error', 'r+', false);
63
        $this->consoleHelper = new ConsoleHelper($this->inputStream, $this->outputStream, $this->errorStream);
64
    }
65
66
    public function tearDown()
67
    {
68
        stream_wrapper_unregister('test');
69
        TestAsset\TestStream::$inputStack = [];
70
        TestAsset\TestStream::$data= [];
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function itDisplaysConfigFromFactory()
77
    {
78
        $cut = new ConfigReader($this->consoleHelper);
79
80
        $fullConfig = $this->getTestConfig();
81
82
        $config = $cut->readConfig($fullConfig, TestAsset\ConnectionConfiguration::class);
83
84
        self::assertSame($fullConfig['doctrine']['connection'], $config);
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function itDisplaysConfigFromFactoryByConfigId()
91
    {
92
        TestAsset\TestStream::$inputStack = ['unknown', 'orm_default'];
93
        $cut = new ConfigReader($this->consoleHelper);
94
95
        $fullConfig = $this->getTestConfig();
96
97
        $config = $cut->readConfig($fullConfig, TestAsset\UniversalContainerIdConfiguration::class);
98
99
        self::assertSame('No config id with name "unknown" exists.', trim(TestAsset\TestStream::$data['error']));
100
        self::assertSame(
101
            'For which config id orm_default, orm_second: For which config id orm_default, orm_second:',
102
            trim(TestAsset\TestStream::$data['output'])
103
        );
104
        self::assertSame($fullConfig['doctrine']['universal']['orm_default'], $config);
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function itDisplaysConfigFromFactoryForAllConfigIds()
111
    {
112
        TestAsset\TestStream::$inputStack = [''];
113
        $cut = new ConfigReader($this->consoleHelper);
114
115
        $fullConfig = $this->getTestConfig();
116
117
        $config = $cut->readConfig($fullConfig, TestAsset\UniversalContainerIdConfiguration::class);
118
119
        self::assertSame('For which config id orm_default, orm_second:', trim(TestAsset\TestStream::$data['output']));
120
        self::assertSame($fullConfig['doctrine']['universal'], $config);
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function itThrowsOptionNotFoundException()
127
    {
128
        $cut = new ConfigReader($this->consoleHelper);
129
130
        $this->expectException(OptionNotFoundException::class);
131
132
        $cut->readConfig([], TestAsset\ConnectionConfiguration::class);
133
    }
134
135
    /**
136
     * Returns test config
137
     *
138
     * @return array
139
     */
140
    private function getTestConfig(): array
141
    {
142
        // Load the user-defined test configuration file, if it exists; otherwise, load default
143
        if (is_readable('test/TestConfig.php')) {
144
            $config = require 'test/testing.config.php';
145
        } else {
146
            $config = require 'test/testing.config.php.dist';
147
        }
148
149
        return $config;
150
    }
151
}
152