1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace BaleenTest\Cli\Config; |
21
|
|
|
|
22
|
|
|
use Baleen\Cli\Config\Config; |
23
|
|
|
use Baleen\Cli\Config\ConfigStorage; |
24
|
|
|
use BaleenTest\Cli\BaseTestCase; |
25
|
|
|
use League\Flysystem\FilesystemInterface; |
26
|
|
|
use Mockery as m; |
27
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class ConfigStorageTest |
31
|
|
|
* @author Gabriel Somoza <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class ConfigStorageTest extends BaseTestCase |
34
|
|
|
{ |
35
|
|
|
/** @var FilesystemInterface|m\Mock */ |
36
|
|
|
protected $filesystem; |
37
|
|
|
|
38
|
|
|
/** @var ConfigStorage|m\Mock $instance */ |
39
|
|
|
protected $config; |
40
|
|
|
|
41
|
|
|
/** @var ConfigStorage|m\Mock $instance */ |
42
|
|
|
protected $instance; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* setUP |
46
|
|
|
*/ |
47
|
|
|
public function setUp() |
48
|
|
|
{ |
49
|
|
|
parent::setUp(); |
50
|
|
|
$this->filesystem = m::mock(FilesystemInterface::class); |
51
|
|
|
$this->instance = m::mock(ConfigStorage::class)->makePartial(); |
52
|
|
|
$this->config = m::mock(Config::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* tearDown |
57
|
|
|
*/ |
58
|
|
|
public function tearDown() |
59
|
|
|
{ |
60
|
|
|
parent::tearDown(); |
61
|
|
|
$this->filesystem = null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* testConstructor |
66
|
|
|
*/ |
67
|
|
|
public function testConstructor() |
68
|
|
|
{ |
69
|
|
|
$instance = new ConfigStorage(Config::class, $this->filesystem); |
70
|
|
|
$this->assertInstanceOf(ConfigStorage::class, $instance); |
71
|
|
|
$this->assertSame($this->filesystem, $this->getPropVal('projectFileSystem', $instance)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* testRead |
76
|
|
|
* @dataProvider yamlFixtures |
77
|
|
|
*/ |
78
|
|
|
public function testRead($config, $validConfig) |
79
|
|
|
{ |
80
|
|
|
if (!$validConfig) { |
81
|
|
|
$this->setExpectedException(InvalidConfigurationException::class); |
82
|
|
|
} |
83
|
|
|
$this->doRead($config, null, true); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* testReadWithoutFile |
88
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
89
|
|
|
*/ |
90
|
|
|
public function testReadWithoutFile() |
91
|
|
|
{ |
92
|
|
|
$this->doRead('irrelevant', 'doesnt exist', false); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* doRead |
97
|
|
|
* |
98
|
|
|
* @param $yamlConfig |
99
|
|
|
* @param null|string $file |
100
|
|
|
* @param bool $fileExists |
101
|
|
|
* @throws \Baleen\Cli\Exception\CliException |
102
|
|
|
*/ |
103
|
|
|
public function doRead($yamlConfig, $file = null, $fileExists = true) |
104
|
|
|
{ |
105
|
|
|
$expectedFile = $file ?: Config::CONFIG_FILE_NAME; |
106
|
|
|
|
107
|
|
|
$instance = new ConfigStorage(Config::class, $this->filesystem); |
108
|
|
|
$this->filesystem->shouldReceive('has')->with(m::type('string'))->once()->andReturn($fileExists); |
109
|
|
|
|
110
|
|
|
if ($fileExists) { |
111
|
|
|
$this->filesystem->shouldReceive('read')->with($expectedFile)->once()->andReturn($yamlConfig); |
112
|
|
|
} else { |
113
|
|
|
$this->filesystem->shouldNotReceive('read'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$result = $instance->load($file); |
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf(Config::class, $result); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Provides sample YAML data |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function yamlFixtures() |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
["test: that\nfoo: bar", false], |
129
|
|
|
["providers: { foo: bar }\nmigrations: { directory: migrations, namespace: Migrations }\nstorage: { file: .baleen_versions }", true], |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $file |
135
|
|
|
* @dataProvider writeProvider |
136
|
|
|
*/ |
137
|
|
|
public function testWrite($file) |
138
|
|
|
{ |
139
|
|
|
$configToArray = ['foo' => 'bar']; |
140
|
|
|
$this->config->shouldReceive('getCleanArray')->once()->andReturn($configToArray); |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
$expectedFile = null === $file ? Config::CONFIG_FILE_NAME : $file; |
144
|
|
|
$this->config->shouldReceive('getFileName')->once()->andReturn($file); |
145
|
|
|
|
146
|
|
|
$expectedResult = 123; // value doesn't really matter, all we have to do is check that its the same |
147
|
|
|
$this->filesystem |
148
|
|
|
->shouldReceive('write') |
149
|
|
|
->once() |
150
|
|
|
->with($expectedFile, m::type('string')) |
151
|
|
|
->andReturn($expectedResult); |
152
|
|
|
|
153
|
|
|
$this->setPropVal('projectFileSystem', $this->filesystem, $this->instance); |
154
|
|
|
|
155
|
|
|
$result = $this->instance->write($this->config); |
156
|
|
|
|
157
|
|
|
$this->assertSame($expectedResult, $result); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function writeProvider() |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
[null, 'some/file'], |
167
|
|
|
['another/file'], |
168
|
|
|
['another/file', null, false], |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $pathOrConfig |
174
|
|
|
* @param null $fsResult |
175
|
|
|
* @dataProvider isInitializedProvider |
176
|
|
|
*/ |
177
|
|
|
public function testIsInitialized($pathOrConfig, $fsResult = null) |
178
|
|
|
{ |
179
|
|
|
$expectedResult = $fsResult; |
180
|
|
|
$this->setPropVal('projectFileSystem', $this->filesystem, $this->instance); |
181
|
|
|
if (null !== $fsResult) { |
182
|
|
|
$this->filesystem->shouldReceive('has')->with(m::type('string'))->once()->andReturn($fsResult); |
183
|
|
|
} else { |
184
|
|
|
$expectedResult = false; |
185
|
|
|
$this->filesystem->shouldNotReceive('has'); |
186
|
|
|
} |
187
|
|
|
$result = $this->instance->isInitialized($pathOrConfig); |
188
|
|
|
$this->assertSame($expectedResult, $result); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function isInitializedProvider() |
192
|
|
|
{ |
193
|
|
|
/** @var m\Mock $configMock1 */ |
194
|
|
|
$configMock1 = m::mock(Config::class); |
195
|
|
|
$configMock1->shouldReceive('getFileName')->andReturn('some/file'); |
196
|
|
|
/** @var m\Mock $configMock2 */ |
197
|
|
|
$configMock2 = m::mock(Config::class); |
198
|
|
|
$configMock2->shouldReceive('getFileName')->andReturn('some/inexistent/file'); |
199
|
|
|
return [ |
200
|
|
|
[$configMock1, true], |
201
|
|
|
[$configMock2, false], |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|