ConvertToXmlTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Model\Export;
  7. use Magento\Framework\Api\Search\DocumentInterface;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Convert\ExcelFactory;
  10. use Magento\Framework\Filesystem;
  11. use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
  12. use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
  13. use Magento\Framework\View\Element\UiComponentInterface;
  14. use Magento\Ui\Component\MassAction\Filter;
  15. use Magento\Ui\Model\Export\ConvertToXml;
  16. use Magento\Ui\Model\Export\MetadataProvider;
  17. use Magento\Ui\Model\Export\SearchResultIteratorFactory;
  18. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  19. use Magento\Ui\Model\Export\SearchResultIterator;
  20. use Magento\Framework\Api\Search\SearchResultInterface;
  21. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
  22. /**
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class ConvertToXmlTest extends \PHPUnit\Framework\TestCase
  26. {
  27. /**
  28. * @var ConvertToXml
  29. */
  30. protected $model;
  31. /**
  32. * @var Filesystem | \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $filesystem;
  35. /**
  36. * @var Filter | \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $filter;
  39. /**
  40. * @var MetadataProvider | \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $metadataProvider;
  43. /**
  44. * @var ExcelFactory | \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $excelFactory;
  47. /**
  48. * @var SearchResultIteratorFactory | \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $iteratorFactory;
  51. /**
  52. * @var DirectoryWriteInterface | \PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $directory;
  55. /**
  56. * @var FileWriteInterface | \PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $stream;
  59. /**
  60. * @var UiComponentInterface | \PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected $component;
  63. protected function setUp()
  64. {
  65. $this->directory = $this->getMockBuilder(DirectoryWriteInterface::class)
  66. ->getMockForAbstractClass();
  67. $this->filesystem = $this->getMockBuilder(Filesystem::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->filesystem->expects($this->any())
  71. ->method('getDirectoryWrite')
  72. ->with(DirectoryList::VAR_DIR)
  73. ->willReturn($this->directory);
  74. $this->filter = $this->getMockBuilder(Filter::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->metadataProvider = $this->getMockBuilder(MetadataProvider::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->excelFactory = $this->getMockBuilder(ExcelFactory::class)
  81. ->disableOriginalConstructor()
  82. ->setMethods(['create'])
  83. ->getMock();
  84. $this->iteratorFactory = $this->getMockBuilder(\Magento\Ui\Model\Export\SearchResultIteratorFactory::class)
  85. ->disableOriginalConstructor()
  86. ->setMethods(['create'])
  87. ->getMock();
  88. $this->component = $this->getMockBuilder(UiComponentInterface::class)
  89. ->getMockForAbstractClass();
  90. $this->stream = $this->getMockBuilder(FileWriteInterface::class)
  91. ->setMethods([
  92. 'lock',
  93. 'unlock',
  94. 'close',
  95. ])
  96. ->getMockForAbstractClass();
  97. $this->model = new ConvertToXml(
  98. $this->filesystem,
  99. $this->filter,
  100. $this->metadataProvider,
  101. $this->excelFactory,
  102. $this->iteratorFactory
  103. );
  104. }
  105. public function testGetRowData()
  106. {
  107. $data = ['data_value'];
  108. /** @var DocumentInterface $document */
  109. $document = $this->getMockBuilder(DocumentInterface::class)
  110. ->getMockForAbstractClass();
  111. $this->metadataProvider->expects($this->once())
  112. ->method('getRowData')
  113. ->with($document, [], [])
  114. ->willReturn($data);
  115. $this->metadataProvider->expects($this->once())
  116. ->method('getFields')
  117. ->with($this->component)
  118. ->willReturn([]);
  119. $this->metadataProvider->expects($this->once())
  120. ->method('getOptions')
  121. ->willReturn([]);
  122. $this->filter->expects($this->once())
  123. ->method('getComponent')
  124. ->willReturn($this->component);
  125. $result = $this->model->getRowData($document);
  126. $this->assertEquals($data, $result);
  127. }
  128. public function testGetXmlFile()
  129. {
  130. $componentName = 'component_name';
  131. /** @var DocumentInterface $document */
  132. $document = $this->getMockBuilder(DocumentInterface::class)
  133. ->getMockForAbstractClass();
  134. $this->mockComponent($componentName, $document);
  135. $this->mockStream();
  136. $this->mockFilter();
  137. $this->mockDirectory();
  138. $this->mockExcel($componentName, $document);
  139. $this->metadataProvider->expects($this->once())
  140. ->method('getHeaders')
  141. ->with($this->component)
  142. ->willReturn([]);
  143. $this->metadataProvider->expects($this->once())
  144. ->method('convertDate')
  145. ->with($document, $componentName);
  146. $result = $this->model->getXmlFile();
  147. $this->assertTrue(is_array($result));
  148. $this->assertArrayHasKey('type', $result);
  149. $this->assertArrayHasKey('value', $result);
  150. $this->assertArrayHasKey('rm', $result);
  151. $this->assertContains($componentName, $result);
  152. $this->assertContains('.xml', $result);
  153. }
  154. protected function mockStream()
  155. {
  156. $this->stream->expects($this->once())
  157. ->method('lock')
  158. ->willReturnSelf();
  159. $this->stream->expects($this->once())
  160. ->method('unlock')
  161. ->willReturnSelf();
  162. $this->stream->expects($this->once())
  163. ->method('close')
  164. ->willReturnSelf();
  165. }
  166. /**
  167. * @param string $componentName
  168. * @param DocumentInterface $document
  169. */
  170. protected function mockExcel($componentName, DocumentInterface $document)
  171. {
  172. $searchResultIterator = $this->getMockBuilder(SearchResultIterator::class)
  173. ->disableOriginalConstructor()
  174. ->getMock();
  175. $excel = $this->getMockBuilder(\Magento\Framework\Convert\Excel::class)
  176. ->disableOriginalConstructor()
  177. ->getMock();
  178. $this->iteratorFactory->expects($this->once())
  179. ->method('create')
  180. ->with(['items' => [$document]])
  181. ->willReturn($searchResultIterator);
  182. $this->excelFactory->expects($this->once())
  183. ->method('create')
  184. ->with([
  185. 'iterator' => $searchResultIterator,
  186. 'rowCallback' => [$this->model, 'getRowData'],
  187. ])
  188. ->willReturn($excel);
  189. $excel->expects($this->once())
  190. ->method('setDataHeader')
  191. ->with([])
  192. ->willReturnSelf();
  193. $excel->expects($this->once())
  194. ->method('write')
  195. ->with($this->stream, $componentName . '.xml')
  196. ->willReturnSelf();
  197. }
  198. /**
  199. * @param string $componentName
  200. * @param DocumentInterface|null $document
  201. */
  202. protected function mockComponent($componentName, DocumentInterface $document = null)
  203. {
  204. $context = $this->getMockBuilder(ContextInterface::class)
  205. ->setMethods(['getDataProvider'])
  206. ->getMockForAbstractClass();
  207. $dataProvider = $this->getMockBuilder(DataProviderInterface::class)
  208. ->setMethods(['getSearchResult', 'setLimit'])
  209. ->getMockForAbstractClass();
  210. $searchResult = $this->getMockBuilder(SearchResultInterface::class)
  211. ->setMethods(['getItems'])
  212. ->getMockForAbstractClass();
  213. $this->component->expects($this->any())
  214. ->method('getName')
  215. ->willReturn($componentName);
  216. $this->component->expects($this->exactly(2))
  217. ->method('getContext')
  218. ->willReturn($context);
  219. $context->expects($this->exactly(2))
  220. ->method('getDataProvider')
  221. ->willReturn($dataProvider);
  222. $dataProvider->expects($this->once())
  223. ->method('getSearchResult')
  224. ->willReturn($searchResult);
  225. $dataProvider->expects($this->once())
  226. ->method('setLimit')
  227. ->with(0, 0);
  228. if ($document) {
  229. $searchResult->expects($this->at(0))
  230. ->method('getItems')
  231. ->willReturn([$document]);
  232. } else {
  233. $searchResult->expects($this->at(0))
  234. ->method('getItems')
  235. ->willReturn([]);
  236. }
  237. }
  238. protected function mockFilter()
  239. {
  240. $this->filter->expects($this->once())
  241. ->method('getComponent')
  242. ->willReturn($this->component);
  243. $this->filter->expects($this->once())
  244. ->method('prepareComponent')
  245. ->with($this->component)
  246. ->willReturnSelf();
  247. $this->filter->expects($this->once())
  248. ->method('applySelectionOnTargetProvider')
  249. ->willReturnSelf();
  250. }
  251. protected function mockDirectory()
  252. {
  253. $this->directory->expects($this->once())
  254. ->method('create')
  255. ->with('export')
  256. ->willReturnSelf();
  257. $this->directory->expects($this->once())
  258. ->method('openFile')
  259. ->willReturn($this->stream);
  260. }
  261. }