2017-09-20 03:10:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Commands\Maintenance;
|
|
|
|
|
2017-12-17 19:07:38 +00:00
|
|
|
use SplFileInfo;
|
2017-09-20 03:10:14 +00:00
|
|
|
use Mockery as m;
|
|
|
|
use Carbon\Carbon;
|
2017-09-22 05:30:09 +00:00
|
|
|
use Tests\Unit\Commands\CommandTestCase;
|
2017-09-20 03:10:14 +00:00
|
|
|
use Illuminate\Contracts\Filesystem\Factory;
|
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
|
|
use Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand;
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
class CleanServiceBackupFilesCommandTest extends CommandTestCase
|
2017-09-20 03:10:14 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand
|
|
|
|
*/
|
|
|
|
protected $command;
|
|
|
|
|
|
|
|
/**
|
2017-09-22 05:30:09 +00:00
|
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem|\Mockery\Mock
|
2017-09-20 03:10:14 +00:00
|
|
|
*/
|
|
|
|
protected $disk;
|
|
|
|
|
|
|
|
/**
|
2017-09-22 05:30:09 +00:00
|
|
|
* @var \Illuminate\Contracts\Filesystem\Factory|\Mockery\Mock
|
2017-09-20 03:10:14 +00:00
|
|
|
*/
|
|
|
|
protected $filesystem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
Carbon::setTestNow(Carbon::now());
|
2017-09-20 03:10:14 +00:00
|
|
|
$this->disk = m::mock(Filesystem::class);
|
|
|
|
$this->filesystem = m::mock(Factory::class);
|
|
|
|
$this->filesystem->shouldReceive('disk')->withNoArgs()->once()->andReturn($this->disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a file is deleted if it is > 5min old.
|
|
|
|
*/
|
|
|
|
public function testCommandCleansFilesMoreThan5MinutesOld()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$file = new SplFileInfo('testfile.txt');
|
|
|
|
|
|
|
|
$this->disk->shouldReceive('files')->with('services/.bak')->once()->andReturn([$file]);
|
|
|
|
$this->disk->shouldReceive('lastModified')->with($file->getPath())->once()->andReturn(Carbon::now()->subDays(100)->getTimestamp());
|
|
|
|
$this->disk->shouldReceive('delete')->with($file->getPath())->once()->andReturnNull();
|
2017-09-20 03:10:14 +00:00
|
|
|
|
2017-12-17 19:07:38 +00:00
|
|
|
$display = $this->runCommand($this->getCommand());
|
2017-09-20 03:10:14 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertContains(trans('command/messages.maintenance.deleting_service_backup', ['file' => 'testfile.txt']), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a file isn't deleted if it is < 5min old.
|
|
|
|
*/
|
|
|
|
public function testCommandDoesNotCleanFileLessThan5MinutesOld()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$file = new SplFileInfo('testfile.txt');
|
|
|
|
|
|
|
|
$this->disk->shouldReceive('files')->with('services/.bak')->once()->andReturn([$file]);
|
|
|
|
$this->disk->shouldReceive('lastModified')->with($file->getPath())->once()->andReturn(Carbon::now()->getTimestamp());
|
2017-09-20 03:10:14 +00:00
|
|
|
|
2017-12-17 19:07:38 +00:00
|
|
|
$display = $this->runCommand($this->getCommand());
|
2017-09-20 03:10:14 +00:00
|
|
|
|
|
|
|
$this->assertEmpty($display);
|
|
|
|
}
|
2017-12-17 19:07:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the command for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand
|
|
|
|
*/
|
|
|
|
private function getCommand(): CleanServiceBackupFilesCommand
|
|
|
|
{
|
|
|
|
$command = new CleanServiceBackupFilesCommand($this->filesystem);
|
|
|
|
$command->setLaravel($this->app);
|
|
|
|
|
|
|
|
return $command;
|
|
|
|
}
|
2017-09-20 03:10:14 +00:00
|
|
|
}
|