DownloadTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backup\Test\Unit\Controller\Adminhtml\Index;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. /**
  10. * @covers \Magento\Backup\Controller\Adminhtml\Index\Download
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class DownloadTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var \Magento\Backend\App\Action\Context
  21. */
  22. protected $context;
  23. /**
  24. * @var \Magento\Backup\Controller\Adminhtml\Index\Download
  25. */
  26. protected $downloadController;
  27. /**
  28. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $objectManagerMock;
  31. /**
  32. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $requestMock;
  35. /**
  36. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $responseMock;
  39. /**
  40. * @var \Magento\Backup\Model\BackupFactory|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $backupModelFactoryMock;
  43. /**
  44. * @var \Magento\Backup\Model\Backup|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $backupModelMock;
  47. /**
  48. * @var \Magento\Backup\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $dataHelperMock;
  51. /**
  52. * @var \Magento\Framework\App\Response\Http\FileFactory|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $fileFactoryMock;
  55. /**
  56. * @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $resultRawFactoryMock;
  59. /**
  60. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  61. */
  62. protected $resultRedirectFactoryMock;
  63. /**
  64. * @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. protected $resultRawMock;
  67. /**
  68. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  69. */
  70. protected $resultRedirectMock;
  71. protected function setUp()
  72. {
  73. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  74. ->getMock();
  75. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  76. ->getMock();
  77. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  78. ->getMock();
  79. $this->backupModelFactoryMock = $this->getMockBuilder(\Magento\Backup\Model\BackupFactory::class)
  80. ->disableOriginalConstructor()
  81. ->setMethods(['create'])
  82. ->getMock();
  83. $this->backupModelMock = $this->getMockBuilder(\Magento\Backup\Model\Backup::class)
  84. ->disableOriginalConstructor()
  85. ->setMethods(['getTime', 'exists', 'getSize', 'output'])
  86. ->getMock();
  87. $this->dataHelperMock = $this->getMockBuilder(\Magento\Backup\Helper\Data::class)
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->fileFactoryMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->resultRawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
  94. ->disableOriginalConstructor()
  95. ->setMethods(['create'])
  96. ->getMock();
  97. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  98. \Magento\Backend\Model\View\Result\RedirectFactory::class
  99. )->disableOriginalConstructor()
  100. ->setMethods(['create'])
  101. ->getMock();
  102. $this->resultRawMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $this->objectManager = new ObjectManager($this);
  109. $this->context = $this->objectManager->getObject(
  110. \Magento\Backend\App\Action\Context::class,
  111. [
  112. 'objectManager' => $this->objectManagerMock,
  113. 'request' => $this->requestMock,
  114. 'response' => $this->responseMock,
  115. 'resultRedirectFactory' => $this->resultRedirectFactoryMock
  116. ]
  117. );
  118. $this->downloadController = $this->objectManager->getObject(
  119. \Magento\Backup\Controller\Adminhtml\Index\Download::class,
  120. [
  121. 'context' => $this->context,
  122. 'backupModelFactory' => $this->backupModelFactoryMock,
  123. 'fileFactory' => $this->fileFactoryMock,
  124. 'resultRawFactory' => $this->resultRawFactoryMock,
  125. ]
  126. );
  127. }
  128. /**
  129. * @covers \Magento\Backup\Controller\Adminhtml\Index\Download::execute
  130. */
  131. public function testExecuteBackupFound()
  132. {
  133. $time = 1;
  134. $type = 'db';
  135. $filename = 'filename';
  136. $size = 10;
  137. $output = 'test';
  138. $this->backupModelMock->expects($this->atLeastOnce())
  139. ->method('getTime')
  140. ->willReturn($time);
  141. $this->backupModelMock->expects($this->atLeastOnce())
  142. ->method('exists')
  143. ->willReturn(true);
  144. $this->backupModelMock->expects($this->atLeastOnce())
  145. ->method('getSize')
  146. ->willReturn($size);
  147. $this->backupModelMock->expects($this->atLeastOnce())
  148. ->method('output')
  149. ->willReturn($output);
  150. $this->requestMock->expects($this->any())
  151. ->method('getParam')
  152. ->willReturnMap(
  153. [
  154. ['time', null, $time],
  155. ['type', null, $type]
  156. ]
  157. );
  158. $this->backupModelFactoryMock->expects($this->once())
  159. ->method('create')
  160. ->with($time, $type)
  161. ->willReturn($this->backupModelMock);
  162. $this->dataHelperMock->expects($this->once())
  163. ->method('generateBackupDownloadName')
  164. ->with($this->backupModelMock)
  165. ->willReturn($filename);
  166. $this->objectManagerMock->expects($this->once())
  167. ->method('get')
  168. ->with(\Magento\Backup\Helper\Data::class)
  169. ->willReturn($this->dataHelperMock);
  170. $this->fileFactoryMock->expects($this->once())
  171. ->method('create')->with(
  172. $filename,
  173. null,
  174. DirectoryList::VAR_DIR,
  175. 'application/octet-stream',
  176. $size
  177. )
  178. ->willReturn($this->responseMock);
  179. $this->resultRawMock->expects($this->once())
  180. ->method('setContents')
  181. ->with($output);
  182. $this->resultRawFactoryMock->expects($this->once())
  183. ->method('create')
  184. ->willReturn($this->resultRawMock);
  185. $this->assertSame($this->resultRawMock, $this->downloadController->execute());
  186. }
  187. /**
  188. * @covers \Magento\Backup\Controller\Adminhtml\Index\Download::execute
  189. * @param int $time
  190. * @param bool $exists
  191. * @param int $existsCount
  192. * @dataProvider executeBackupNotFoundDataProvider
  193. */
  194. public function testExecuteBackupNotFound($time, $exists, $existsCount)
  195. {
  196. $type = 'db';
  197. $this->backupModelMock->expects($this->atLeastOnce())
  198. ->method('getTime')
  199. ->willReturn($time);
  200. $this->backupModelMock->expects($this->exactly($existsCount))
  201. ->method('exists')
  202. ->willReturn($exists);
  203. $this->requestMock->expects($this->any())
  204. ->method('getParam')
  205. ->willReturnMap(
  206. [
  207. ['time', null, $time],
  208. ['type', null, $type]
  209. ]
  210. );
  211. $this->backupModelFactoryMock->expects($this->once())
  212. ->method('create')
  213. ->with($time, $type)
  214. ->willReturn($this->backupModelMock);
  215. $this->resultRedirectMock->expects($this->once())
  216. ->method('setPath')
  217. ->with('backup/*');
  218. $this->resultRedirectFactoryMock->expects($this->once())
  219. ->method('create')
  220. ->willReturn($this->resultRedirectMock);
  221. $this->assertSame($this->resultRedirectMock, $this->downloadController->execute());
  222. }
  223. /**
  224. * @return array
  225. */
  226. public function executeBackupNotFoundDataProvider()
  227. {
  228. return [
  229. [1, false, 1],
  230. [0, true, 0],
  231. [0, false, 0]
  232. ];
  233. }
  234. }