From 0da9c4c3ec6035a99b8d290c2ae38beec0c222f3 Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Thu, 19 Mar 2015 08:45:57 +0000 Subject: [PATCH 1/4] restore path in FileSelector --- src/Commands/CleanCommand.php | 3 ++- src/FileHelpers/FileSelector.php | 4 +++- tests/fileSelector/FileSelectorTest.php | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) 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..e1dac447 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; } /** diff --git a/tests/fileSelector/FileSelectorTest.php b/tests/fileSelector/FileSelectorTest.php index ed62d9eb..8a757fcb 100644 --- a/tests/fileSelector/FileSelectorTest.php +++ b/tests/fileSelector/FileSelectorTest.php @@ -24,7 +24,7 @@ public function setUp() } $this->disk = new Illuminate\Filesystem\FilesystemAdapter(new Filesystem(new Local($this->path))); - $this->fileSelector = new FileSelector($this->disk); + $this->fileSelector = new FileSelector($this->disk, $this->path); } /** From 4ef7f1cdda26791291c7e26ba100f024cc412a3f Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Thu, 19 Mar 2015 11:18:06 +0000 Subject: [PATCH 2/4] update test --- src/FileHelpers/FileSelector.php | 2 +- src/config/laravel-backup.php | 8 ++-- .../root/TomJones.zip} | 0 .../root/backups/ElvisPresley.zip} | 0 tests/_data/disk/root/backups/JohnnyCash.zip | 0 .../{ => disk/root}/backups/MariahCarey.php | 0 tests/fileSelector/FileSelectorTest.php | 39 +++++++++++++++---- 7 files changed, 36 insertions(+), 13 deletions(-) rename tests/_data/{backups/ElvisPresley.zip => disk/root/TomJones.zip} (100%) rename tests/_data/{backups/JohnnyCash.zip => disk/root/backups/ElvisPresley.zip} (100%) create mode 100644 tests/_data/disk/root/backups/JohnnyCash.zip rename tests/_data/{ => disk/root}/backups/MariahCarey.php (100%) diff --git a/src/FileHelpers/FileSelector.php b/src/FileHelpers/FileSelector.php index e1dac447..196a8146 100644 --- a/src/FileHelpers/FileSelector.php +++ b/src/FileHelpers/FileSelector.php @@ -23,7 +23,7 @@ public function __construct($disk, $path) */ public function getFilesOlderThan(DateTime $date, array $onlyIncludeFilesWithExtension) { - $allFiles = $this->disk->allFiles(); + $allFiles = $this->disk->allFiles($this->path); foreach($onlyIncludeFilesWithExtension as $extension) { diff --git a/src/config/laravel-backup.php b/src/config/laravel-backup.php index 108aad1f..d79af90b 100644 --- a/src/config/laravel-backup.php +++ b/src/config/laravel-backup.php @@ -33,26 +33,26 @@ 'destination' => [ /* - * The filesystem(s) you on which the backups will be stored. Choose one or more + * The filesystem(s) you on which the disk will be stored. Choose one or more * of the filesystems you configured in app/config/filesystems.php */ 'filesystem' => ['local'], /* - * The path where the backups will be saved. This path + * The path where the disk will be saved. This path * is relative to the root you configured on your chosen * filesystem(s). * * If you're using the local filesystem a .gitignore file will * be automatically placed in this directory so you don't - * accidentally end up committing these backups. + * accidentally end up committing these disk. */ 'path' => 'backups', ], 'clean' => [ /* - * The clean command will remove all backups on all configured filesystems + * The clean command will remove all disk on all configured filesystems * that are older then this amount of days. */ 'maxAgeInDays' => 90, 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/fileSelector/FileSelectorTest.php b/tests/fileSelector/FileSelectorTest.php index 8a757fcb..1529b636 100644 --- a/tests/fileSelector/FileSelectorTest.php +++ b/tests/fileSelector/FileSelectorTest.php @@ -10,20 +10,30 @@ 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->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)); } /** From 37fa92aea2fa76bd65cfc1272428e11215f9e467 Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Thu, 19 Mar 2015 11:21:42 +0000 Subject: [PATCH 3/4] fix config documentation --- src/config/laravel-backup.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config/laravel-backup.php b/src/config/laravel-backup.php index d79af90b..108aad1f 100644 --- a/src/config/laravel-backup.php +++ b/src/config/laravel-backup.php @@ -33,26 +33,26 @@ 'destination' => [ /* - * The filesystem(s) you on which the disk will be stored. Choose one or more + * The filesystem(s) you on which the backups will be stored. Choose one or more * of the filesystems you configured in app/config/filesystems.php */ 'filesystem' => ['local'], /* - * The path where the disk will be saved. This path + * The path where the backups will be saved. This path * is relative to the root you configured on your chosen * filesystem(s). * * If you're using the local filesystem a .gitignore file will * be automatically placed in this directory so you don't - * accidentally end up committing these disk. + * accidentally end up committing these backups. */ 'path' => 'backups', ], 'clean' => [ /* - * The clean command will remove all disk on all configured filesystems + * The clean command will remove all backups on all configured filesystems * that are older then this amount of days. */ 'maxAgeInDays' => 90, From ce071217d0645a8b4f27241d4a14aebed37afcc2 Mon Sep 17 00:00:00 2001 From: MatthiasDeWinter Date: Thu, 19 Mar 2015 12:00:25 +0000 Subject: [PATCH 4/4] add missing test file --- tests/_data/disk/root/backups/test.zip | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/_data/disk/root/backups/test.zip 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