Skip to content

Commit

Permalink
Fix deleting all backups when using maximum storage (#562)
Browse files Browse the repository at this point in the history
* Add failing test for deleting backups when reaching max storage

* Update filepaths to match app_name in phpunit.xml.dist

* Fix failing test

* Remove obvious comment
  • Loading branch information
AlexVanderbist authored and freekmurze committed Nov 3, 2017
1 parent ed7e638 commit 6f9dd12
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/Tasks/Cleanup/Strategies/DefaultStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ protected function removeOldBackupsUntilUsingLessThanMaximumStorage(BackupCollec

$oldest->delete();

$backups = $backups->filter->exists();

$this->removeOldBackupsUntilUsingLessThanMaximumStorage($backups);
}
}
65 changes: 36 additions & 29 deletions tests/Integration/CleanupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ public function setUp()
/** @test */
public function it_can_remove_old_backups_until_using_less_than_maximum_storage()
{
// 1 megabyte storage size
$this->app['config']->set('backup.cleanup.defaultStrategy.deleteOldestBackupsWhenUsingMoreMegabytesThan', 1);
$this->app['config']->set('backup.cleanup.defaultStrategy.deleteOldestBackupsWhenUsingMoreMegabytesThan', 2);

$this->testHelper->createTempFile1Mb('mysite.com/test1.zip', Carbon::now()->subDays(1));
$this->testHelper->createTempFile1Mb('mysite.com/test2.zip', Carbon::now()->subDays(2));
$this->testHelper->createTempFile1Mb('mysite/test1.zip', Carbon::now()->subDays(1));
$this->testHelper->createTempFile1Mb('mysite/test2.zip', Carbon::now()->subDays(2));
$this->testHelper->createTempFile1Mb('mysite/test3.zip', Carbon::now()->subDays(3));
$this->testHelper->createTempFile1Mb('mysite/test4.zip', Carbon::now()->subDays(4));

Artisan::call('backup:clean');

$this->assertTempFilesExist([
'mysite.com/test1.zip',
'mysite/test1.zip',
'mysite/test2.zip',
]);

$this->assertTempFilesNotExist([
'mysite/test3.zip',
'mysite/test4.zip',
]);
}

Expand Down Expand Up @@ -122,18 +129,18 @@ public function it_can_remove_old_backups_from_the_backup_directory()
/** @test */
public function it_will_leave_non_zip_files_alone()
{
$this->testHelper->createTempFileWithAge('mysite.com/test1.txt', Carbon::now()->subDays(1));
$this->testHelper->createTempFileWithAge('mysite.com/test2.txt', Carbon::now()->subDays(2));
$this->testHelper->createTempFileWithAge('mysite.com/test1000.txt', Carbon::now()->subDays(1000));
$this->testHelper->createTempFileWithAge('mysite.com/test2000.txt', Carbon::now()->subDays(2000));
$this->testHelper->createTempFileWithAge('mysite/test1.txt', Carbon::now()->subDays(1));
$this->testHelper->createTempFileWithAge('mysite/test2.txt', Carbon::now()->subDays(2));
$this->testHelper->createTempFileWithAge('mysite/test1000.txt', Carbon::now()->subDays(1000));
$this->testHelper->createTempFileWithAge('mysite/test2000.txt', Carbon::now()->subDays(2000));

Artisan::call('backup:clean');

$this->assertTempFilesExist([
'mysite.com/test1.txt',
'mysite.com/test2.txt',
'mysite.com/test1000.txt',
'mysite.com/test2000.txt',
'mysite/test1.txt',
'mysite/test2.txt',
'mysite/test1000.txt',
'mysite/test2000.txt',
]);
}

Expand Down Expand Up @@ -164,18 +171,18 @@ public function it_should_trigger_the_cleanup_successful_event()
{
$this->expectsEvents(CleanupWasSuccessful::class);

$this->testHelper->createTempFileWithAge('mysite.com/test1.txt', Carbon::now()->subDays(1));
$this->testHelper->createTempFileWithAge('mysite.com/test2.txt', Carbon::now()->subDays(2));
$this->testHelper->createTempFileWithAge('mysite.com/test1000.txt', Carbon::now()->subDays(1000));
$this->testHelper->createTempFileWithAge('mysite.com/test2000.txt', Carbon::now()->subDays(2000));
$this->testHelper->createTempFileWithAge('mysite/test1.txt', Carbon::now()->subDays(1));
$this->testHelper->createTempFileWithAge('mysite/test2.txt', Carbon::now()->subDays(2));
$this->testHelper->createTempFileWithAge('mysite/test1000.txt', Carbon::now()->subDays(1000));
$this->testHelper->createTempFileWithAge('mysite/test2000.txt', Carbon::now()->subDays(2000));

Artisan::call('backup:clean');

$this->assertTempFilesExist([
'mysite.com/test1.txt',
'mysite.com/test2.txt',
'mysite.com/test1000.txt',
'mysite.com/test2000.txt',
'mysite/test1.txt',
'mysite/test2.txt',
'mysite/test1000.txt',
'mysite/test2000.txt',
]);
}

Expand All @@ -184,18 +191,18 @@ public function it_should_omit_the_cleanup_successful_event()
{
$this->doesntExpectEvents(CleanupWasSuccessful::class);

$this->testHelper->createTempFileWithAge('mysite.com/test1.txt', Carbon::now()->subDays(1));
$this->testHelper->createTempFileWithAge('mysite.com/test2.txt', Carbon::now()->subDays(2));
$this->testHelper->createTempFileWithAge('mysite.com/test1000.txt', Carbon::now()->subDays(1000));
$this->testHelper->createTempFileWithAge('mysite.com/test2000.txt', Carbon::now()->subDays(2000));
$this->testHelper->createTempFileWithAge('mysite/test1.txt', Carbon::now()->subDays(1));
$this->testHelper->createTempFileWithAge('mysite/test2.txt', Carbon::now()->subDays(2));
$this->testHelper->createTempFileWithAge('mysite/test1000.txt', Carbon::now()->subDays(1000));
$this->testHelper->createTempFileWithAge('mysite/test2000.txt', Carbon::now()->subDays(2000));

Artisan::call('backup:clean', ['--disable-notifications' => true]);

$this->assertTempFilesExist([
'mysite.com/test1.txt',
'mysite.com/test2.txt',
'mysite.com/test1000.txt',
'mysite.com/test2000.txt',
'mysite/test1.txt',
'mysite/test2.txt',
'mysite/test1000.txt',
'mysite/test2000.txt',
]);
}
}

0 comments on commit 6f9dd12

Please sign in to comment.