Skip to content

Commit

Permalink
Merge pull request #19 from MatthiasDeWinter/master
Browse files Browse the repository at this point in the history
Restores the path property of FileSelector and update the test accordingly
  • Loading branch information
freekmurze committed Mar 19, 2015
2 parents 8672209 + ce07121 commit cfd9264
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/FileHelpers/FileSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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)
{
Expand Down
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
Empty file.
41 changes: 32 additions & 9 deletions tests/fileSelector/FileSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit cfd9264

Please sign in to comment.