Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PreparedValueFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Config\Model\Config\StructureFactory;
17 use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
21 use PHPUnit_Framework_MockObject_MockObject as Mock;
22 
28 class PreparedValueFactoryTest extends \PHPUnit\Framework\TestCase
29 {
33  private $structureFactoryMock;
34 
38  private $valueFactoryMock;
39 
43  private $valueMock;
44 
48  private $structureMock;
49 
53  private $fieldMock;
54 
58  private $configMock;
59 
63  private $scopeResolverPoolMock;
64 
68  private $scopeResolverMock;
69 
73  private $scopeMock;
74 
78  private $scopeTypeNormalizer;
79 
83  private $preparedValueFactory;
84 
88  protected function setUp()
89  {
90  $this->structureFactoryMock = $this->getMockBuilder(StructureFactory::class)
91  ->disableOriginalConstructor()
92  ->setMethods(['create'])
93  ->getMock();
94  $this->valueFactoryMock = $this->getMockBuilder(BackendFactory::class)
95  ->disableOriginalConstructor()
96  ->setMethods(['create'])
97  ->getMock();
98  $this->structureMock = $this->getMockBuilder(Structure::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101  $this->fieldMock = $this->getMockBuilder(Field::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104  $this->valueMock = $this->getMockBuilder(Value::class)
105  ->disableOriginalConstructor()
106  ->setMethods([
107  'setPath', 'setScope', 'setScopeId', 'setValue', 'setField',
108  'setGroupId', 'setFieldConfig', 'setScopeCode'
109  ])
110  ->getMock();
111  $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
112  ->getMockForAbstractClass();
113  $this->scopeResolverPoolMock = $this->getMockBuilder(ScopeResolverPool::class)
114  ->disableOriginalConstructor()
115  ->getMock();
116  $this->scopeResolverMock = $this->getMockBuilder(ScopeResolver::class)
117  ->disableOriginalConstructor()
118  ->getMock();
119  $this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
120  ->getMockForAbstractClass();
121  $this->scopeTypeNormalizer = $this->getMockBuilder(ScopeTypeNormalizer::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124 
125  $this->preparedValueFactory = new PreparedValueFactory(
126  $this->scopeResolverPoolMock,
127  $this->structureFactoryMock,
128  $this->valueFactoryMock,
129  $this->configMock,
130  $this->scopeTypeNormalizer
131  );
132  }
133 
143  public function testCreate(
144  $path,
145  $configPath,
146  $value,
147  $scope,
148  $scopeCode,
149  $scopeId
150  ) {
151  $groupPath = 'some/group';
152  $groupId = 'some_group';
153  $fieldId = 'some_field';
154  $fieldData = ['backend_model' => 'some_model'];
155 
156  if (ScopeInterface::SCOPE_DEFAULT !== $scope) {
157  $this->scopeResolverPoolMock->expects($this->once())
158  ->method('get')
159  ->with($scope)
160  ->willReturn($this->scopeResolverMock);
161  $this->scopeResolverMock->expects($this->once())
162  ->method('getScope')
163  ->with($scopeCode)
164  ->willReturn($this->scopeMock);
165  $this->scopeMock->expects($this->once())
166  ->method('getId')
167  ->willReturn($scopeId);
168  }
170  $groupMock = $this->getMockBuilder(Group::class)
171  ->disableOriginalConstructor()
172  ->getMock();
173  $groupMock->expects($this->once())
174  ->method('getId')
175  ->willReturn($groupId);
176 
177  $this->scopeTypeNormalizer->expects($this->once())
178  ->method('normalize')
179  ->with($scope, true)
180  ->willReturnArgument(0);
181  $this->structureFactoryMock->expects($this->once())
182  ->method('create')
183  ->willReturn($this->structureMock);
184  $this->structureMock->expects($this->once())
185  ->method('getElementByConfigPath')
186  ->willReturn($this->fieldMock);
187  $this->structureMock->expects($this->once())
188  ->method('getElement')
189  ->with($groupPath)
190  ->willReturn($groupMock);
191  $this->fieldMock->expects($this->once())
192  ->method('hasBackendModel')
193  ->willReturn(true);
194  $this->fieldMock
195  ->method('getConfigPath')
196  ->willReturn($configPath);
197  $this->fieldMock
198  ->method('getId')
199  ->willReturn($fieldId);
200  $this->fieldMock
201  ->method('getData')
202  ->willReturn($fieldData);
203  $this->fieldMock->expects($this->once())
204  ->method('getGroupPath')
205  ->willReturn($groupPath);
206  $this->valueFactoryMock->expects($this->once())
207  ->method('create')
208  ->willReturn($this->valueMock);
209  $this->valueMock->expects($this->once())
210  ->method('setPath')
211  ->with($configPath ?: $path)
212  ->willReturnSelf();
213  $this->valueMock->expects($this->once())
214  ->method('setScope')
215  ->with($scope)
216  ->willReturnSelf();
217  $this->valueMock->expects($this->once())
218  ->method('setScopeId')
219  ->with($scopeId)
220  ->willReturnSelf();
221  $this->valueMock->expects($this->once())
222  ->method('setScopeCode')
223  ->with($scopeCode)
224  ->willReturnSelf();
225  $this->valueMock->expects($this->once())
226  ->method('setValue')
227  ->with($value)
228  ->willReturnSelf();
229  $this->valueMock->expects($this->once())
230  ->method('setField')
231  ->with($fieldId)
232  ->willReturnSelf();
233  $this->valueMock->expects($this->once())
234  ->method('setGroupId')
235  ->with($groupId)
236  ->willReturnSelf();
237  $this->valueMock->expects($this->once())
238  ->method('setFieldConfig')
239  ->with($fieldData)
240  ->willReturnSelf();
241 
242  $this->assertInstanceOf(
243  Value::class,
244  $this->preparedValueFactory->create($path, $value, $scope, $scopeCode)
245  );
246  }
247 
251  public function createDataProvider()
252  {
253  return [
254  'standard flow' => [
255  '/some/path',
256  null,
257  'someValue',
258  'someScope',
259  'someScopeCode',
260  1,
261  ],
262  'standard flow with custom config path' => [
263  '/some/path',
264  '/custom/config_path',
265  'someValue',
266  'someScope',
267  'someScope',
268  'someScopeCode',
269  1,
270  ],
271  'default scope flow' => [
272  '/some/path',
273  null,
274  'someValue',
277  null,
278  0,
279  ],
280  'website scope flow' => [
281  '/some/path',
282  'someValue',
283  StoreScopeInterface::SCOPE_WEBSITE,
284  StoreScopeInterface::SCOPE_WEBSITES,
285  null,
286  0,
287  ],
288  'websites scope flow' => [
289  '/some/path',
290  'someValue',
291  StoreScopeInterface::SCOPE_WEBSITES,
292  StoreScopeInterface::SCOPE_WEBSITES,
293  null,
294  0,
295  ],
296  'store scope flow' => [
297  '/some/path',
298  'someValue',
299  StoreScopeInterface::SCOPE_STORE,
300  StoreScopeInterface::SCOPE_STORES,
301  null,
302  0,
303  ],
304  'stores scope flow' => [
305  '/some/path',
306  'someValue',
307  StoreScopeInterface::SCOPE_STORES,
308  StoreScopeInterface::SCOPE_STORES,
309  null,
310  0,
311  ],
312  ];
313  }
314 
322  $path,
323  $scope,
324  $scopeCode
325  ) {
326  $this->scopeResolverPoolMock->expects($this->never())
327  ->method('get');
328  $this->scopeResolverMock->expects($this->never())
329  ->method('getScope');
330  $this->scopeMock->expects($this->never())
331  ->method('getId');
332 
333  $value = new \stdClass();
334 
335  $this->structureFactoryMock->expects($this->once())
336  ->method('create')
337  ->willReturn($this->structureMock);
338  $this->structureMock->expects($this->once())
339  ->method('getElementByConfigPath')
340  ->willReturn($this->fieldMock);
341  $this->fieldMock->expects($this->once())
342  ->method('hasBackendModel')
343  ->willReturn(false);
344  $this->fieldMock->expects($this->never())
345  ->method('getBackendModel');
346  $this->valueFactoryMock->expects($this->once())
347  ->method('create')
348  ->willReturn($value);
349  $this->valueMock->expects($this->never())
350  ->method('setPath');
351  $this->valueMock->expects($this->never())
352  ->method('setScope');
353  $this->valueMock->expects($this->never())
354  ->method('setScopeId');
355  $this->valueMock->expects($this->never())
356  ->method('setValue');
357 
358  $this->assertSame(
359  $value,
360  $this->preparedValueFactory->create($path, $value, $scope, $scopeCode)
361  );
362  }
363 
368  {
369  return [
370  'standard flow' => [
371  '/some/path',
372  'someScope',
373  'someScopeCode',
374  1,
375  ],
376  'default scope flow' => [
377  '/some/path',
379  null,
380  ],
381  ];
382  }
383 
388  public function testCreateWithException()
389  {
390  $this->structureFactoryMock->expects($this->once())
391  ->method('create')
392  ->willThrowException(new \Exception('Some exception'));
393 
394  $this->preparedValueFactory->create('path', 'value', ScopeInterface::SCOPE_DEFAULT);
395  }
396 }
$fieldId
Definition: element.phtml:16
$value
Definition: gender.phtml:16