/** * Backup Gists * * If the time of last backup is less than 5 minutes ago, interrupts and returns a string. * Otherwise returns the archived file response. * * @return string | \Symfony\Component\HttpFoundation\BinaryFileResponse */ public function backup() { $user = $this->userFinder->getAuthenticatedUser(); $userCanBackup = GistBackupHandler::checkLastBackupTime($user); if (!$userCanBackup) { return 'WAIT_A_MINUTE'; } else { $gists = $this->gistFinder->getAll(); $gistBackupHandler = $this->gistBackupHandlerFactory->getInstance($user, $gists); $zipPath = $gistBackupHandler->backup(); return Response::download($zipPath, null, ['Set-Cookie' => 'fileDownload=true; path=/']); } }
public function testBackup() { $this->githubApiMock->shouldReceive('getGistFileContents')->twice()->andReturn('test'); $user = new User(); $user->setId(1); $user->setLogin('test'); $userFolderPath = storage_path() . '/backups/' . $user->getId(); $gist1 = new Gist(); $gist1->setUpdatedAt('2014-06-12 12:30:15'); $gist1->setFiles(['test.md' => ['filename' => 'test.md', 'size' => 1000, 'raw_url' => 'https://gist.githubusercontent.com/raw/365370/7f2ac0ff512853564e/ring.erl']]); $gist2 = new Gist(); $gist2->setUpdatedAt('2000-06-12 12:30:15'); $gist2->setFiles(['test.md' => ['filename' => 'test.md', 'size' => 1000, 'raw_url' => 'https://gist.githubusercontent.com/raw/365370/7f2ac0ff512853564e/ring.erl']]); $gists = [$gist1, $gist2]; $gistBackupHandler = new GistBackupHandler($this->githubApiMock, $user, $gists); $zipPath = $gistBackupHandler->backup(); $this->assertFileExists($zipPath); $this->assertFileNotExists($userFolderPath . '/files/20140612123015-test.md'); $this->assertFileNotExists($userFolderPath . '/files/20000613123015-test.md'); unlink($zipPath); rmdir($userFolderPath . '/files'); rmdir($userFolderPath . '/zip'); rmdir($userFolderPath); }