BackupRollbackTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Test\Unit;
  7. use Magento\Framework\Backup\Factory;
  8. use Magento\Framework\Setup\BackupRollback;
  9. use Magento\Framework\Setup\LoggerInterface;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class BackupRollbackTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $objectManager;
  19. /**
  20. * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $log;
  23. /**
  24. * @var \Magento\Framework\App\Filesystem\DirectoryList|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $directoryList;
  27. /**
  28. * @var BackupRollback
  29. */
  30. private $model;
  31. /**
  32. * @var \Magento\Framework\Filesystem\Driver\File|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $file;
  35. /**
  36. * @var \Magento\Framework\Backup\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $filesystem;
  39. /**
  40. * @var \Magento\Framework\Backup\Filesystem\Helper|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $helper;
  43. /**
  44. * @var \Magento\Framework\Backup\Db|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $database;
  47. /**
  48. * @var string
  49. */
  50. private $path;
  51. protected function setUp()
  52. {
  53. $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  54. $this->log = $this->createMock(\Magento\Framework\Setup\LoggerInterface::class);
  55. $this->directoryList = $this->createMock(\Magento\Framework\App\Filesystem\DirectoryList::class);
  56. $this->path = realpath(__DIR__);
  57. $this->directoryList->expects($this->any())
  58. ->method('getRoot')
  59. ->willReturn($this->path);
  60. $this->directoryList->expects($this->any())
  61. ->method('getPath')
  62. ->willReturn($this->path);
  63. $this->file = $this->createMock(\Magento\Framework\Filesystem\Driver\File::class);
  64. $this->filesystem = $this->createMock(\Magento\Framework\Backup\Filesystem::class);
  65. $this->database = $this->createMock(\Magento\Framework\Backup\Db::class);
  66. $this->helper = $this->createMock(\Magento\Framework\Backup\Filesystem\Helper::class);
  67. $this->helper->expects($this->any())
  68. ->method('getInfo')
  69. ->willReturn(['writable' => true, 'size' => 100]);
  70. $configLoader = $this->createMock(\Magento\Framework\App\ObjectManager\ConfigLoader::class);
  71. $configLoader->expects($this->any())
  72. ->method('load')
  73. ->willReturn([]);
  74. $this->objectManager->expects($this->any())
  75. ->method('get')
  76. ->will($this->returnValueMap([
  77. [
  78. \Magento\Framework\App\State::class, $this->createMock(\Magento\Framework\App\State::class)
  79. ],
  80. [\Magento\Framework\ObjectManager\ConfigLoaderInterface::class, $configLoader],
  81. ]));
  82. $this->objectManager->expects($this->any())
  83. ->method('create')
  84. ->will($this->returnValueMap([
  85. [\Magento\Framework\Backup\Filesystem\Helper::class, [], $this->helper],
  86. [\Magento\Framework\Backup\Filesystem::class, [], $this->filesystem],
  87. [\Magento\Framework\Backup\Db::class, [], $this->database],
  88. ]));
  89. $this->model = new BackupRollback(
  90. $this->objectManager,
  91. $this->log,
  92. $this->directoryList,
  93. $this->file,
  94. $this->helper
  95. );
  96. }
  97. public function testCodeBackup()
  98. {
  99. $this->setupCodeBackupRollback();
  100. $this->filesystem->expects($this->once())
  101. ->method('create');
  102. $this->file->expects($this->once())->method('isExists')->with($this->path . '/backups')->willReturn(false);
  103. $this->file->expects($this->once())->method('createDirectory')->with($this->path . '/backups', 0777);
  104. $this->model->codeBackup(time());
  105. }
  106. /**
  107. * @expectedException \Magento\Framework\Exception\LocalizedException
  108. * @expectedExceptionMessage This backup type \'txt\' is not supported.
  109. */
  110. public function testCodeBackupWithInvalidType()
  111. {
  112. $this->model->codeBackup(time(), 'txt');
  113. }
  114. public function testCodeRollback()
  115. {
  116. $this->filesystem->expects($this->once())->method('rollback');
  117. $this->setupCodeBackupRollback();
  118. $this->file->expects($this->once())
  119. ->method('isExists')
  120. ->with($this->path . '/backups/12345_filesystem_code.tgz')
  121. ->willReturn(true);
  122. $this->model->codeRollback('12345_filesystem_code.tgz');
  123. }
  124. /**
  125. * @expectedException \Magento\Framework\Exception\LocalizedException
  126. * @expectedExceptionMessage The rollback file doesn't exist. Verify the file and try again.
  127. */
  128. public function testCodeRollbackWithInvalidFilePath()
  129. {
  130. $this->file->expects($this->once())
  131. ->method('isExists')
  132. ->willReturn(false);
  133. $this->model->codeRollback('12345_filesystem_code.tgz');
  134. }
  135. /**
  136. * @expectedException \Magento\Framework\Exception\LocalizedException
  137. * @expectedExceptionMessage The rollback file is invalid. Verify the file and try again.
  138. */
  139. public function testCodeRollbackWithInvalidFileType()
  140. {
  141. $this->model->codeRollback('RollbackFile_A.txt');
  142. }
  143. public function testMediaBackup()
  144. {
  145. $this->setupCodeBackupRollback();
  146. $this->filesystem->expects($this->once())
  147. ->method('create');
  148. $this->file->expects($this->once())->method('isExists')->with($this->path . '/backups')->willReturn(false);
  149. $this->file->expects($this->once())->method('createDirectory')->with($this->path . '/backups', 0777);
  150. $this->model->codeBackup(time(), Factory::TYPE_MEDIA);
  151. }
  152. public function testMediaRollback()
  153. {
  154. $this->filesystem->expects($this->once())->method('rollback');
  155. $this->setupCodeBackupRollback();
  156. $this->file->expects($this->once())
  157. ->method('isExists')
  158. ->with($this->path . '/backups/12345_filesystem_media.tgz')
  159. ->willReturn(true);
  160. $this->model->codeRollback('12345_filesystem_media.tgz', Factory::TYPE_MEDIA);
  161. }
  162. public function testDbBackup()
  163. {
  164. $this->setupDbBackupRollback();
  165. $this->database->expects($this->once())->method('getBackupFilename')->willReturn('RollbackFile_A.gz');
  166. $this->database->expects($this->once())->method('create');
  167. $this->file->expects($this->once())->method('isExists')->willReturn(false);
  168. $this->file->expects($this->once())->method('createDirectory');
  169. $this->model->dbBackup(time());
  170. }
  171. public function testDbRollback()
  172. {
  173. $this->setupDbBackupRollback();
  174. $this->database->expects($this->once())->method('rollback');
  175. $this->database->expects($this->exactly(2))->method('getBackupFilename')
  176. ->willReturnOnConsecutiveCalls('test', '1510140748_db_test_backup');
  177. $this->database->expects($this->once())->method('getTime')->willReturn(1510140748);
  178. $this->database->expects($this->once())->method('getType')->willReturn('db');
  179. $this->database->expects($this->once())->method('setName')->with(' test backup');
  180. $this->file->expects($this->once())
  181. ->method('isExists')
  182. ->with($this->path . '/backups/1510140748_db_test_backup.sql')
  183. ->willReturn(true);
  184. $this->model->dbRollback('1510140748_db_test_backup.sql');
  185. }
  186. private function setupCodeBackupRollback()
  187. {
  188. $this->filesystem->expects($this->once())
  189. ->method('addIgnorePaths');
  190. $this->filesystem->expects($this->once())
  191. ->method('setBackupsDir');
  192. $this->filesystem->expects($this->once())
  193. ->method('setBackupExtension');
  194. $this->filesystem->expects($this->once())
  195. ->method('setTime');
  196. $this->filesystem->expects($this->once())
  197. ->method('getBackupFilename')
  198. ->willReturn('RollbackFile_A.tgz');
  199. $this->filesystem->expects($this->atLeastOnce())
  200. ->method('getBackupPath')
  201. ->willReturn('pathToFile/12345_filesystem_code.tgz');
  202. $this->log->expects($this->once())
  203. ->method('logSuccess');
  204. }
  205. private function setupDbBackupRollback()
  206. {
  207. $this->database->expects($this->once())
  208. ->method('setBackupsDir');
  209. $this->database->expects($this->once())
  210. ->method('setBackupExtension');
  211. $this->database->expects($this->once())
  212. ->method('setTime');
  213. $this->database->expects($this->atLeastOnce())
  214. ->method('getBackupPath')
  215. ->willReturn('pathToFile/12345_db.sql');
  216. $this->log->expects($this->once())
  217. ->method('logSuccess');
  218. }
  219. public function testGetFSDiskSpaceback()
  220. {
  221. $size = $this->model->getFSDiskSpace();
  222. $this->assertEquals(100, $size);
  223. }
  224. public function testGetDBDiskSpace()
  225. {
  226. $this->database->expects($this->once())->method('getDBSize')->willReturn(100);
  227. $size = $this->model->getDBDiskSpace();
  228. $this->assertEquals(100, $size);
  229. }
  230. }