Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConvertToCsvTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWriteInterface;
11 use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
16 
20 class ConvertToCsvTest extends \PHPUnit\Framework\TestCase
21 {
25  protected $model;
26 
30  protected $directory;
31 
35  protected $filesystem;
36 
40  protected $filter;
41 
45  protected $metadataProvider;
46 
50  protected $stream;
51 
55  protected $component;
56 
57  protected function setUp()
58  {
59  $this->directory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
60  ->getMockForAbstractClass();
61 
62  $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->filesystem->expects($this->any())
66  ->method('getDirectoryWrite')
68  ->willReturn($this->directory);
69 
70  $this->filter = $this->getMockBuilder(\Magento\Ui\Component\MassAction\Filter::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73 
74  $this->metadataProvider = $this->getMockBuilder(\Magento\Ui\Model\Export\MetadataProvider::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77 
78  $this->component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
79  ->getMockForAbstractClass();
80 
81  $this->stream = $this->getMockBuilder(\Magento\Framework\Filesystem\File\WriteInterface::class)
82  ->setMethods([
83  'lock',
84  'unlock',
85  'close',
86  ])
87  ->getMockForAbstractClass();
88 
89  $this->model = new ConvertToCsv(
90  $this->filesystem,
91  $this->filter,
92  $this->metadataProvider
93  );
94  }
95 
96  public function testGetCsvFile()
97  {
98  $componentName = 'component_name';
99  $data = ['data_value'];
100 
101  $document = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentInterface::class)
102  ->getMockForAbstractClass();
103 
104  $this->mockComponent($componentName, [$document]);
105  $this->mockFilter();
106  $this->mockDirectory();
107 
108  $this->stream->expects($this->once())
109  ->method('lock')
110  ->willReturnSelf();
111  $this->stream->expects($this->once())
112  ->method('unlock')
113  ->willReturnSelf();
114  $this->stream->expects($this->once())
115  ->method('close')
116  ->willReturnSelf();
117  $this->stream->expects($this->any())
118  ->method('writeCsv')
119  ->with($data)
120  ->willReturnSelf();
121 
122  $this->metadataProvider->expects($this->once())
123  ->method('getOptions')
124  ->willReturn([]);
125  $this->metadataProvider->expects($this->once())
126  ->method('getHeaders')
127  ->with($this->component)
128  ->willReturn($data);
129  $this->metadataProvider->expects($this->once())
130  ->method('getFields')
131  ->with($this->component)
132  ->willReturn([]);
133  $this->metadataProvider->expects($this->once())
134  ->method('getRowData')
135  ->with($document, [], [])
136  ->willReturn($data);
137  $this->metadataProvider->expects($this->once())
138  ->method('convertDate')
139  ->with($document, $componentName);
140 
141  $result = $this->model->getCsvFile();
142  $this->assertTrue(is_array($result));
143  $this->assertArrayHasKey('type', $result);
144  $this->assertArrayHasKey('value', $result);
145  $this->assertArrayHasKey('rm', $result);
146  $this->assertContains($componentName, $result);
147  $this->assertContains('.csv', $result);
148  }
149 
153  protected function mockStream($expected)
154  {
155  $this->stream = $this->getMockBuilder(\Magento\Framework\Filesystem\File\WriteInterface::class)
156  ->setMethods([
157  'lock',
158  'unlock',
159  'close',
160  ])
161  ->getMockForAbstractClass();
162 
163  $this->stream->expects($this->once())
164  ->method('lock')
165  ->willReturnSelf();
166  $this->stream->expects($this->once())
167  ->method('unlock')
168  ->willReturnSelf();
169  $this->stream->expects($this->once())
170  ->method('close')
171  ->willReturnSelf();
172  $this->stream->expects($this->once())
173  ->method('writeCsv')
174  ->with($expected)
175  ->willReturnSelf();
176  }
177 
182  protected function mockComponent($componentName, $items)
183  {
184  $context = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
185  ->setMethods(['getDataProvider'])
186  ->getMockForAbstractClass();
187 
188  $dataProvider = $this->getMockBuilder(
189  \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class
190  )
191  ->setMethods(['getSearchResult'])
192  ->getMockForAbstractClass();
193 
194  $searchResult = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchResultInterface::class)
195  ->setMethods(['getItems'])
196  ->getMockForAbstractClass();
197 
198  $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaInterface::class)
199  ->setMethods(['setPageSize', 'setCurrentPage'])
200  ->getMockForAbstractClass();
201  $this->component->expects($this->any())
202  ->method('getName')
203  ->willReturn($componentName);
204  $this->component->expects($this->once())
205  ->method('getContext')
206  ->willReturn($context);
207 
208  $context->expects($this->once())
209  ->method('getDataProvider')
210  ->willReturn($dataProvider);
211 
212  $dataProvider->expects($this->exactly(2))
213  ->method('getSearchResult')
214  ->willReturn($searchResult);
215 
216  $dataProvider->expects($this->once())
217  ->method('getSearchCriteria')
218  ->willReturn($searchCriteria);
219 
220  $searchResult->expects($this->once())
221  ->method('getItems')
222  ->willReturn($items);
223 
224  $searchResult->expects($this->once())
225  ->method('getTotalCount')
226  ->willReturn(1);
227 
228  $searchCriteria->expects($this->any())
229  ->method('setCurrentPage')
230  ->willReturnSelf();
231 
232  $searchCriteria->expects($this->once())
233  ->method('setPageSize')
234  ->with(200)
235  ->willReturnSelf();
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 
252  protected function mockDirectory()
253  {
254  $this->directory->expects($this->once())
255  ->method('create')
256  ->with('export')
257  ->willReturnSelf();
258  $this->directory->expects($this->once())
259  ->method('openFile')
260  ->willReturn($this->stream);
261  }
262 }
$searchCriteria
$items