Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackupRollbackTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class BackupRollbackTest extends \PHPUnit\Framework\TestCase
17 {
21  private $objectManager;
22 
26  private $log;
27 
31  private $directoryList;
32 
36  private $model;
37 
41  private $file;
42 
46  private $filesystem;
47 
51  private $helper;
52 
56  private $database;
57 
61  private $path;
62 
63  protected function setUp()
64  {
65  $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
66  $this->log = $this->createMock(\Magento\Framework\Setup\LoggerInterface::class);
67  $this->directoryList = $this->createMock(\Magento\Framework\App\Filesystem\DirectoryList::class);
68  $this->path = realpath(__DIR__);
69  $this->directoryList->expects($this->any())
70  ->method('getRoot')
71  ->willReturn($this->path);
72  $this->directoryList->expects($this->any())
73  ->method('getPath')
74  ->willReturn($this->path);
75  $this->file = $this->createMock(\Magento\Framework\Filesystem\Driver\File::class);
76  $this->filesystem = $this->createMock(\Magento\Framework\Backup\Filesystem::class);
77  $this->database = $this->createMock(\Magento\Framework\Backup\Db::class);
78  $this->helper = $this->createMock(\Magento\Framework\Backup\Filesystem\Helper::class);
79  $this->helper->expects($this->any())
80  ->method('getInfo')
81  ->willReturn(['writable' => true, 'size' => 100]);
82  $configLoader = $this->createMock(\Magento\Framework\App\ObjectManager\ConfigLoader::class);
83  $configLoader->expects($this->any())
84  ->method('load')
85  ->willReturn([]);
86  $this->objectManager->expects($this->any())
87  ->method('get')
88  ->will($this->returnValueMap([
89  [
90  \Magento\Framework\App\State::class, $this->createMock(\Magento\Framework\App\State::class)
91  ],
92  [\Magento\Framework\ObjectManager\ConfigLoaderInterface::class, $configLoader],
93  ]));
94  $this->objectManager->expects($this->any())
95  ->method('create')
96  ->will($this->returnValueMap([
97  [\Magento\Framework\Backup\Filesystem\Helper::class, [], $this->helper],
98  [\Magento\Framework\Backup\Filesystem::class, [], $this->filesystem],
99  [\Magento\Framework\Backup\Db::class, [], $this->database],
100  ]));
101  $this->model = new BackupRollback(
102  $this->objectManager,
103  $this->log,
104  $this->directoryList,
105  $this->file,
106  $this->helper
107  );
108  }
109 
110  public function testCodeBackup()
111  {
112  $this->setupCodeBackupRollback();
113  $this->filesystem->expects($this->once())
114  ->method('create');
115  $this->file->expects($this->once())->method('isExists')->with($this->path . '/backups')->willReturn(false);
116  $this->file->expects($this->once())->method('createDirectory')->with($this->path . '/backups', 0777);
117  $this->model->codeBackup(time());
118  }
119 
125  {
126  $this->model->codeBackup(time(), 'txt');
127  }
128 
129  public function testCodeRollback()
130  {
131  $this->filesystem->expects($this->once())->method('rollback');
132  $this->setupCodeBackupRollback();
133  $this->file->expects($this->once())
134  ->method('isExists')
135  ->with($this->path . '/backups/12345_filesystem_code.tgz')
136  ->willReturn(true);
137  $this->model->codeRollback('12345_filesystem_code.tgz');
138  }
139 
145  {
146  $this->file->expects($this->once())
147  ->method('isExists')
148  ->willReturn(false);
149  $this->model->codeRollback('12345_filesystem_code.tgz');
150  }
151 
157  {
158  $this->model->codeRollback('RollbackFile_A.txt');
159  }
160 
161  public function testMediaBackup()
162  {
163  $this->setupCodeBackupRollback();
164  $this->filesystem->expects($this->once())
165  ->method('create');
166  $this->file->expects($this->once())->method('isExists')->with($this->path . '/backups')->willReturn(false);
167  $this->file->expects($this->once())->method('createDirectory')->with($this->path . '/backups', 0777);
168  $this->model->codeBackup(time(), Factory::TYPE_MEDIA);
169  }
170 
171  public function testMediaRollback()
172  {
173  $this->filesystem->expects($this->once())->method('rollback');
174  $this->setupCodeBackupRollback();
175  $this->file->expects($this->once())
176  ->method('isExists')
177  ->with($this->path . '/backups/12345_filesystem_media.tgz')
178  ->willReturn(true);
179  $this->model->codeRollback('12345_filesystem_media.tgz', Factory::TYPE_MEDIA);
180  }
181 
182  public function testDbBackup()
183  {
184  $this->setupDbBackupRollback();
185  $this->database->expects($this->once())->method('getBackupFilename')->willReturn('RollbackFile_A.gz');
186  $this->database->expects($this->once())->method('create');
187  $this->file->expects($this->once())->method('isExists')->willReturn(false);
188  $this->file->expects($this->once())->method('createDirectory');
189  $this->model->dbBackup(time());
190  }
191 
192  public function testDbRollback()
193  {
194  $this->setupDbBackupRollback();
195 
196  $this->database->expects($this->once())->method('rollback');
197  $this->database->expects($this->exactly(2))->method('getBackupFilename')
198  ->willReturnOnConsecutiveCalls('test', '1510140748_db_test_backup');
199  $this->database->expects($this->once())->method('getTime')->willReturn(1510140748);
200  $this->database->expects($this->once())->method('getType')->willReturn('db');
201  $this->database->expects($this->once())->method('setName')->with(' test backup');
202 
203  $this->file->expects($this->once())
204  ->method('isExists')
205  ->with($this->path . '/backups/1510140748_db_test_backup.sql')
206  ->willReturn(true);
207 
208  $this->model->dbRollback('1510140748_db_test_backup.sql');
209  }
210 
211  private function setupCodeBackupRollback()
212  {
213  $this->filesystem->expects($this->once())
214  ->method('addIgnorePaths');
215  $this->filesystem->expects($this->once())
216  ->method('setBackupsDir');
217  $this->filesystem->expects($this->once())
218  ->method('setBackupExtension');
219  $this->filesystem->expects($this->once())
220  ->method('setTime');
221  $this->filesystem->expects($this->once())
222  ->method('getBackupFilename')
223  ->willReturn('RollbackFile_A.tgz');
224  $this->filesystem->expects($this->atLeastOnce())
225  ->method('getBackupPath')
226  ->willReturn('pathToFile/12345_filesystem_code.tgz');
227  $this->log->expects($this->once())
228  ->method('logSuccess');
229  }
230 
231  private function setupDbBackupRollback()
232  {
233  $this->database->expects($this->once())
234  ->method('setBackupsDir');
235  $this->database->expects($this->once())
236  ->method('setBackupExtension');
237  $this->database->expects($this->once())
238  ->method('setTime');
239  $this->database->expects($this->atLeastOnce())
240  ->method('getBackupPath')
241  ->willReturn('pathToFile/12345_db.sql');
242  $this->log->expects($this->once())
243  ->method('logSuccess');
244  }
245 
246  public function testGetFSDiskSpaceback()
247  {
248  $size = $this->model->getFSDiskSpace();
249  $this->assertEquals(100, $size);
250  }
251 
252  public function testGetDBDiskSpace()
253  {
254  $this->database->expects($this->once())->method('getDBSize')->willReturn(100);
255  $size = $this->model->getDBDiskSpace();
256  $this->assertEquals(100, $size);
257  }
258 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60