Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExportDataHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
18 
19 class ExportDataHandlerTest extends \PHPUnit\Framework\TestCase
20 {
24  private $filesystemMock;
25 
29  private $archiveMock;
30 
34  private $reportWriterMock;
35 
39  private $cryptographerMock;
40 
44  private $fileRecorderMock;
45 
49  private $directoryMock;
50 
54  private $encodedContextMock;
55 
59  private $objectManagerHelper;
60 
64  private $exportDataHandler;
65 
69  private $subdirectoryPath = 'analytics/';
70 
74  private $archiveName = 'data.tgz';
75 
79  protected function setUp()
80  {
81  $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84 
85  $this->archiveMock = $this->getMockBuilder(Archive::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->reportWriterMock = $this->getMockBuilder(ReportWriterInterface::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $this->cryptographerMock = $this->getMockBuilder(Cryptographer::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96 
97  $this->fileRecorderMock = $this->getMockBuilder(FileRecorder::class)
98  ->disableOriginalConstructor()
99  ->getMock();
100 
101  $this->directoryMock = $this->getMockBuilder(WriteInterface::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104 
105  $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108 
109  $this->objectManagerHelper = new ObjectManagerHelper($this);
110 
111  $this->exportDataHandler = $this->objectManagerHelper->getObject(
112  ExportDataHandler::class,
113  [
114  'filesystem' => $this->filesystemMock,
115  'archive' => $this->archiveMock,
116  'reportWriter' => $this->reportWriterMock,
117  'cryptographer' => $this->cryptographerMock,
118  'fileRecorder' => $this->fileRecorderMock,
119  'subdirectoryPath' => $this->subdirectoryPath,
120  'archiveName' => $this->archiveName,
121  ]
122  );
123  }
124 
129  public function testPrepareExportData($isArchiveSourceDirectory)
130  {
131  $tmpFilesDirectoryPath = $this->subdirectoryPath . 'tmp/';
132  $archiveRelativePath = $this->subdirectoryPath . $this->archiveName;
133 
134  $archiveSource = $isArchiveSourceDirectory ? (__DIR__) : '/tmp/' . $tmpFilesDirectoryPath;
135  $archiveAbsolutePath = '/tmp/' . $archiveRelativePath;
136 
137  $this->filesystemMock
138  ->expects($this->once())
139  ->method('getDirectoryWrite')
140  ->with(DirectoryList::SYS_TMP)
141  ->willReturn($this->directoryMock);
142  $this->directoryMock
143  ->expects($this->exactly(4))
144  ->method('delete')
145  ->withConsecutive(
146  [$tmpFilesDirectoryPath],
147  [$archiveRelativePath]
148  );
149 
150  $this->directoryMock
151  ->expects($this->exactly(4))
152  ->method('getAbsolutePath')
153  ->withConsecutive(
154  [$tmpFilesDirectoryPath],
155  [$tmpFilesDirectoryPath],
156  [$archiveRelativePath],
157  [$archiveRelativePath]
158  )
159  ->willReturnOnConsecutiveCalls(
160  $archiveSource,
161  $archiveSource,
162  $archiveAbsolutePath,
163  $archiveAbsolutePath
164  );
165 
166  $this->reportWriterMock
167  ->expects($this->once())
168  ->method('write')
169  ->with($this->directoryMock, $tmpFilesDirectoryPath);
170 
171  $this->directoryMock
172  ->expects($this->exactly(2))
173  ->method('isExist')
174  ->withConsecutive(
175  [$tmpFilesDirectoryPath],
176  [$archiveRelativePath]
177  )
178  ->willReturnOnConsecutiveCalls(
179  true,
180  true
181  );
182 
183  $this->directoryMock
184  ->expects($this->once())
185  ->method('create')
186  ->with(dirname($archiveRelativePath));
187 
188  $this->archiveMock
189  ->expects($this->once())
190  ->method('pack')
191  ->with(
192  $archiveSource,
193  $archiveAbsolutePath,
194  $isArchiveSourceDirectory ? true : false
195  );
196 
197  $fileContent = 'Some text';
198  $this->directoryMock
199  ->expects($this->once())
200  ->method('readFile')
201  ->with($archiveRelativePath)
202  ->willReturn($fileContent);
203 
204  $this->cryptographerMock
205  ->expects($this->once())
206  ->method('encode')
207  ->with($fileContent)
208  ->willReturn($this->encodedContextMock);
209 
210  $this->fileRecorderMock
211  ->expects($this->once())
212  ->method('recordNewFile')
213  ->with($this->encodedContextMock);
214 
215  $this->assertTrue($this->exportDataHandler->prepareExportData());
216  }
217 
222  {
223  return [
224  'Data source for archive is directory' => [true],
225  'Data source for archive doesn\'t directory' => [false],
226  ];
227  }
228 
234  {
235  $tmpFilesDirectoryPath = $this->subdirectoryPath . 'tmp/';
236  $archivePath = $this->subdirectoryPath . $this->archiveName;
237 
238  $this->filesystemMock
239  ->expects($this->once())
240  ->method('getDirectoryWrite')
241  ->with(DirectoryList::SYS_TMP)
242  ->willReturn($this->directoryMock);
243  $this->reportWriterMock
244  ->expects($this->once())
245  ->method('write')
246  ->with($this->directoryMock, $tmpFilesDirectoryPath);
247  $this->directoryMock
248  ->expects($this->exactly(3))
249  ->method('delete')
250  ->withConsecutive(
251  [$tmpFilesDirectoryPath],
252  [$tmpFilesDirectoryPath],
253  [$archivePath]
254  );
255  $this->directoryMock
256  ->expects($this->exactly(2))
257  ->method('getAbsolutePath')
258  ->with($tmpFilesDirectoryPath);
259  $this->directoryMock
260  ->expects($this->once())
261  ->method('isExist')
262  ->with($tmpFilesDirectoryPath)
263  ->willReturn(false);
264 
265  $this->assertNull($this->exportDataHandler->prepareExportData());
266  }
267 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60