-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTaskTest.php
300 lines (225 loc) · 8.59 KB
/
TaskTest.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
use CodeIgniter\I18n\Time;
use CodeIgniter\Tasks\Exceptions\TasksException;
use CodeIgniter\Tasks\Task;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use Tests\Support\TasksTestCase;
/**
* @internal
*/
final class TaskTest extends TasksTestCase
{
use DatabaseTestTrait;
protected $namespace;
protected function setUp(): void
{
parent::setUp();
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();
CITestStreamFilter::addErrorFilter();
}
protected function tearDown(): void
{
parent::tearDown();
CITestStreamFilter::removeOutputFilter();
CITestStreamFilter::removeErrorFilter();
}
protected function getBuffer(): string
{
return CITestStreamFilter::$buffer;
}
public function testNamed()
{
$task = new Task('command', 'foo:bar');
// Will build a random name
$this->assertSame(0, strpos($task->name, 'command_'));
$task = (new Task('command', 'foo:bar'))->named('foo');
$this->assertSame('foo', $task->name);
}
public function testConstructSavesAction()
{
$task = new Task('command', 'foo:bar');
$result = $this->getPrivateProperty($task, 'action');
$this->assertSame('foo:bar', $result);
}
public function testGetAction()
{
$task = new Task('command', 'foo:bar');
$this->assertSame('foo:bar', $task->getAction());
}
public function testGetType()
{
$task = new Task('command', 'foo:bar');
$this->assertSame('command', $task->getType());
}
public function testCommandRunsCommand()
{
$task = new Task('command', 'tasks:example');
$task->run();
$this->assertStringContainsString(
'Commands can output text.',
$this->getBuffer(),
);
}
/**
* `command()` is not buffering the output like it appears it should,
* so the result is not actually being returned. Disabling this test
* until the root issue can be resolved.
*/
// public function testCommandReturnsOutput()
// {
// $task = new Task('command', 'tasks:test');
// $result = $task->run();
//
// $this->assertEquals('Commands can output text.', $result);
// }
public function testShouldRunSimple()
{
$task = (new Task('command', 'tasks:test'))->hourly();
$this->assertFalse($task->shouldRun('12:05am'));
$this->assertTrue($task->shouldRun('12:00am'));
}
public function testShouldRunWithEnvironments()
{
$originalEnv = $_SERVER['CI_ENVIRONMENT'];
$_SERVER['CI_ENVIRONMENT'] = 'development';
$task = (new Task('command', 'tasks:test'))->environments('development');
$this->assertTrue($task->shouldRun('12:00am'));
$_SERVER['CI_ENVIRONMENT'] = 'production';
$this->assertFalse($task->shouldRun('12:00am'));
$_SERVER['CI_ENVIRONMENT'] = $originalEnv;
}
public function testLastRun()
{
helper('setting');
setting('Tasks.logPerformance', true);
$task = new Task('closure', static fn () => 1);
$task->named('foo');
// Should be dashes when not ran
$this->assertSame('--', $task->lastRun());
$date = date('Y-m-d H:i:s');
// Insert a performance bit in the db
setting("Tasks.log-{$task->name}", [[
'task' => $task->name,
'type' => $task->getType(),
'start' => $date,
'duration' => '11.3s',
'output' => null,
'error' => null,
]]);
// Should return the current time
$this->assertInstanceOf(Time::class, $task->lastRun()); // @phpstan-ignore-line
$this->assertSame($date, $task->lastRun()->format('Y-m-d H:i:s'));
}
public function testSingleInstanceMethod()
{
$task = new Task('command', 'foo:bar');
$this->assertFalse($this->getPrivateProperty($task, 'singleInstance'));
$result = $task->singleInstance();
$this->assertTrue($this->getPrivateProperty($task, 'singleInstance'));
$this->assertNull($this->getPrivateProperty($task, 'singleInstanceTTL'));
$this->assertSame($task, $result);
// Test with custom TTL
$task->singleInstance(3600);
$this->assertTrue($this->getPrivateProperty($task, 'singleInstance'));
$this->assertSame(3600, $this->getPrivateProperty($task, 'singleInstanceTTL'));
}
public function testGetLockKey()
{
$task = new Task('command', 'foo:bar');
$task->named('test_task');
$method = $this->getPrivateMethodInvoker($task, 'getLockKey');
$expected = 'task_lock_test_task';
$this->assertSame($expected, $method());
// Test with unnamed task - should use dynamic name
$task = new Task('command', 'foo:bar');
$method = $this->getPrivateMethodInvoker($task, 'getLockKey');
// Should use task name from magic getter
$expected = 'task_lock_' . $task->name;
$this->assertSame($expected, $method());
}
public function testNamedTaskLockConsistency()
{
// Create two different closure tasks with the same name
$closure1 = static fn () => 'test1';
$closure2 = static function () {
return 'test2'; // Different functionality
};
$task1 = new Task('closure', $closure1);
$task2 = new Task('closure', $closure2);
// If they have the same name, they should have the same lock key
$task1->named('same_name');
$task2->named('same_name');
$getLockKey1 = $this->getPrivateMethodInvoker($task1, 'getLockKey');
$getLockKey2 = $this->getPrivateMethodInvoker($task2, 'getLockKey');
$this->assertSame($getLockKey1(), $getLockKey2());
// Different names should produce different keys
$task3 = new Task('closure', $closure1);
$task3->named('different_name');
$getLockKey3 = $this->getPrivateMethodInvoker($task3, 'getLockKey');
$this->assertNotSame($getLockKey1(), $getLockKey3());
}
public function testShouldRunWithSingleInstance()
{
$task = (new Task('command', 'foo:bar'))
->named('test_should_run')
->hourly()
->singleInstance();
// Should run at the right time with no existing lock
$this->assertTrue($task->shouldRun('12:00am'));
// Create a lock
$lockKey = $this->getPrivateMethodInvoker($task, 'getLockKey')();
cache()->save($lockKey, [], 3600);
// Should not run if a lock exists
$this->assertFalse($task->shouldRun('12:00am'));
cache()->delete($lockKey);
}
public function testRunWithSingleInstance()
{
$task = new Task('closure', static fn () => 'task executed');
$task->named('test_run_single');
$task->singleInstance();
$result = $task->run();
$this->assertSame('task executed', $result);
$lockKey = $this->getPrivateMethodInvoker($task, 'getLockKey')();
$this->assertNull(cache()->get($lockKey));
}
public function testLockReleasedAfterException()
{
$task = new Task('command', 'invalid:command');
$task->named('test_exception');
$task->singleInstance();
$reflection = new ReflectionClass($task);
$property = $reflection->getProperty('type');
$property->setValue($task, 'invalid_type');
$lockKey = $this->getPrivateMethodInvoker($task, 'getLockKey')();
try {
$task->run();
$this->fail('Expected exception was not thrown');
} catch (Exception $e) {
$this->assertInstanceOf(TasksException::class, $e);
}
$this->assertNull(cache()->get($lockKey));
}
public function testSingleInstanceWithCustomTTL()
{
$task = new Task('closure', static fn () => 'done');
$task->named('test_ttl');
$task->singleInstance(60);
$this->assertSame(60, $this->getPrivateProperty($task, 'singleInstanceTTL'));
$task2 = new Task('closure', static fn () => 'done');
$task2->named('test_no_ttl');
$task2->singleInstance();
$this->assertNull($this->getPrivateProperty($task2, 'singleInstanceTTL'));
}
}