forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskCreationServiceTest.php
More file actions
208 lines (185 loc) · 5.88 KB
/
TaskCreationServiceTest.php
File metadata and controls
208 lines (185 loc) · 5.88 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?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 Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
use Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException;
class TaskCreationServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->repository = m::mock(TaskRepositoryInterface::class);
$this->service = new TaskCreationService($this->repository);
}
/**
* Test that a task is created and a model is returned for the task.
*
* @dataProvider validIntervalProvider
*/
public function testTaskIsCreatedAndModelReturned($interval, $value, $final)
{
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make();
$this->repository->shouldReceive('create')->with([
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'action' => $task->action,
'payload' => $task->payload,
'time_offset' => $final,
])->once()->andReturn($task);
$response = $this->service->handle($schedule, [
'time_interval' => $interval,
'time_value' => $value,
'sequence_id' => 1,
'action' => $task->action,
'payload' => $task->payload,
]);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Task::class, $response);
$this->assertEquals($task, $response);
}
/**
* Test that no new model is returned when a task is created.
*/
public function testTaskIsCreatedAndModelIsNotReturned()
{
$schedule = factory(Schedule::class)->make();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
'time_offset' => 300,
])->once()->andReturn(true);
$response = $this->service->handle($schedule, [
'time_interval' => 'm',
'time_value' => 5,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
], false);
$this->assertNotEmpty($response);
$this->assertNotInstanceOf(Task::class, $response);
$this->assertTrue($response);
}
/**
* Test that an ID can be passed in place of the schedule model itself.
*/
public function testIdCanBePassedInPlaceOfScheduleModel()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'schedule_id' => 1234,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
'time_offset' => 300,
])->once()->andReturn(true);
$response = $this->service->handle(1234, [
'time_interval' => 'm',
'time_value' => 5,
'sequence_id' => 1,
'action' => 'test',
'payload' => 'testpayload',
], false);
$this->assertNotEmpty($response);
$this->assertNotInstanceOf(Task::class, $response);
$this->assertTrue($response);
}
/**
* Test exception is thrown if the interval is greater than 15 minutes.
*
* @dataProvider invalidIntervalProvider
*/
public function testExceptionIsThrownIfIntervalIsMoreThan15Minutes($interval, $value)
{
try {
$this->service->handle(1234, [
'time_interval' => $interval,
'time_value' => $value,
]);
} catch (DisplayException $exception) {
$this->assertInstanceOf(TaskIntervalTooLongException::class, $exception);
$this->assertEquals(trans('exceptions.tasks.chain_interval_too_long'), $exception->getMessage());
}
}
/**
* Test that exceptions are thrown if the Scheudle module or ID is invalid.
*
* @dataProvider invalidScheduleArgumentProvider
* @expectedException \InvalidArgumentException
*/
public function testExceptionIsThrownIfInvalidArgumentIsPassed($argument)
{
$this->service->handle($argument, []);
}
/**
* Provides valid time intervals to be used in tests.
*
* @return array
*/
public function validIntervalProvider()
{
return [
['s', 30, 30],
['s', 60, 60],
['s', 90, 90],
['m', 1, 60],
['m', 5, 300],
];
}
/**
* Return invalid time formats.
*
* @return array
*/
public function invalidIntervalProvider()
{
return [
['m', 15.1],
['m', 16],
['s', 901],
];
}
/**
* Return an array of invalid schedule data to test aganist.
*
* @return array
*/
public function invalidScheduleArgumentProvider()
{
return [
[123.456],
['string'],
['abc123'],
['123_test'],
[new Server()],
[Schedule::class],
];
}
}