diff --git a/src/Commands/CleanCommand.php b/src/Commands/CleanCommand.php index 963d7ae1..222a2a35 100644 --- a/src/Commands/CleanCommand.php +++ b/src/Commands/CleanCommand.php @@ -33,8 +33,9 @@ public function fire() foreach($this->getTargetFileSystems() as $filesystem) { $disk = Storage::disk($filesystem); + $path = config('laravel-backup.destination.path'); - $filesToBeDeleted = (new FileSelector($disk))->getFilesOlderThan($expireDate, ['zip']); + $filesToBeDeleted = (new FileSelector($disk, $path))->getFilesOlderThan($expireDate, ['zip']); $filesDeleted = 0; foreach($filesToBeDeleted as $file) diff --git a/src/FileHelpers/FileSelector.php b/src/FileHelpers/FileSelector.php index bfc0974d..196a8146 100644 --- a/src/FileHelpers/FileSelector.php +++ b/src/FileHelpers/FileSelector.php @@ -5,10 +5,12 @@ class FileSelector { protected $disk; + protected $path; - public function __construct($disk) + public function __construct($disk, $path) { $this->disk = $disk; + $this->path = $path; } /** @@ -21,7 +23,7 @@ public function __construct($disk) */ public function getFilesOlderThan(DateTime $date, array $onlyIncludeFilesWithExtension) { - $allFiles = $this->disk->allFiles(); + $allFiles = $this->disk->allFiles($this->path); foreach($onlyIncludeFilesWithExtension as $extension) { diff --git a/tests/_data/backups/ElvisPresley.zip b/tests/_data/disk/root/TomJones.zip similarity index 100% rename from tests/_data/backups/ElvisPresley.zip rename to tests/_data/disk/root/TomJones.zip diff --git a/tests/_data/backups/JohnnyCash.zip b/tests/_data/disk/root/backups/ElvisPresley.zip similarity index 100% rename from tests/_data/backups/JohnnyCash.zip rename to tests/_data/disk/root/backups/ElvisPresley.zip diff --git a/tests/_data/disk/root/backups/JohnnyCash.zip b/tests/_data/disk/root/backups/JohnnyCash.zip new file mode 100644 index 00000000..e69de29b diff --git a/tests/_data/backups/MariahCarey.php b/tests/_data/disk/root/backups/MariahCarey.php similarity index 100% rename from tests/_data/backups/MariahCarey.php rename to tests/_data/disk/root/backups/MariahCarey.php diff --git a/tests/_data/disk/root/backups/test.zip b/tests/_data/disk/root/backups/test.zip new file mode 100644 index 00000000..e69de29b diff --git a/tests/fileSelector/FileSelectorTest.php b/tests/fileSelector/FileSelectorTest.php index ed62d9eb..1529b636 100644 --- a/tests/fileSelector/FileSelectorTest.php +++ b/tests/fileSelector/FileSelectorTest.php @@ -10,21 +10,31 @@ class FileSelectorTest extends Orchestra\Testbench\TestCase { protected $disk; + protected $root; + + protected $testFilesPath; + protected $fileSelector; public function setUp() { parent::setUp(); - $this->path = realpath('tests/_data/backups'); + + $this->root = realpath('tests/_data/disk/root'); + + $this->path = 'backups'; + + $this->testFilesPath = realpath($this->root . '/' . $this->path); //make sure all files in our testdirectory are 5 days old - foreach (scandir($this->path) as $file) + foreach (scandir($this->testFilesPath) as $file) { - touch($this->path . '/' . $file, time() - (60 * 60 * 24 * 5)); + + touch($this->testFilesPath . '/' . $file, time() - (60 * 60 * 24 * 5)); } - $this->disk = new Illuminate\Filesystem\FilesystemAdapter(new Filesystem(new Local($this->path))); - $this->fileSelector = new FileSelector($this->disk); + $this->disk = new Illuminate\Filesystem\FilesystemAdapter(new Filesystem(new Local($this->root))); + $this->fileSelector = new FileSelector($this->disk, $this->path); } /** @@ -56,17 +66,30 @@ public function it_gets_files_older_than_the_given_date() { $testFileName = 'test_it_gets_files_older_than_the_given_date.zip'; - touch($this->path . '/' .$testFileName , time() - (60 * 60 * 24 * 10) + 60); //create a file that is 10 days and a minute old + touch($this->testFilesPath . '/' .$testFileName , time() - (60 * 60 * 24 * 10) + 60); //create a file that is 10 days and a minute old $oldFiles = $this->fileSelector->getFilesOlderThan((new DateTime())->sub(new DateInterval('P9D')), ['zip']); - $this->assertTrue(in_array($testFileName, $oldFiles)); + + $this->assertTrue(in_array($this->path.'/'.$testFileName, $oldFiles)); $oldFiles = $this->fileSelector->getFilesOlderThan((new DateTime())->sub(new DateInterval('P10D')), ['zip']); - $this->assertFalse(in_array($testFileName, $oldFiles)); + $this->assertFalse(in_array($this->path.'/'.$testFileName, $oldFiles)); $oldFiles = $this->fileSelector->getFilesOlderThan((new DateTime())->sub(new DateInterval('P11D')), ['zip']); - $this->assertFalse(in_array($testFileName, $oldFiles)); + $this->assertFalse(in_array($this->path.'/'.$testFileName, $oldFiles)); + } + + /** + * @test + */ + public function it_excludes_files_outside_given_path() + { + $files = $this->fileSelector->getFilesOlderThan(new DateTime(), ['zip']); + + touch(realpath('tests/_data/disk/root/TomJones.zip'), time() - (60 * 60 * 24 * 10) + 60); + $this->assertFalse(in_array($this->path . '/' . 'TomJones.zip', $files)); + $this->assertTrue(in_array($this->path . '/' . 'test.zip', $files)); } /**