Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConvertToXmlTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
13 use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
18 use Magento\Ui\Model\Export\SearchResultIteratorFactory;
23 
27 class ConvertToXmlTest extends \PHPUnit\Framework\TestCase
28 {
32  protected $model;
33 
37  protected $filesystem;
38 
42  protected $filter;
43 
47  protected $metadataProvider;
48 
52  protected $excelFactory;
53 
57  protected $iteratorFactory;
58 
62  protected $directory;
63 
67  protected $stream;
68 
72  protected $component;
73 
74  protected function setUp()
75  {
76  $this->directory = $this->getMockBuilder(DirectoryWriteInterface::class)
77  ->getMockForAbstractClass();
78 
79  $this->filesystem = $this->getMockBuilder(Filesystem::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->filesystem->expects($this->any())
83  ->method('getDirectoryWrite')
85  ->willReturn($this->directory);
86 
87  $this->filter = $this->getMockBuilder(Filter::class)
88  ->disableOriginalConstructor()
89  ->getMock();
90 
91  $this->metadataProvider = $this->getMockBuilder(MetadataProvider::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94 
95  $this->excelFactory = $this->getMockBuilder(ExcelFactory::class)
96  ->disableOriginalConstructor()
97  ->setMethods(['create'])
98  ->getMock();
99 
100  $this->iteratorFactory = $this->getMockBuilder(\Magento\Ui\Model\Export\SearchResultIteratorFactory::class)
101  ->disableOriginalConstructor()
102  ->setMethods(['create'])
103  ->getMock();
104 
105  $this->component = $this->getMockBuilder(UiComponentInterface::class)
106  ->getMockForAbstractClass();
107 
108  $this->stream = $this->getMockBuilder(FileWriteInterface::class)
109  ->setMethods([
110  'lock',
111  'unlock',
112  'close',
113  ])
114  ->getMockForAbstractClass();
115 
116  $this->model = new ConvertToXml(
117  $this->filesystem,
118  $this->filter,
119  $this->metadataProvider,
120  $this->excelFactory,
121  $this->iteratorFactory
122  );
123  }
124 
125  public function testGetRowData()
126  {
127  $data = ['data_value'];
128 
130  $document = $this->getMockBuilder(DocumentInterface::class)
131  ->getMockForAbstractClass();
132 
133  $this->metadataProvider->expects($this->once())
134  ->method('getRowData')
135  ->with($document, [], [])
136  ->willReturn($data);
137  $this->metadataProvider->expects($this->once())
138  ->method('getFields')
139  ->with($this->component)
140  ->willReturn([]);
141  $this->metadataProvider->expects($this->once())
142  ->method('getOptions')
143  ->willReturn([]);
144 
145  $this->filter->expects($this->once())
146  ->method('getComponent')
147  ->willReturn($this->component);
148 
149  $result = $this->model->getRowData($document);
150  $this->assertEquals($data, $result);
151  }
152 
153  public function testGetXmlFile()
154  {
155  $componentName = 'component_name';
156 
158  $document = $this->getMockBuilder(DocumentInterface::class)
159  ->getMockForAbstractClass();
160 
161  $this->mockComponent($componentName, $document);
162  $this->mockStream();
163  $this->mockFilter();
164  $this->mockDirectory();
165  $this->mockExcel($componentName, $document);
166 
167  $this->metadataProvider->expects($this->once())
168  ->method('getHeaders')
169  ->with($this->component)
170  ->willReturn([]);
171  $this->metadataProvider->expects($this->once())
172  ->method('convertDate')
173  ->with($document, $componentName);
174 
175  $result = $this->model->getXmlFile();
176  $this->assertTrue(is_array($result));
177  $this->assertArrayHasKey('type', $result);
178  $this->assertArrayHasKey('value', $result);
179  $this->assertArrayHasKey('rm', $result);
180  $this->assertContains($componentName, $result);
181  $this->assertContains('.xml', $result);
182  }
183 
184  protected function mockStream()
185  {
186  $this->stream->expects($this->once())
187  ->method('lock')
188  ->willReturnSelf();
189  $this->stream->expects($this->once())
190  ->method('unlock')
191  ->willReturnSelf();
192  $this->stream->expects($this->once())
193  ->method('close')
194  ->willReturnSelf();
195  }
196 
201  protected function mockExcel($componentName, DocumentInterface $document)
202  {
203  $searchResultIterator = $this->getMockBuilder(SearchResultIterator::class)
204  ->disableOriginalConstructor()
205  ->getMock();
206 
207  $excel = $this->getMockBuilder(\Magento\Framework\Convert\Excel::class)
208  ->disableOriginalConstructor()
209  ->getMock();
210 
211  $this->iteratorFactory->expects($this->once())
212  ->method('create')
213  ->with(['items' => [$document]])
214  ->willReturn($searchResultIterator);
215 
216  $this->excelFactory->expects($this->once())
217  ->method('create')
218  ->with([
219  'iterator' => $searchResultIterator,
220  'rowCallback' => [$this->model, 'getRowData'],
221  ])
222  ->willReturn($excel);
223 
224  $excel->expects($this->once())
225  ->method('setDataHeader')
226  ->with([])
227  ->willReturnSelf();
228  $excel->expects($this->once())
229  ->method('write')
230  ->with($this->stream, $componentName . '.xml')
231  ->willReturnSelf();
232  }
233 
238  protected function mockComponent($componentName, DocumentInterface $document = null)
239  {
240  $context = $this->getMockBuilder(ContextInterface::class)
241  ->setMethods(['getDataProvider'])
242  ->getMockForAbstractClass();
243 
244  $dataProvider = $this->getMockBuilder(DataProviderInterface::class)
245  ->setMethods(['getSearchResult', 'setLimit'])
246  ->getMockForAbstractClass();
247 
248  $searchResult = $this->getMockBuilder(SearchResultInterface::class)
249  ->setMethods(['getItems'])
250  ->getMockForAbstractClass();
251 
252  $this->component->expects($this->any())
253  ->method('getName')
254  ->willReturn($componentName);
255  $this->component->expects($this->exactly(2))
256  ->method('getContext')
257  ->willReturn($context);
258 
259  $context->expects($this->exactly(2))
260  ->method('getDataProvider')
261  ->willReturn($dataProvider);
262 
263  $dataProvider->expects($this->once())
264  ->method('getSearchResult')
265  ->willReturn($searchResult);
266 
267  $dataProvider->expects($this->once())
268  ->method('setLimit')
269  ->with(0, 0);
270 
271  if ($document) {
272  $searchResult->expects($this->at(0))
273  ->method('getItems')
274  ->willReturn([$document]);
275  } else {
276  $searchResult->expects($this->at(0))
277  ->method('getItems')
278  ->willReturn([]);
279  }
280  }
281 
282  protected function mockFilter()
283  {
284  $this->filter->expects($this->once())
285  ->method('getComponent')
286  ->willReturn($this->component);
287  $this->filter->expects($this->once())
288  ->method('prepareComponent')
289  ->with($this->component)
290  ->willReturnSelf();
291  $this->filter->expects($this->once())
292  ->method('applySelectionOnTargetProvider')
293  ->willReturnSelf();
294  }
295 
296  protected function mockDirectory()
297  {
298  $this->directory->expects($this->once())
299  ->method('create')
300  ->with('export')
301  ->willReturnSelf();
302  $this->directory->expects($this->once())
303  ->method('openFile')
304  ->willReturn($this->stream);
305  }
306 }
mockComponent($componentName, DocumentInterface $document=null)
mockExcel($componentName, DocumentInterface $document)