forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleCreationServiceTest.php
More file actions
108 lines (90 loc) · 3.57 KB
/
ScheduleCreationServiceTest.php
File metadata and controls
108 lines (90 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Tests\Unit\Services\Schedules;
use Mockery as m;
use Tests\TestCase;
use Cron\CronExpression;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Schedules\ScheduleCreationService;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleCreationServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Cron\CronExpression
*/
protected $cron;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\ScheduleCreationService
*/
protected $service;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
protected $taskCreationService;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->taskCreationService = m::mock(TaskCreationService::class);
$this->service = new ScheduleCreationService($this->connection, $this->repository, $this->taskCreationService);
}
/**
* Test that a schedule with no tasks can be created.
*/
public function testScheduleWithNoTasksIsCreated()
{
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_key' => 'value', 'server_id' => '123abc']);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
/**
* Test that a schedule with at least one task can be created.
*/
public function testScheduleWithTasksIsCreated()
{
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->taskCreationService->shouldReceive('handle')->with($schedule, [
'time_interval' => 'm',
'time_value' => 10,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
], false)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_key' => 'value'], [
['time_interval' => 'm', 'time_value' => 10, 'action' => 'test', 'payload' => 'testpayload'],
]);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
}