1717use Magento \Framework \App \Config \Value ;
1818use Magento \Framework \App \DeploymentConfig ;
1919use Magento \Framework \DB \Adapter \TableNotFoundException ;
20+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
2021use PHPUnit \Framework \MockObject \MockObject ;
2122use PHPUnit \Framework \TestCase ;
2223
2324/**
2425 * Test Class for retrieving runtime configuration from database.
26+ *
27+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528 */
2629class RuntimeConfigSourceTest extends TestCase
2730{
31+ /**
32+ * @var RuntimeConfigSource
33+ */
34+ private $ model ;
35+
2836 /**
2937 * @var CollectionFactory|MockObject
3038 */
31- private $ collectionFactory ;
39+ private $ collectionFactoryMock ;
3240
3341 /**
3442 * @var ScopeCodeResolver|MockObject
3543 */
36- private $ scopeCodeResolver ;
44+ private $ scopeCodeResolverMock ;
3745
3846 /**
3947 * @var Converter|MockObject
4048 */
41- private $ converter ;
49+ private $ converterMock ;
4250
4351 /**
4452 * @var Value|MockObject
4553 */
46- private $ configItem ;
54+ private $ configItemMock ;
4755
4856 /**
4957 * @var Value|MockObject
5058 */
51- private $ configItemTwo ;
59+ private $ configItemMockTwo ;
5260
53- /**
54- * @var RuntimeConfigSource
55- */
56- private $ configSource ;
5761 /**
5862 * @var DeploymentConfig|MockObject
5963 */
60- private $ deploymentConfig ;
64+ private $ deploymentConfigMock ;
6165
66+ /**
67+ * @inheritdoc
68+ */
6269 protected function setUp (): void
6370 {
64- $ this ->collectionFactory = $ this ->getMockBuilder (CollectionFactory::class)
71+ $ objectManager = new ObjectManager ($ this );
72+
73+ $ this ->collectionFactoryMock = $ this ->getMockBuilder (CollectionFactory::class)
6574 ->disableOriginalConstructor ()
66- ->setMethods (['create ' ])
6775 ->getMock ();
68- $ this ->scopeCodeResolver = $ this ->getMockBuilder (ScopeCodeResolver::class)
76+ $ this ->scopeCodeResolverMock = $ this ->getMockBuilder (ScopeCodeResolver::class)
6977 ->disableOriginalConstructor ()
7078 ->getMock ();
71- $ this ->converter = $ this ->getMockBuilder (Converter::class)
79+ $ this ->converterMock = $ this ->getMockBuilder (Converter::class)
7280 ->disableOriginalConstructor ()
7381 ->getMock ();
74- $ this ->configItem = $ this ->getMockBuilder (Value::class)
82+ $ this ->configItemMock = $ this ->getMockBuilder (Value::class)
7583 ->disableOriginalConstructor ()
76- ->setMethods (['getScope ' , 'getPath ' , 'getValue ' ])
84+ ->addMethods (['getScope ' , 'getPath ' , 'getValue ' ])
7785 ->getMock ();
78- $ this ->configItemTwo = $ this ->getMockBuilder (Value::class)
86+ $ this ->configItemMockTwo = $ this ->getMockBuilder (Value::class)
7987 ->disableOriginalConstructor ()
80- ->setMethods (['getScope ' , 'getPath ' , 'getValue ' , 'getScopeId ' ])
88+ ->addMethods (['getScope ' , 'getPath ' , 'getValue ' , 'getScopeId ' ])
8189 ->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+ ]
88102 );
89103 }
90104
91- public function testGet ()
105+ /**
106+ * Test get initial data.
107+ *
108+ * @return void
109+ */
110+ public function testGet (): void
92111 {
93- $ this ->deploymentConfig ->method ('isDbAvailable ' )
112+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
113+ ->method ('isDbAvailable ' )
94114 ->willReturn (true );
95115 $ collection = $ this ->createPartialMock (Collection::class, ['load ' , 'getIterator ' ]);
96- $ collection ->method ('load ' )
116+ $ collection ->expects ($ this ->once ())
117+ ->method ('load ' )
97118 ->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 ]));
100122 $ scope = 'websites ' ;
101123 $ scopeCode = 'myWebsites ' ;
102- $ this ->collectionFactory ->expects ($ this ->once ())
124+ $ this ->collectionFactoryMock ->expects ($ this ->once ())
103125 ->method ('create ' )
104126 ->willReturn ($ collection );
105- $ this ->configItem ->expects ($ this ->exactly (2 ))
127+ $ this ->configItemMock ->expects ($ this ->exactly (2 ))
106128 ->method ('getScope ' )
107129 ->willReturn (ScopeConfigInterface::SCOPE_TYPE_DEFAULT );
108- $ this ->configItem ->expects ($ this ->once ())
130+ $ this ->configItemMock ->expects ($ this ->once ())
109131 ->method ('getPath ' )
110132 ->willReturn ('dev/test/setting ' );
111- $ this ->configItem ->expects ($ this ->once ())
133+ $ this ->configItemMock ->expects ($ this ->once ())
112134 ->method ('getValue ' )
113135 ->willReturn (true );
114136
115- $ this ->configItemTwo ->expects ($ this ->exactly (3 ))
137+ $ this ->configItemMockTwo ->expects ($ this ->exactly (3 ))
116138 ->method ('getScope ' )
117139 ->willReturn ($ scope );
118- $ this ->configItemTwo ->expects ($ this ->once ())
140+ $ this ->configItemMockTwo ->expects ($ this ->once ())
119141 ->method ('getScopeId ' )
120142 ->willReturn ($ scopeCode );
121- $ this ->configItemTwo ->expects ($ this ->once ())
143+ $ this ->configItemMockTwo ->expects ($ this ->once ())
122144 ->method ('getPath ' )
123145 ->willReturn ('dev/test/setting2 ' );
124- $ this ->configItemTwo ->expects ($ this ->once ())
146+ $ this ->configItemMockTwo ->expects ($ this ->once ())
125147 ->method ('getValue ' )
126148 ->willReturn (false );
127- $ this ->scopeCodeResolver ->expects ($ this ->once ())
149+ $ this ->scopeCodeResolverMock ->expects ($ this ->once ())
128150 ->method ('resolve ' )
129151 ->with ($ scope , $ scopeCode )
130152 ->willReturnArgument (1 );
131- $ this ->converter ->expects ($ this ->exactly (2 ))
153+ $ this ->converterMock ->expects ($ this ->exactly (2 ))
132154 ->method ('convert ' )
133155 ->withConsecutive (
134156 [['dev/test/setting ' => true ]],
@@ -150,25 +172,97 @@ public function testGet()
150172 ]
151173 ]
152174 ],
153- $ this ->configSource ->get ()
175+ $ this ->model ->get ()
154176 );
155177 }
156178
157- public function testGetWhenDbIsNotAvailable ()
179+ /**
180+ * Test get with not available db
181+ *
182+ * @return void
183+ */
184+ public function testGetWhenDbIsNotAvailable (): void
158185 {
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 ());
161190 }
162191
163- public function testGetWhenDbIsEmpty ()
192+ /**
193+ * Test get with empty db
194+ *
195+ * @return void
196+ */
197+ public function testGetWhenDbIsEmpty (): void
164198 {
165- $ this ->deploymentConfig ->method ('isDbAvailable ' )
199+ $ this ->deploymentConfigMock ->expects ($ this ->once ())
200+ ->method ('isDbAvailable ' )
166201 ->willReturn (true );
167202 $ collection = $ this ->createPartialMock (Collection::class, ['load ' ]);
168- $ collection ->method ('load ' )
203+ $ collection ->expects ($ this ->once ())
204+ ->method ('load ' )
169205 ->willThrowException ($ this ->createMock (TableNotFoundException::class));
170- $ this ->collectionFactory ->method ('create ' )
206+ $ this ->collectionFactoryMock ->expects ($ this ->once ())
207+ ->method ('create ' )
171208 ->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+ 'config value null ' => ['default/test/option ' , ['test ' => ['option ' => null ]], '' ],
266+ ];
173267 }
174268}
0 commit comments