Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restores the path property of FileSelector and update the test accordingly #19

Merged
merged 4 commits into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Empty file.
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