17
17
use Magento \Framework \App \Config \Value ;
18
18
use Magento \Framework \App \DeploymentConfig ;
19
19
use Magento \Framework \DB \Adapter \TableNotFoundException ;
20
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
21
use PHPUnit \Framework \MockObject \MockObject ;
21
22
use PHPUnit \Framework \TestCase ;
22
23
23
24
/**
24
25
* Test Class for retrieving runtime configuration from database.
26
+ *
27
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
28
*/
26
29
class RuntimeConfigSourceTest extends TestCase
27
30
{
31
+ /**
32
+ * @var RuntimeConfigSource
33
+ */
34
+ private $ model ;
35
+
28
36
/**
29
37
* @var CollectionFactory|MockObject
30
38
*/
31
- private $ collectionFactory ;
39
+ private $ collectionFactoryMock ;
32
40
33
41
/**
34
42
* @var ScopeCodeResolver|MockObject
35
43
*/
36
- private $ scopeCodeResolver ;
44
+ private $ scopeCodeResolverMock ;
37
45
38
46
/**
39
47
* @var Converter|MockObject
40
48
*/
41
- private $ converter ;
49
+ private $ converterMock ;
42
50
43
51
/**
44
52
* @var Value|MockObject
45
53
*/
46
- private $ configItem ;
54
+ private $ configItemMock ;
47
55
48
56
/**
49
57
* @var Value|MockObject
50
58
*/
51
- private $ configItemTwo ;
59
+ private $ configItemMockTwo ;
52
60
53
- /**
54
- * @var RuntimeConfigSource
55
- */
56
- private $ configSource ;
57
61
/**
58
62
* @var DeploymentConfig|MockObject
59
63
*/
60
- private $ deploymentConfig ;
64
+ private $ deploymentConfigMock ;
61
65
66
+ /**
67
+ * @inheritdoc
68
+ */
62
69
protected function setUp (): void
63
70
{
64
- $ this ->collectionFactory = $ this ->getMockBuilder (CollectionFactory::class)
71
+ $ objectManager = new ObjectManager ($ this );
72
+
73
+ $ this ->collectionFactoryMock = $ this ->getMockBuilder (CollectionFactory::class)
65
74
->disableOriginalConstructor ()
66
- ->setMethods (['create ' ])
67
75
->getMock ();
68
- $ this ->scopeCodeResolver = $ this ->getMockBuilder (ScopeCodeResolver::class)
76
+ $ this ->scopeCodeResolverMock = $ this ->getMockBuilder (ScopeCodeResolver::class)
69
77
->disableOriginalConstructor ()
70
78
->getMock ();
71
- $ this ->converter = $ this ->getMockBuilder (Converter::class)
79
+ $ this ->converterMock = $ this ->getMockBuilder (Converter::class)
72
80
->disableOriginalConstructor ()
73
81
->getMock ();
74
- $ this ->configItem = $ this ->getMockBuilder (Value::class)
82
+ $ this ->configItemMock = $ this ->getMockBuilder (Value::class)
75
83
->disableOriginalConstructor ()
76
- ->setMethods (['getScope ' , 'getPath ' , 'getValue ' ])
84
+ ->addMethods (['getScope ' , 'getPath ' , 'getValue ' ])
77
85
->getMock ();
78
- $ this ->configItemTwo = $ this ->getMockBuilder (Value::class)
86
+ $ this ->configItemMockTwo = $ this ->getMockBuilder (Value::class)
79
87
->disableOriginalConstructor ()
80
- ->setMethods (['getScope ' , 'getPath ' , 'getValue ' , 'getScopeId ' ])
88
+ ->addMethods (['getScope ' , 'getPath ' , 'getValue ' , 'getScopeId ' ])
81
89
->getMock ();
82
- $ this ->deploymentConfig = $ this ->createPartialMock (DeploymentConfig::class, ['isDbAvailable ' ]);
83
- $ this ->configSource = new RuntimeConfigSource (
84
- $ this ->collectionFactory ,
85
- $ this ->scopeCodeResolver ,
86
- $ this ->converter ,
87
- $ this ->deploymentConfig
90
+ $ this ->deploymentConfigMock = $ this ->createPartialMock (
91
+ DeploymentConfig::class,
92
+ ['isDbAvailable ' ]
93
+ );
94
+ $ this ->model = $ objectManager ->getObject (
95
+ RuntimeConfigSource::class,
96
+ [
97
+ 'collectionFactory ' => $ this ->collectionFactoryMock ,
98
+ 'scopeCodeResolver ' => $ this ->scopeCodeResolverMock ,
99
+ 'converter ' => $ this ->converterMock ,
100
+ 'deploymentConfig ' => $ this ->deploymentConfigMock ,
101
+ ]
88
102
);
89
103
}
90
104
91
- public function testGet ()
105
+ /**
106
+ * Test get initial data.
107
+ *
108
+ * @return void
109
+ */
110
+ public function testGet (): void
92
111
{
93
- $ this ->deploymentConfig ->method ('isDbAvailable ' )
112
+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
113
+ ->method ('isDbAvailable ' )
94
114
->willReturn (true );
95
115
$ collection = $ this ->createPartialMock (Collection::class, ['load ' , 'getIterator ' ]);
96
- $ collection ->method ('load ' )
116
+ $ collection ->expects ($ this ->once ())
117
+ ->method ('load ' )
97
118
->willReturn ($ collection );
98
- $ collection ->method ('getIterator ' )
99
- ->willReturn (new ArrayIterator ([$ this ->configItem , $ this ->configItemTwo ]));
119
+ $ collection ->expects ($ this ->once ())
120
+ ->method ('getIterator ' )
121
+ ->willReturn (new ArrayIterator ([$ this ->configItemMock , $ this ->configItemMockTwo ]));
100
122
$ scope = 'websites ' ;
101
123
$ scopeCode = 'myWebsites ' ;
102
- $ this ->collectionFactory ->expects ($ this ->once ())
124
+ $ this ->collectionFactoryMock ->expects ($ this ->once ())
103
125
->method ('create ' )
104
126
->willReturn ($ collection );
105
- $ this ->configItem ->expects ($ this ->exactly (2 ))
127
+ $ this ->configItemMock ->expects ($ this ->exactly (2 ))
106
128
->method ('getScope ' )
107
129
->willReturn (ScopeConfigInterface::SCOPE_TYPE_DEFAULT );
108
- $ this ->configItem ->expects ($ this ->once ())
130
+ $ this ->configItemMock ->expects ($ this ->once ())
109
131
->method ('getPath ' )
110
132
->willReturn ('dev/test/setting ' );
111
- $ this ->configItem ->expects ($ this ->once ())
133
+ $ this ->configItemMock ->expects ($ this ->once ())
112
134
->method ('getValue ' )
113
135
->willReturn (true );
114
136
115
- $ this ->configItemTwo ->expects ($ this ->exactly (3 ))
137
+ $ this ->configItemMockTwo ->expects ($ this ->exactly (3 ))
116
138
->method ('getScope ' )
117
139
->willReturn ($ scope );
118
- $ this ->configItemTwo ->expects ($ this ->once ())
140
+ $ this ->configItemMockTwo ->expects ($ this ->once ())
119
141
->method ('getScopeId ' )
120
142
->willReturn ($ scopeCode );
121
- $ this ->configItemTwo ->expects ($ this ->once ())
143
+ $ this ->configItemMockTwo ->expects ($ this ->once ())
122
144
->method ('getPath ' )
123
145
->willReturn ('dev/test/setting2 ' );
124
- $ this ->configItemTwo ->expects ($ this ->once ())
146
+ $ this ->configItemMockTwo ->expects ($ this ->once ())
125
147
->method ('getValue ' )
126
148
->willReturn (false );
127
- $ this ->scopeCodeResolver ->expects ($ this ->once ())
149
+ $ this ->scopeCodeResolverMock ->expects ($ this ->once ())
128
150
->method ('resolve ' )
129
151
->with ($ scope , $ scopeCode )
130
152
->willReturnArgument (1 );
131
- $ this ->converter ->expects ($ this ->exactly (2 ))
153
+ $ this ->converterMock ->expects ($ this ->exactly (2 ))
132
154
->method ('convert ' )
133
155
->withConsecutive (
134
156
[['dev/test/setting ' => true ]],
@@ -150,25 +172,96 @@ public function testGet()
150
172
]
151
173
]
152
174
],
153
- $ this ->configSource ->get ()
175
+ $ this ->model ->get ()
154
176
);
155
177
}
156
178
157
- public function testGetWhenDbIsNotAvailable ()
179
+ /**
180
+ * Test get with not available db
181
+ *
182
+ * @return void
183
+ */
184
+ public function testGetWhenDbIsNotAvailable (): void
158
185
{
159
- $ this ->deploymentConfig ->method ('isDbAvailable ' )->willReturn (false );
160
- $ this ->assertEquals ([], $ this ->configSource ->get ());
186
+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
187
+ ->method ('isDbAvailable ' )
188
+ ->willReturn (false );
189
+ $ this ->assertEquals ([], $ this ->model ->get ());
161
190
}
162
191
163
- public function testGetWhenDbIsEmpty ()
192
+ /**
193
+ * Test get with empty db
194
+ *
195
+ * @return void
196
+ */
197
+ public function testGetWhenDbIsEmpty (): void
164
198
{
165
- $ this ->deploymentConfig ->method ('isDbAvailable ' )
199
+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
200
+ ->method ('isDbAvailable ' )
166
201
->willReturn (true );
167
202
$ collection = $ this ->createPartialMock (Collection::class, ['load ' ]);
168
- $ collection ->method ('load ' )
203
+ $ collection ->expects ($ this ->once ())
204
+ ->method ('load ' )
169
205
->willThrowException ($ this ->createMock (TableNotFoundException::class));
170
- $ this ->collectionFactory ->method ('create ' )
206
+ $ this ->collectionFactoryMock ->expects ($ this ->once ())
207
+ ->method ('create ' )
171
208
->willReturn ($ collection );
172
- $ this ->assertEquals ([], $ this ->configSource ->get ());
209
+
210
+ $ this ->assertEquals ([], $ this ->model ->get ());
211
+ }
212
+
213
+ /**
214
+ * Test get value for specified config
215
+ *
216
+ * @dataProvider configDataProvider
217
+ *
218
+ * @param string $path
219
+ * @param array $configData
220
+ * @param string $expectedResult
221
+ * @return void
222
+ */
223
+ public function testGetConfigValue (string $ path , array $ configData , string $ expectedResult ): void
224
+ {
225
+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
226
+ ->method ('isDbAvailable ' )
227
+ ->willReturn (true );
228
+
229
+ $ collection = $ this ->createPartialMock (Collection::class, ['load ' , 'getIterator ' ]);
230
+ $ collection ->expects ($ this ->once ())
231
+ ->method ('load ' )
232
+ ->willReturn ($ collection );
233
+ $ collection ->expects ($ this ->once ())
234
+ ->method ('getIterator ' )
235
+ ->willReturn (new ArrayIterator ([$ this ->configItemMock ]));
236
+
237
+ $ this ->collectionFactoryMock ->expects ($ this ->once ())
238
+ ->method ('create ' )
239
+ ->willReturn ($ collection );
240
+
241
+ $ this ->configItemMock ->expects ($ this ->exactly (2 ))
242
+ ->method ('getScope ' )
243
+ ->willReturn (ScopeConfigInterface::SCOPE_TYPE_DEFAULT );
244
+ $ this ->configItemMock ->expects ($ this ->once ())
245
+ ->method ('getPath ' )
246
+ ->willReturn ($ path );
247
+
248
+ $ this ->converterMock ->expects ($ this ->once ())
249
+ ->method ('convert ' )
250
+ ->willReturn ($ configData );
251
+
252
+ $ this ->assertEquals ($ expectedResult , $ this ->model ->get ($ path ));
253
+ }
254
+
255
+ /**
256
+ * DataProvider for testGetConfigValue
257
+ *
258
+ * @return array
259
+ */
260
+ public function configDataProvider (): array
261
+ {
262
+ return [
263
+ 'config value 0 ' => ['default/test/option ' , ['test ' => ['option ' => 0 ]], '0 ' ],
264
+ 'config value blank ' => ['default/test/option ' , ['test ' => ['option ' => '' ]], '' ],
265
+ ];
173
266
}
174
267
}
0 commit comments