2017-09-20 03:10:14 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-20 03:10:14 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-20 03:10:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Commands\Maintenance;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
/**
|
2017-09-22 05:30:09 +00:00
|
|
|
* @var \Carbon\Carbon|\Mockery\Mock
|
2017-09-20 03:10:14 +00:00
|
|
|
*/
|
|
|
|
protected $carbon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
|
|
|
$this->carbon = m::mock(Carbon::class);
|
|
|
|
$this->disk = m::mock(Filesystem::class);
|
|
|
|
$this->filesystem = m::mock(Factory::class);
|
|
|
|
$this->filesystem->shouldReceive('disk')->withNoArgs()->once()->andReturn($this->disk);
|
|
|
|
|
|
|
|
$this->command = new CleanServiceBackupFilesCommand($this->carbon, $this->filesystem);
|
|
|
|
$this->command->setLaravel($this->app);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a file is deleted if it is > 5min old.
|
|
|
|
*/
|
|
|
|
public function testCommandCleansFilesMoreThan5MinutesOld()
|
|
|
|
{
|
|
|
|
$this->disk->shouldReceive('files')->with('services/.bak')->once()->andReturn(['testfile.txt']);
|
|
|
|
$this->disk->shouldReceive('lastModified')->with('testfile.txt')->once()->andReturn('disk:last:modified');
|
|
|
|
$this->carbon->shouldReceive('timestamp')->with('disk:last:modified')->once()->andReturnSelf();
|
|
|
|
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->carbon->shouldReceive('diffInMinutes')->with(null)->once()->andReturn(10);
|
|
|
|
$this->disk->shouldReceive('delete')->with('testfile.txt')->once()->andReturnNull();
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->runCommand($this->command);
|
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()
|
|
|
|
{
|
|
|
|
$this->disk->shouldReceive('files')->with('services/.bak')->once()->andReturn(['testfile.txt']);
|
|
|
|
$this->disk->shouldReceive('lastModified')->with('testfile.txt')->once()->andReturn('disk:last:modified');
|
|
|
|
$this->carbon->shouldReceive('timestamp')->with('disk:last:modified')->once()->andReturnSelf();
|
|
|
|
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->carbon->shouldReceive('diffInMinutes')->with(null)->once()->andReturn(2);
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->runCommand($this->command);
|
2017-09-20 03:10:14 +00:00
|
|
|
|
|
|
|
$this->assertEmpty($display);
|
|
|
|
}
|
|
|
|
}
|