public function testCancel() { $filesystem = $this->testStart(); /** @var \ZipArchive $archive */ $archive = $filesystem->getAdapter()->getArchive(); $this->filesystem->remove($archive->filename); $this->storage->cancel($filesystem); }
/** * 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; }
public function testBackupCancelOnBackup() { $nanbando = $this->getNanbando(['uploads' => ['plugin' => 'directory', 'parameter' => ['directory' => 'uploads']]]); $filesystem = new Filesystem(new MemoryAdapter()); $this->storage->start()->willReturn($filesystem); $this->storage->cancel($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]->cancel(); }); $this->eventDispatcher->dispatch(Events::POST_BACKUP_EVENT, Argument::type(PostBackupEvent::class))->shouldNotBeCalled(); $this->assertEquals(BackupStatus::STATE_FAILED, $nanbando->backup()); }