public function testCloseLabel() { $filesystem = $this->testStart(); $name = date('H-i-s-Y-m-d'); $this->localFilesystem->putStream('test/' . $name . '_test.zip', Argument::any())->shouldBeCalled(); $this->slugify->slugify('test')->willReturn('test'); $result = $this->storage->close($filesystem, 'test'); $this->assertEquals($name . '_test', $result); }
public function testBackupCancelOnBackupGoOn() { $nanbando = $this->getNanbando(['uploads' => ['plugin' => 'directory', 'parameter' => ['directory' => 'uploads']]]); $filesystem = new Filesystem(new MemoryAdapter()); $this->storage->start()->willReturn($filesystem); $this->storage->close($filesystem)->shouldBeCalled(); $this->eventDispatcher->dispatch(Events::PRE_BACKUP_EVENT, Argument::type(PreBackupEvent::class))->shouldBeCalled(); $this->eventDispatcher->dispatch(Events::BACKUP_EVENT, Argument::type(BackupEvent::class))->will(function ($arguments) { $arguments[1]->setStatus(BackupStatus::STATE_FAILED); }); $this->eventDispatcher->dispatch(Events::POST_BACKUP_EVENT, Argument::type(PostBackupEvent::class))->shouldBeCalled(); $this->assertEquals(BackupStatus::STATE_PARTIALLY, $nanbando->backup()); $files = $filesystem->listContents('', true); $fileNames = array_map(function ($item) { return $item['path']; }, $files); $this->assertEquals(['database', 'database/backup', 'database/backup/uploads.json', 'database/system.json'], $fileNames); }
/** * Backup process. * * @param string $label * @param string $message * * @return int */ public function backup($label = '', $message = '') { $destination = $this->storage->start(); $source = new Filesystem(new ReadonlyAdapter(new Local(realpath('.')))); $source->addPlugin(new ListFiles()); $source->addPlugin(new HashPlugin()); $systemDatabase = $this->databaseFactory->create(); $systemDatabase->set('label', $label); $systemDatabase->set('message', $message); $systemDatabase->set('started', (new \DateTime())->format(\DateTime::RFC3339)); $event = new PreBackupEvent($this->backup, $systemDatabase, $source, $destination); $this->eventDispatcher->dispatch(Events::PRE_BACKUP_EVENT, $event); if ($event->isCanceled()) { $this->storage->cancel($destination); return BackupStatus::STATE_FAILED; } $status = BackupStatus::STATE_SUCCESS; $backupConfig = $event->getBackup(); foreach ($backupConfig as $backupName => $backup) { $backupDestination = new Filesystem(new PrefixAdapter('backup/' . $backupName, $destination->getAdapter())); $backupDestination->addPlugin(new ListFiles()); $backupDestination->addPlugin(new HashPlugin()); $database = $this->databaseFactory->create(); $event = new BackupEvent($systemDatabase, $database, $source, $backupDestination, $backupName, $backup); $this->eventDispatcher->dispatch(Events::BACKUP_EVENT, $event); if ($event->isCanceled()) { $this->storage->cancel($destination); return BackupStatus::STATE_FAILED; } if ($event->getStatus() === BackupStatus::STATE_FAILED) { $status = BackupStatus::STATE_PARTIALLY; } $encodedData = json_encode($database->getAll(), JSON_PRETTY_PRINT); $destination->put(sprintf('database/backup/%s.json', $event->getName()), $encodedData); } $systemDatabase->set('finished', (new \DateTime())->format(\DateTime::RFC3339)); $systemDatabase->set('state', $status); $encodedSystemData = json_encode($systemDatabase->getAll(), JSON_PRETTY_PRINT); $destination->put('database/system.json', $encodedSystemData); $name = $this->storage->close($destination); $this->eventDispatcher->dispatch(Events::POST_BACKUP_EVENT, new PostBackupEvent($name, $status)); return $status; }