Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ManagerTest.php
Go to the documentation of this file.
1 <?php
8 
20 
26 class ManagerTest extends \PHPUnit\Framework\TestCase
27 {
31  protected $manager;
32 
37 
41  protected $cacheConfig;
42 
47 
51  protected $uiReader;
52 
56  protected $readerFactory;
57 
62 
66  protected $domMerger;
67 
72 
77 
79  private $serializer;
80 
81  protected function setUp()
82  {
83  $this->componentConfigProvider = $this->getMockBuilder(
84  \Magento\Framework\View\Element\UiComponent\Config\Provider\Component\Definition::class
85  )->disableOriginalConstructor()->getMock();
86  $this->domMerger = $this->getMockBuilder(
87  \Magento\Framework\View\Element\UiComponent\Config\DomMergerInterface::class
88  )->getMockForAbstractClass();
89  $this->aggregatedFileCollector = $this->getMockBuilder(
90  \Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollector::class
91  )->disableOriginalConstructor()->getMock();
92  $this->aggregatedFileCollectorFactory = $this->getMockBuilder(
93  \Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollectorFactory::class
94  )->disableOriginalConstructor()->getMock();
95  $this->arrayObjectFactory = $this->getMockBuilder(
96  \Magento\Framework\View\Element\UiComponent\ArrayObjectFactory::class
97  )->disableOriginalConstructor()->getMock();
98  $this->arrayObjectFactory->expects($this->at(0))
99  ->method('create')
100  ->willReturn(new \ArrayObject([]));
101  $this->uiReader = $this->getMockBuilder(
102  \Magento\Framework\View\Element\UiComponent\Config\UiReaderInterface::class
103  )->getMockForAbstractClass();
104  $this->readerFactory = $this->getMockBuilder(
105  \Magento\Framework\View\Element\UiComponent\Config\ReaderFactory::class
106  )->disableOriginalConstructor()->getMock();
107  $this->cacheConfig = $this->getMockBuilder(\Magento\Framework\Config\CacheInterface::class)
108  ->getMockForAbstractClass();
109  $this->argumentInterpreter = $this->getMockBuilder(\Magento\Framework\Data\Argument\InterpreterInterface::class)
110  ->getMockForAbstractClass();
111  $this->serializer = $this->getMockBuilder(
112  \Magento\Framework\Serialize\SerializerInterface::class
113  )->getMockForAbstractClass();
114  $this->serializer->expects($this->any())
115  ->method('serialize')
116  ->willReturnCallback(
117  function ($value) {
118  return json_encode($value);
119  }
120  );
121  $this->serializer->expects($this->any())
122  ->method('unserialize')
123  ->willReturnCallback(
124  function ($value) {
125  return json_decode($value, true);
126  }
127  );
128 
129  $this->manager = new Manager(
130  $this->componentConfigProvider,
131  $this->domMerger,
132  $this->readerFactory,
133  $this->arrayObjectFactory,
134  $this->aggregatedFileCollectorFactory,
135  $this->cacheConfig,
136  $this->argumentInterpreter,
137  $this->serializer
138  );
139  }
140 
141  public function testGetReader()
142  {
143  $this->readerFactory->expects($this->once())
144  ->method('create')
145  ->with(['fileCollector' => $this->aggregatedFileCollector, 'domMerger' => $this->domMerger])
146  ->willReturn($this->uiReader);
147  $this->aggregatedFileCollectorFactory->expects($this->once())
148  ->method('create')
149  ->willReturn($this->aggregatedFileCollector);
150  $this->assertEquals($this->uiReader, $this->manager->getReader('some_name'));
151  }
152 
153  public function testPrepareDataWithoutName()
154  {
155  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
156  $this->expectExceptionMessage(
157  (string)__('The "" UI component element name is invalid. Verify the name and try again.')
158  );
159  $this->manager->prepareData(null);
160  }
161 
165  public function testPrepareGetData($componentName, $componentData, $isCached, $readerData, $expectedResult)
166  {
167  $this->readerFactory->expects($this->any())
168  ->method('create')
169  ->with(['fileCollector' => $this->aggregatedFileCollector, 'domMerger' => $this->domMerger])
170  ->willReturn($this->uiReader);
171  $this->aggregatedFileCollectorFactory->expects($this->any())
172  ->method('create')
173  ->willReturn($this->aggregatedFileCollector);
174  $this->argumentInterpreter->expects($this->any())
175  ->method('evaluate')
176  ->willReturnCallback(function ($argument) {
177  return ['argument' => $argument['value']];
178  });
179  $this->arrayObjectFactory->expects($this->any())
180  ->method('create')
181  ->willReturn($componentData);
182  $this->cacheConfig->expects($this->any())
183  ->method('load')
184  ->with(Manager::CACHE_ID . '_' . $componentName)
185  ->willReturn($isCached);
186 
187  $this->uiReader->expects($this->any())
188  ->method('read')
189  ->willReturn($readerData);
190  $this->assertEquals(
191  $expectedResult,
192  $this->manager->prepareData($componentName)->getData($componentName)
193  );
194  }
195 
199  public function getComponentData()
200  {
201  $cachedData = new \ArrayObject(
202  ['test_component1' =>
203  [
204  ManagerInterface::COMPONENT_ARGUMENTS_KEY => ['argument_name1' => ['value' => 'value1']],
206  'custom' => [
208  ['custom_name1' => ['value' => 'custom_value1']],
210  ],
211  ],
212  ]
213  ]
214  );
215 
216  return [
217  [
218  'test_component1',
219  new \ArrayObject(),
220  json_encode($cachedData->getArrayCopy()),
221  [],
222  [
223  'test_component1' => [
224  ManagerInterface::COMPONENT_ARGUMENTS_KEY => ['argument_name1' => ['argument' => 'value1']],
226  'custom' => [
228  ['custom_name1' => ['argument' => 'custom_value1']],
230  ]
231  ]
232  ],
233  ],
234  ],
235  [
236  'test_component2',
237  new \ArrayObject(
238  ['test_component2' =>
239  [
240  ManagerInterface::COMPONENT_ARGUMENTS_KEY => ['argument_name2' => ['value' => 'value2']],
242  'test_component21' => [
244  ['argument_name21' => ['value' => 'value21']],
246  ],
247  ],
248  ]
249  ]
250  ),
251  false,
252  ['componentGroup' => [0 => [
253  Converter::DATA_ARGUMENTS_KEY => ['argument_name2' => ['value' => 'value2']],
254  Converter::DATA_ATTRIBUTES_KEY => ['name' => 'attribute_name2'],
255  'test_component21' => [0 => [
256  Converter::DATA_ARGUMENTS_KEY => ['argument_name21' => ['value' => 'value21']],
257  Converter::DATA_ATTRIBUTES_KEY => ['name' => 'attribute_name21'],
258  ]
259  ],
260  ]]],
261  [
262  'test_component2' => [
263  ManagerInterface::COMPONENT_ARGUMENTS_KEY => ['argument_name2' => ['argument' => 'value2']],
264  ManagerInterface::COMPONENT_ATTRIBUTES_KEY => ['name' => 'attribute_name2'],
266  'attribute_name21' => [
268  ['argument_name21' => ['argument' => 'value21']],
269  ManagerInterface::COMPONENT_ATTRIBUTES_KEY => ['name' => 'attribute_name21'],
271  ],
272  ],
273  ],
274  ],
275  ],
276  ];
277  }
278 
282  public function testCreateRawComponentData($componentName, $configData, $componentData, $needEvaluate)
283  {
284  $this->componentConfigProvider->expects($this->any())
285  ->method('getComponentData')
286  ->willReturn($configData);
287  if ($needEvaluate === true) {
288  $this->argumentInterpreter->expects($this->once())
289  ->method('evaluate')
290  ->willReturnCallback(function ($argument) {
291  return ['argument' => $argument['value']];
292  });
293  } else {
294  $this->argumentInterpreter->expects($this->never())->method('evaluate');
295  }
296  $this->assertEquals($componentData, $this->manager->createRawComponentData($componentName, $needEvaluate));
297  }
298 
302  public function getComponentDataProvider()
303  {
304  return [
305  [
306  'test_component1',
307  [
308  Converter::DATA_ATTRIBUTES_KEY => ['name' => 'attribute_name1'],
309  ],
310  [
311  ManagerInterface::COMPONENT_ATTRIBUTES_KEY => ['name' => 'attribute_name1'],
313 
314  ],
315  false,
316  ],
317  [
318  'test_component2',
319  [
320  Converter::DATA_ARGUMENTS_KEY => ['argument_name2' => ['value' => 'value2']],
321  ],
322  [
324  ManagerInterface::COMPONENT_ARGUMENTS_KEY => ['argument_name2' => ['value' => 'value2']],
325 
326  ],
327  false,
328  ],
329  [
330  'test_component3',
331  [
332  Converter::DATA_ATTRIBUTES_KEY => ['name' => 'attribute_name3'],
333  Converter::DATA_ARGUMENTS_KEY => ['argument_name3' => ['value' => 'value3']],
334  ],
335  [
336  ManagerInterface::COMPONENT_ATTRIBUTES_KEY => ['name' => 'attribute_name3'],
337  ManagerInterface::COMPONENT_ARGUMENTS_KEY => ['argument_name3' => ['argument' => 'value3']],
338 
339  ],
340  true,
341  ],
342  ];
343  }
344 }
testPrepareGetData($componentName, $componentData, $isCached, $readerData, $expectedResult)
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
testCreateRawComponentData($componentName, $configData, $componentData, $needEvaluate)