|
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\ConfigReader; |
|
13
|
|
|
use Interop\Config\Tool\ConfigReaderCommand; |
|
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\ConfigReaderCommand |
|
21
|
|
|
* @covers \Interop\Config\Tool\ConsoleHelper |
|
22
|
|
|
*/ |
|
23
|
|
|
class ConfigReaderCommandTest 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
|
|
|
file_put_contents(self::CONFIG_FILE, $this->getTestConfig()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function tearDown() |
|
71
|
|
|
{ |
|
72
|
|
|
stream_wrapper_unregister('test'); |
|
73
|
|
|
TestAsset\TestStream::$inputStack = []; |
|
74
|
|
|
TestAsset\TestStream::$data = []; |
|
75
|
|
|
|
|
76
|
|
|
unlink(self::CONFIG_FILE); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function itReadsConfigByConfigId() |
|
83
|
|
|
{ |
|
84
|
|
|
|
|
85
|
|
|
$expectedOutput = <<<EOF |
|
86
|
|
|
For which config id orm_default, orm_second: [ |
|
87
|
|
|
'driverClass' => 'Doctrine\\\DBAL\\\Driver\\\PDOMySql\\\Driver', |
|
88
|
|
|
'params' => [ |
|
89
|
|
|
'host' => 'localhost', |
|
90
|
|
|
'port' => 3306, |
|
91
|
|
|
'user' => 'username', |
|
92
|
|
|
'password' => 'password', |
|
93
|
|
|
'dbname' => 'database', |
|
94
|
|
|
], |
|
95
|
|
|
] |
|
96
|
|
|
|
|
97
|
|
|
EOF; |
|
98
|
|
|
TestAsset\TestStream::$inputStack = ['orm_second']; |
|
99
|
|
|
$argv = [self::CONFIG_FILE, TestAsset\UniversalContainerIdConfiguration::class]; |
|
100
|
|
|
|
|
101
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
102
|
|
|
|
|
103
|
|
|
$cut($argv); |
|
104
|
|
|
self::assertSame($expectedOutput, TestAsset\TestStream::$data['output']); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function providerMultipleConfigIds() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
[self::CONFIG_FILE], |
|
111
|
|
|
[__DIR__ . '/_files/iterator.php'], |
|
112
|
|
|
[__DIR__ . '/_files/iterator_aggregate.php'], |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @test |
|
118
|
|
|
* @dataProvider providerMultipleConfigIds |
|
119
|
|
|
*/ |
|
120
|
|
|
public function itReadsConfigMultipleConfigIds($file) |
|
121
|
|
|
{ |
|
122
|
|
|
$expectedOutput = <<<EOF |
|
123
|
|
|
For which config id orm_default, orm_second: [ |
|
124
|
|
|
'orm_default' => [ |
|
125
|
|
|
'driverClass' => 'Doctrine\\\DBAL\\\Driver\\\PDOMySql\\\Driver', |
|
126
|
|
|
'params' => [ |
|
127
|
|
|
'host' => 'localhost', |
|
128
|
|
|
'user' => 'username', |
|
129
|
|
|
'password' => 'password', |
|
130
|
|
|
'dbname' => 'database', |
|
131
|
|
|
], |
|
132
|
|
|
], |
|
133
|
|
|
'orm_second' => [ |
|
134
|
|
|
'driverClass' => 'Doctrine\\\DBAL\\\Driver\\\PDOMySql\\\Driver', |
|
135
|
|
|
'params' => [ |
|
136
|
|
|
'host' => 'localhost', |
|
137
|
|
|
'port' => 3306, |
|
138
|
|
|
'user' => 'username', |
|
139
|
|
|
'password' => 'password', |
|
140
|
|
|
'dbname' => 'database', |
|
141
|
|
|
], |
|
142
|
|
|
], |
|
143
|
|
|
] |
|
144
|
|
|
|
|
145
|
|
|
EOF; |
|
146
|
|
|
|
|
147
|
|
|
TestAsset\TestStream::$inputStack = ['']; |
|
148
|
|
|
$argv = [$file, TestAsset\UniversalContainerIdConfiguration::class]; |
|
149
|
|
|
|
|
150
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
151
|
|
|
|
|
152
|
|
|
$cut($argv); |
|
153
|
|
|
self::assertSame($expectedOutput, TestAsset\TestStream::$data['output']); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @test |
|
158
|
|
|
*/ |
|
159
|
|
|
public function itDisplaysHelp() |
|
160
|
|
|
{ |
|
161
|
|
|
$argv = ['help']; |
|
162
|
|
|
|
|
163
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
164
|
|
|
|
|
165
|
|
|
$cut($argv); |
|
166
|
|
|
|
|
167
|
|
|
self::assertStringStartsWith('Usage:', TestAsset\TestStream::$data['error']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @test |
|
172
|
|
|
*/ |
|
173
|
|
|
public function itDisplaysHelpIfNoArguments() |
|
174
|
|
|
{ |
|
175
|
|
|
$argv = []; |
|
176
|
|
|
|
|
177
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
178
|
|
|
|
|
179
|
|
|
$cut($argv); |
|
180
|
|
|
|
|
181
|
|
|
self::assertStringStartsWith('Usage:', TestAsset\TestStream::$data['error']); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @test |
|
186
|
|
|
*/ |
|
187
|
|
|
public function itDisplaysError() |
|
188
|
|
|
{ |
|
189
|
|
|
$argv = ['wrong']; |
|
190
|
|
|
|
|
191
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
192
|
|
|
|
|
193
|
|
|
$cut($argv); |
|
194
|
|
|
|
|
195
|
|
|
self::assertStringStartsWith('Missing class name', TestAsset\TestStream::$data['error']); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @test |
|
200
|
|
|
*/ |
|
201
|
|
|
public function itDisplaysErrorIfClassNotExists() |
|
202
|
|
|
{ |
|
203
|
|
|
$argv = [self::CONFIG_FILE, 'UnknownClassName']; |
|
204
|
|
|
|
|
205
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
206
|
|
|
|
|
207
|
|
|
$cut($argv); |
|
208
|
|
|
|
|
209
|
|
|
self::assertStringStartsWith('Class "UnknownClassName"', TestAsset\TestStream::$data['error']); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @test |
|
214
|
|
|
*/ |
|
215
|
|
|
public function itDisplaysErrorIfFileIsNotReadable() |
|
216
|
|
|
{ |
|
217
|
|
|
$argv = ['/unknown/place/config.php', TestAsset\UniversalContainerIdConfiguration::class]; |
|
218
|
|
|
|
|
219
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
220
|
|
|
|
|
221
|
|
|
$cut($argv); |
|
222
|
|
|
|
|
223
|
|
|
self::assertStringStartsWith('Cannot read configuration', TestAsset\TestStream::$data['error']); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @test |
|
228
|
|
|
*/ |
|
229
|
|
|
public function itDisplaysErrorIfClassDoesNotImplementRequiresConfig() |
|
230
|
|
|
{ |
|
231
|
|
|
$argv = [self::CONFIG_FILE, TestAsset\TestStream::class]; |
|
232
|
|
|
|
|
233
|
|
|
$cut = new ConfigReaderCommand($this->consoleHelper, new ConfigReader($this->consoleHelper)); |
|
234
|
|
|
|
|
235
|
|
|
$cut($argv); |
|
236
|
|
|
|
|
237
|
|
|
self::assertStringStartsWith( |
|
238
|
|
|
'Class "InteropTest\Config\TestAsset\TestStream" does not implement', |
|
239
|
|
|
TestAsset\TestStream::$data['error'] |
|
240
|
|
|
); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Returns test config |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
|
|
private function getTestConfig(): string |
|
249
|
|
|
{ |
|
250
|
|
|
// Load the user-defined test configuration file, if it exists; otherwise, load default |
|
251
|
|
|
if (is_readable('test/TestConfig.php')) { |
|
252
|
|
|
$config = file_get_contents('test/testing.config.php'); |
|
253
|
|
|
} else { |
|
254
|
|
|
$config = file_get_contents('test/testing.config.php.dist'); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $config; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|