ExportDataHandlerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Model;
  7. use Magento\Analytics\Model\Cryptographer;
  8. use Magento\Analytics\Model\EncodedContext;
  9. use Magento\Analytics\Model\ExportDataHandler;
  10. use Magento\Analytics\Model\FileRecorder;
  11. use Magento\Analytics\Model\ReportWriterInterface;
  12. use Magento\Framework\Archive;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\Filesystem\Directory\WriteInterface;
  15. use Magento\Framework\Filesystem\DirectoryList;
  16. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  17. class ExportDataHandlerTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $filesystemMock;
  23. /**
  24. * @var Archive|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $archiveMock;
  27. /**
  28. * @var ReportWriterInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $reportWriterMock;
  31. /**
  32. * @var Cryptographer|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $cryptographerMock;
  35. /**
  36. * @var FileRecorder|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $fileRecorderMock;
  39. /**
  40. * @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $directoryMock;
  43. /**
  44. * @var EncodedContext|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $encodedContextMock;
  47. /**
  48. * @var ObjectManagerHelper
  49. */
  50. private $objectManagerHelper;
  51. /**
  52. * @var ExportDataHandler
  53. */
  54. private $exportDataHandler;
  55. /**
  56. * @var string
  57. */
  58. private $subdirectoryPath = 'analytics/';
  59. /**
  60. * @var string
  61. */
  62. private $archiveName = 'data.tgz';
  63. /**
  64. * @return void
  65. */
  66. protected function setUp()
  67. {
  68. $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->archiveMock = $this->getMockBuilder(Archive::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->reportWriterMock = $this->getMockBuilder(ReportWriterInterface::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->cryptographerMock = $this->getMockBuilder(Cryptographer::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->fileRecorderMock = $this->getMockBuilder(FileRecorder::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->directoryMock = $this->getMockBuilder(WriteInterface::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->objectManagerHelper = new ObjectManagerHelper($this);
  90. $this->exportDataHandler = $this->objectManagerHelper->getObject(
  91. ExportDataHandler::class,
  92. [
  93. 'filesystem' => $this->filesystemMock,
  94. 'archive' => $this->archiveMock,
  95. 'reportWriter' => $this->reportWriterMock,
  96. 'cryptographer' => $this->cryptographerMock,
  97. 'fileRecorder' => $this->fileRecorderMock,
  98. 'subdirectoryPath' => $this->subdirectoryPath,
  99. 'archiveName' => $this->archiveName,
  100. ]
  101. );
  102. }
  103. /**
  104. * @param bool $isArchiveSourceDirectory
  105. * @dataProvider prepareExportDataDataProvider
  106. */
  107. public function testPrepareExportData($isArchiveSourceDirectory)
  108. {
  109. $tmpFilesDirectoryPath = $this->subdirectoryPath . 'tmp/';
  110. $archiveRelativePath = $this->subdirectoryPath . $this->archiveName;
  111. $archiveSource = $isArchiveSourceDirectory ? (__DIR__) : '/tmp/' . $tmpFilesDirectoryPath;
  112. $archiveAbsolutePath = '/tmp/' . $archiveRelativePath;
  113. $this->filesystemMock
  114. ->expects($this->once())
  115. ->method('getDirectoryWrite')
  116. ->with(DirectoryList::SYS_TMP)
  117. ->willReturn($this->directoryMock);
  118. $this->directoryMock
  119. ->expects($this->exactly(4))
  120. ->method('delete')
  121. ->withConsecutive(
  122. [$tmpFilesDirectoryPath],
  123. [$archiveRelativePath]
  124. );
  125. $this->directoryMock
  126. ->expects($this->exactly(4))
  127. ->method('getAbsolutePath')
  128. ->withConsecutive(
  129. [$tmpFilesDirectoryPath],
  130. [$tmpFilesDirectoryPath],
  131. [$archiveRelativePath],
  132. [$archiveRelativePath]
  133. )
  134. ->willReturnOnConsecutiveCalls(
  135. $archiveSource,
  136. $archiveSource,
  137. $archiveAbsolutePath,
  138. $archiveAbsolutePath
  139. );
  140. $this->reportWriterMock
  141. ->expects($this->once())
  142. ->method('write')
  143. ->with($this->directoryMock, $tmpFilesDirectoryPath);
  144. $this->directoryMock
  145. ->expects($this->exactly(2))
  146. ->method('isExist')
  147. ->withConsecutive(
  148. [$tmpFilesDirectoryPath],
  149. [$archiveRelativePath]
  150. )
  151. ->willReturnOnConsecutiveCalls(
  152. true,
  153. true
  154. );
  155. $this->directoryMock
  156. ->expects($this->once())
  157. ->method('create')
  158. ->with(dirname($archiveRelativePath));
  159. $this->archiveMock
  160. ->expects($this->once())
  161. ->method('pack')
  162. ->with(
  163. $archiveSource,
  164. $archiveAbsolutePath,
  165. $isArchiveSourceDirectory
  166. );
  167. $fileContent = 'Some text';
  168. $this->directoryMock
  169. ->expects($this->once())
  170. ->method('readFile')
  171. ->with($archiveRelativePath)
  172. ->willReturn($fileContent);
  173. $this->cryptographerMock
  174. ->expects($this->once())
  175. ->method('encode')
  176. ->with($fileContent)
  177. ->willReturn($this->encodedContextMock);
  178. $this->fileRecorderMock
  179. ->expects($this->once())
  180. ->method('recordNewFile')
  181. ->with($this->encodedContextMock);
  182. $this->assertTrue($this->exportDataHandler->prepareExportData());
  183. }
  184. /**
  185. * @return array
  186. */
  187. public function prepareExportDataDataProvider()
  188. {
  189. return [
  190. 'Data source for archive is directory' => [true],
  191. 'Data source for archive isn\'t directory' => [false],
  192. ];
  193. }
  194. /**
  195. * @return void
  196. * @expectedException \Magento\Framework\Exception\LocalizedException
  197. */
  198. public function testPrepareExportDataWithLocalizedException()
  199. {
  200. $tmpFilesDirectoryPath = $this->subdirectoryPath . 'tmp/';
  201. $archivePath = $this->subdirectoryPath . $this->archiveName;
  202. $this->filesystemMock
  203. ->expects($this->once())
  204. ->method('getDirectoryWrite')
  205. ->with(DirectoryList::SYS_TMP)
  206. ->willReturn($this->directoryMock);
  207. $this->reportWriterMock
  208. ->expects($this->once())
  209. ->method('write')
  210. ->with($this->directoryMock, $tmpFilesDirectoryPath);
  211. $this->directoryMock
  212. ->expects($this->exactly(3))
  213. ->method('delete')
  214. ->withConsecutive(
  215. [$tmpFilesDirectoryPath],
  216. [$tmpFilesDirectoryPath],
  217. [$archivePath]
  218. );
  219. $this->directoryMock
  220. ->expects($this->exactly(2))
  221. ->method('getAbsolutePath')
  222. ->with($tmpFilesDirectoryPath);
  223. $this->directoryMock
  224. ->expects($this->once())
  225. ->method('isExist')
  226. ->with($tmpFilesDirectoryPath)
  227. ->willReturn(false);
  228. $this->assertNull($this->exportDataHandler->prepareExportData());
  229. }
  230. }