Merge branch 'develop' into feature/vue-serverview
This commit is contained in:
commit
5ca13839cf
110 changed files with 7190 additions and 2093 deletions
|
@ -1,11 +1,4 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Nodes;
|
||||
|
||||
|
@ -63,17 +56,17 @@ class NodeUpdateServiceTest extends TestCase
|
|||
->expects($this->once())->willReturn('random_string');
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
'name' => 'NewName',
|
||||
'daemonSecret' => 'random_string',
|
||||
])->andReturn(true);
|
||||
])->andReturn($model);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
||||
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->returnUpdatedModel(false)->handle($model, ['name' => 'NewName', 'reset_secret' => true]);
|
||||
$this->assertTrue($response);
|
||||
$response = $this->getService()->handle($model, ['name' => 'NewName', 'reset_secret' => true]);
|
||||
$this->assertInstanceOf(Node::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,37 +76,17 @@ class NodeUpdateServiceTest extends TestCase
|
|||
{
|
||||
$model = factory(Node::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
||||
'name' => 'NewName',
|
||||
])->andReturn(true);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
||||
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->returnUpdatedModel(false)->handle($model, ['name' => 'NewName']);
|
||||
$this->assertTrue($response);
|
||||
}
|
||||
|
||||
public function testUpdatedModelIsReturned()
|
||||
{
|
||||
$model = factory(Node::class)->make();
|
||||
$updated = clone $model;
|
||||
$updated->name = 'NewName';
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
'name' => $updated->name,
|
||||
])->andReturn($updated);
|
||||
'name' => 'NewName',
|
||||
])->andReturn($model);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
|
||||
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->returnUpdatedModel()->handle($model, ['name' => $updated->name]);
|
||||
$response = $this->getService()->handle($model, ['name' => 'NewName']);
|
||||
$this->assertInstanceOf(Node::class, $response);
|
||||
$this->assertSame($updated, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +100,7 @@ class NodeUpdateServiceTest extends TestCase
|
|||
$model = factory(Node::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('withoutFreshModel->update')->andReturn(new Response);
|
||||
$this->repository->shouldReceive('update')->andReturn($model);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
@ -146,7 +119,7 @@ class NodeUpdateServiceTest extends TestCase
|
|||
$model = factory(Node::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('withoutFreshModel->update')->andReturn(new Response);
|
||||
$this->repository->shouldReceive('update')->andReturn($model);
|
||||
|
||||
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
|
||||
|
||||
|
|
|
@ -3,36 +3,32 @@
|
|||
namespace Tests\Unit\Services\Schedules;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use Cron\CronExpression;
|
||||
use Pterodactyl\Models\Task;
|
||||
use Pterodactyl\Models\Schedule;
|
||||
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
||||
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
||||
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
||||
|
||||
class ProcessScheduleServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Cron\CronExpression|\Mockery\Mock
|
||||
* @var \Illuminate\Contracts\Bus\Dispatcher|\Mockery\Mock
|
||||
*/
|
||||
protected $cron;
|
||||
private $dispatcher;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
private $scheduleRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService|\Mockery\Mock
|
||||
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $runnerService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Schedules\ProcessScheduleService
|
||||
*/
|
||||
protected $service;
|
||||
private $taskRepository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -40,12 +36,10 @@ class ProcessScheduleServiceTest extends TestCase
|
|||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$this->repository = m::mock(ScheduleRepositoryInterface::class);
|
||||
$this->runnerService = m::mock(RunTaskService::class);
|
||||
|
||||
$this->service = new ProcessScheduleService($this->runnerService, $this->repository);
|
||||
$this->dispatcher = m::mock(Dispatcher::class);
|
||||
$this->scheduleRepository = m::mock(ScheduleRepositoryInterface::class);
|
||||
$this->taskRepository = m::mock(TaskRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,37 +52,36 @@ class ProcessScheduleServiceTest extends TestCase
|
|||
'sequence_id' => 1,
|
||||
])]));
|
||||
|
||||
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
||||
$this->scheduleRepository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
||||
|
||||
$formatted = sprintf('%s %s %s * %s', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
$this->scheduleRepository->shouldReceive('update')->with($model->id, [
|
||||
'is_processing' => true,
|
||||
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
|
||||
]);
|
||||
|
||||
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
||||
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once();
|
||||
|
||||
$this->service->handle($model);
|
||||
$this->dispatcher->shouldReceive('dispatch')->with(m::on(function ($class) use ($model, $task) {
|
||||
$this->assertInstanceOf(RunTaskJob::class, $class);
|
||||
$this->assertSame($task->time_offset, $class->delay);
|
||||
$this->assertSame($task->id, $class->task);
|
||||
$this->assertSame($model->id, $class->schedule);
|
||||
|
||||
return true;
|
||||
}))->once();
|
||||
|
||||
$this->getService()->handle($model);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testScheduleRunTimeCanBeOverridden()
|
||||
/**
|
||||
* Return an instance of the service for testing purposes.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Schedules\ProcessScheduleService
|
||||
*/
|
||||
private function getService(): ProcessScheduleService
|
||||
{
|
||||
$model = factory(Schedule::class)->make();
|
||||
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
|
||||
'sequence_id' => 1,
|
||||
])]));
|
||||
|
||||
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
||||
|
||||
$this->repository->shouldReceive('update')->with($model->id, [
|
||||
'is_processing' => true,
|
||||
'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(),
|
||||
]);
|
||||
|
||||
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
||||
|
||||
$this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model);
|
||||
$this->assertTrue(true);
|
||||
return new ProcessScheduleService($this->dispatcher, $this->scheduleRepository, $this->taskRepository);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,90 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Schedules\Tasks;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Task;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
||||
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
|
||||
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
||||
|
||||
class RunTaskServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Bus\Dispatcher|\Mockery\Mock
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Bus::fake();
|
||||
$this->repository = m::mock(TaskRepositoryInterface::class);
|
||||
|
||||
$this->service = new RunTaskService($this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a job is dispatched.
|
||||
*/
|
||||
public function testTaskIsDispatched()
|
||||
{
|
||||
$task = factory(Task::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($task);
|
||||
|
||||
Bus::assertDispatched(RunTaskJob::class, function ($job) use ($task) {
|
||||
$this->assertEquals($task->id, $job->task, 'Assert job task matches parent task model.');
|
||||
$this->assertEquals($task->schedule_id, $job->schedule, 'Assert job is linked to correct schedule.');
|
||||
$this->assertEquals($task->time_offset, $job->delay, 'Assert job delay is set correctly to match task.');
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing an ID in place of a model works.
|
||||
*/
|
||||
public function testIdCanBePassedInPlaceOfModel()
|
||||
{
|
||||
$task = factory(Task::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('find')->with($task->id)->once()->andReturn($task);
|
||||
$this->repository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($task->id);
|
||||
|
||||
Bus::assertDispatched(RunTaskJob::class, function ($job) use ($task) {
|
||||
$this->assertEquals($task->id, $job->task, 'Assert job task matches parent task model.');
|
||||
$this->assertEquals($task->schedule_id, $job->schedule, 'Assert job is linked to correct schedule.');
|
||||
$this->assertEquals($task->time_offset, $job->delay, 'Assert job delay is set correctly to match task.');
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
Reference in a new issue