ConvertToCsvTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
  10. use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
  11. use Magento\Framework\View\Element\UiComponentInterface;
  12. use Magento\Ui\Component\MassAction\Filter;
  13. use Magento\Ui\Model\Export\ConvertToCsv;
  14. use Magento\Ui\Model\Export\MetadataProvider;
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class ConvertToCsvTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var ConvertToCsv
  22. */
  23. protected $model;
  24. /**
  25. * @var DirectoryWriteInterface | \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $directory;
  28. /**
  29. * @var Filesystem | \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $filesystem;
  32. /**
  33. * @var Filter | \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $filter;
  36. /**
  37. * @var MetadataProvider | \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $metadataProvider;
  40. /**
  41. * @var FileWriteInterface | \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $stream;
  44. /**
  45. * @var UiComponentInterface | \PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $component;
  48. protected function setUp()
  49. {
  50. $this->directory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  51. ->getMockForAbstractClass();
  52. $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->filesystem->expects($this->any())
  56. ->method('getDirectoryWrite')
  57. ->with(DirectoryList::VAR_DIR)
  58. ->willReturn($this->directory);
  59. $this->filter = $this->getMockBuilder(\Magento\Ui\Component\MassAction\Filter::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->metadataProvider = $this->getMockBuilder(\Magento\Ui\Model\Export\MetadataProvider::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
  66. ->getMockForAbstractClass();
  67. $this->stream = $this->getMockBuilder(\Magento\Framework\Filesystem\File\WriteInterface::class)
  68. ->setMethods([
  69. 'lock',
  70. 'unlock',
  71. 'close',
  72. ])
  73. ->getMockForAbstractClass();
  74. $this->model = new ConvertToCsv(
  75. $this->filesystem,
  76. $this->filter,
  77. $this->metadataProvider
  78. );
  79. }
  80. public function testGetCsvFile()
  81. {
  82. $componentName = 'component_name';
  83. $data = ['data_value'];
  84. $document = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentInterface::class)
  85. ->getMockForAbstractClass();
  86. $this->mockComponent($componentName, [$document]);
  87. $this->mockFilter();
  88. $this->mockDirectory();
  89. $this->stream->expects($this->once())
  90. ->method('lock')
  91. ->willReturnSelf();
  92. $this->stream->expects($this->once())
  93. ->method('unlock')
  94. ->willReturnSelf();
  95. $this->stream->expects($this->once())
  96. ->method('close')
  97. ->willReturnSelf();
  98. $this->stream->expects($this->any())
  99. ->method('writeCsv')
  100. ->with($data)
  101. ->willReturnSelf();
  102. $this->metadataProvider->expects($this->once())
  103. ->method('getOptions')
  104. ->willReturn([]);
  105. $this->metadataProvider->expects($this->once())
  106. ->method('getHeaders')
  107. ->with($this->component)
  108. ->willReturn($data);
  109. $this->metadataProvider->expects($this->once())
  110. ->method('getFields')
  111. ->with($this->component)
  112. ->willReturn([]);
  113. $this->metadataProvider->expects($this->once())
  114. ->method('getRowData')
  115. ->with($document, [], [])
  116. ->willReturn($data);
  117. $this->metadataProvider->expects($this->once())
  118. ->method('convertDate')
  119. ->with($document, $componentName);
  120. $result = $this->model->getCsvFile();
  121. $this->assertTrue(is_array($result));
  122. $this->assertArrayHasKey('type', $result);
  123. $this->assertArrayHasKey('value', $result);
  124. $this->assertArrayHasKey('rm', $result);
  125. $this->assertContains($componentName, $result);
  126. $this->assertContains('.csv', $result);
  127. }
  128. /**
  129. * @param array $expected
  130. */
  131. protected function mockStream($expected)
  132. {
  133. $this->stream = $this->getMockBuilder(\Magento\Framework\Filesystem\File\WriteInterface::class)
  134. ->setMethods([
  135. 'lock',
  136. 'unlock',
  137. 'close',
  138. ])
  139. ->getMockForAbstractClass();
  140. $this->stream->expects($this->once())
  141. ->method('lock')
  142. ->willReturnSelf();
  143. $this->stream->expects($this->once())
  144. ->method('unlock')
  145. ->willReturnSelf();
  146. $this->stream->expects($this->once())
  147. ->method('close')
  148. ->willReturnSelf();
  149. $this->stream->expects($this->once())
  150. ->method('writeCsv')
  151. ->with($expected)
  152. ->willReturnSelf();
  153. }
  154. /**
  155. * @param string $componentName
  156. * @param array $items
  157. */
  158. protected function mockComponent($componentName, $items)
  159. {
  160. $context = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
  161. ->setMethods(['getDataProvider'])
  162. ->getMockForAbstractClass();
  163. $dataProvider = $this->getMockBuilder(
  164. \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class
  165. )
  166. ->setMethods(['getSearchResult'])
  167. ->getMockForAbstractClass();
  168. $searchResult = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchResultInterface::class)
  169. ->setMethods(['getItems'])
  170. ->getMockForAbstractClass();
  171. $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
  172. ->setMethods(['setPageSize', 'setCurrentPage'])
  173. ->getMockForAbstractClass();
  174. $this->component->expects($this->any())
  175. ->method('getName')
  176. ->willReturn($componentName);
  177. $this->component->expects($this->once())
  178. ->method('getContext')
  179. ->willReturn($context);
  180. $context->expects($this->once())
  181. ->method('getDataProvider')
  182. ->willReturn($dataProvider);
  183. $dataProvider->expects($this->exactly(2))
  184. ->method('getSearchResult')
  185. ->willReturn($searchResult);
  186. $dataProvider->expects($this->once())
  187. ->method('getSearchCriteria')
  188. ->willReturn($searchCriteria);
  189. $searchResult->expects($this->once())
  190. ->method('getItems')
  191. ->willReturn($items);
  192. $searchResult->expects($this->once())
  193. ->method('getTotalCount')
  194. ->willReturn(1);
  195. $searchCriteria->expects($this->any())
  196. ->method('setCurrentPage')
  197. ->willReturnSelf();
  198. $searchCriteria->expects($this->once())
  199. ->method('setPageSize')
  200. ->with(200)
  201. ->willReturnSelf();
  202. }
  203. protected function mockFilter()
  204. {
  205. $this->filter->expects($this->once())
  206. ->method('getComponent')
  207. ->willReturn($this->component);
  208. $this->filter->expects($this->once())
  209. ->method('prepareComponent')
  210. ->with($this->component)
  211. ->willReturnSelf();
  212. $this->filter->expects($this->once())
  213. ->method('applySelectionOnTargetProvider')
  214. ->willReturnSelf();
  215. }
  216. protected function mockDirectory()
  217. {
  218. $this->directory->expects($this->once())
  219. ->method('create')
  220. ->with('export')
  221. ->willReturnSelf();
  222. $this->directory->expects($this->once())
  223. ->method('openFile')
  224. ->willReturn($this->stream);
  225. }
  226. }