Skip to content

Commit cfd9264

Browse files
committed
Merge pull request #19 from MatthiasDeWinter/master
Restores the path property of FileSelector and update the test accordingly
2 parents 8672209 + ce07121 commit cfd9264

File tree

8 files changed

+38
-12
lines changed

8 files changed

+38
-12
lines changed

src/Commands/CleanCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public function fire()
3333
foreach($this->getTargetFileSystems() as $filesystem)
3434
{
3535
$disk = Storage::disk($filesystem);
36+
$path = config('laravel-backup.destination.path');
3637

37-
$filesToBeDeleted = (new FileSelector($disk))->getFilesOlderThan($expireDate, ['zip']);
38+
$filesToBeDeleted = (new FileSelector($disk, $path))->getFilesOlderThan($expireDate, ['zip']);
3839

3940
$filesDeleted = 0;
4041
foreach($filesToBeDeleted as $file)

src/FileHelpers/FileSelector.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
class FileSelector {
66

77
protected $disk;
8+
protected $path;
89

9-
public function __construct($disk)
10+
public function __construct($disk, $path)
1011
{
1112
$this->disk = $disk;
13+
$this->path = $path;
1214
}
1315

1416
/**
@@ -21,7 +23,7 @@ public function __construct($disk)
2123
*/
2224
public function getFilesOlderThan(DateTime $date, array $onlyIncludeFilesWithExtension)
2325
{
24-
$allFiles = $this->disk->allFiles();
26+
$allFiles = $this->disk->allFiles($this->path);
2527

2628
foreach($onlyIncludeFilesWithExtension as $extension)
2729
{

tests/_data/disk/root/backups/JohnnyCash.zip

Whitespace-only changes.

tests/_data/disk/root/backups/test.zip

Whitespace-only changes.

tests/fileSelector/FileSelectorTest.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,31 @@ class FileSelectorTest extends Orchestra\Testbench\TestCase {
1010

1111
protected $disk;
1212

13+
protected $root;
14+
15+
protected $testFilesPath;
16+
1317
protected $fileSelector;
1418

1519
public function setUp()
1620
{
1721
parent::setUp();
18-
$this->path = realpath('tests/_data/backups');
22+
23+
$this->root = realpath('tests/_data/disk/root');
24+
25+
$this->path = 'backups';
26+
27+
$this->testFilesPath = realpath($this->root . '/' . $this->path);
1928

2029
//make sure all files in our testdirectory are 5 days old
21-
foreach (scandir($this->path) as $file)
30+
foreach (scandir($this->testFilesPath) as $file)
2231
{
23-
touch($this->path . '/' . $file, time() - (60 * 60 * 24 * 5));
32+
33+
touch($this->testFilesPath . '/' . $file, time() - (60 * 60 * 24 * 5));
2434
}
2535

26-
$this->disk = new Illuminate\Filesystem\FilesystemAdapter(new Filesystem(new Local($this->path)));
27-
$this->fileSelector = new FileSelector($this->disk);
36+
$this->disk = new Illuminate\Filesystem\FilesystemAdapter(new Filesystem(new Local($this->root)));
37+
$this->fileSelector = new FileSelector($this->disk, $this->path);
2838
}
2939

3040
/**
@@ -56,17 +66,30 @@ public function it_gets_files_older_than_the_given_date()
5666
{
5767
$testFileName = 'test_it_gets_files_older_than_the_given_date.zip';
5868

59-
touch($this->path . '/' .$testFileName , time() - (60 * 60 * 24 * 10) + 60); //create a file that is 10 days and a minute old
69+
touch($this->testFilesPath . '/' .$testFileName , time() - (60 * 60 * 24 * 10) + 60); //create a file that is 10 days and a minute old
6070

6171
$oldFiles = $this->fileSelector->getFilesOlderThan((new DateTime())->sub(new DateInterval('P9D')), ['zip']);
62-
$this->assertTrue(in_array($testFileName, $oldFiles));
72+
73+
$this->assertTrue(in_array($this->path.'/'.$testFileName, $oldFiles));
6374

6475
$oldFiles = $this->fileSelector->getFilesOlderThan((new DateTime())->sub(new DateInterval('P10D')), ['zip']);
65-
$this->assertFalse(in_array($testFileName, $oldFiles));
76+
$this->assertFalse(in_array($this->path.'/'.$testFileName, $oldFiles));
6677

6778
$oldFiles = $this->fileSelector->getFilesOlderThan((new DateTime())->sub(new DateInterval('P11D')), ['zip']);
68-
$this->assertFalse(in_array($testFileName, $oldFiles));
79+
$this->assertFalse(in_array($this->path.'/'.$testFileName, $oldFiles));
80+
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function it_excludes_files_outside_given_path()
86+
{
87+
$files = $this->fileSelector->getFilesOlderThan(new DateTime(), ['zip']);
88+
89+
touch(realpath('tests/_data/disk/root/TomJones.zip'), time() - (60 * 60 * 24 * 10) + 60);
6990

91+
$this->assertFalse(in_array($this->path . '/' . 'TomJones.zip', $files));
92+
$this->assertTrue(in_array($this->path . '/' . 'test.zip', $files));
7093
}
7194

7295
/**

0 commit comments

Comments
 (0)