forked from codeigniter4/tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskRunnerTest.php
More file actions
137 lines (116 loc) · 3.77 KB
/
TaskRunnerTest.php
File metadata and controls
137 lines (116 loc) · 3.77 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
use CodeIgniter\Tasks\Task;
use CodeIgniter\Tasks\TaskRunner;
use CodeIgniter\Test\CIUnitTestCase as TestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Services;
/**
* @internal
*/
final class TaskRunnerTest extends TestCase
{
use DatabaseTestTrait;
protected $refresh = true;
protected $namespace;
protected function setUp(): void
{
parent::setUp();
helper('setting');
setting('Tasks.logPerformance', true);
}
public function testRunWithNoTasks()
{
$this->assertNull($this->getRunner()->run());
}
public function testRunWithSuccess()
{
$task1 = (new Task('closure', static function () {
echo 'Task 1';
}))->daily('12:05am')->named('task1');
$task2 = (new Task('closure', static function () {
echo 'Task 2';
}))->daily('12:00am')->named('task2');
$runner = $this->getRunner([$task1, $task2]);
ob_start();
$runner->withTestTime('12:00am')->run();
$output = ob_get_clean();
// Only task 2 should have run
$this->assertSame('Task 2', $output);
// Should have logged the stats
$expected = [
[
'task' => 'task2',
'type' => 'closure',
'start' => date('Y-m-d H:i:s'),
'duration' => '0.00',
'output' => null,
'error' => null,
],
];
$this->seeInDatabase('settings', [
'class' => 'CodeIgniter\Tasks\Config\Tasks',
'key' => 'log-task2',
'value' => serialize($expected),
]);
$this->dontSeeInDatabase('settings', [
'key' => 'log-task1',
]);
}
/**
* @throws ReflectionException
*/
public function testRunWithError()
{
$task1 = (new Task('closure', static function () {
echo 'Task 1';
}))->daily('12:05am')->named('task1');
$task3 = (new Task('closure', static function () {
throw new Exception('Example exception in Task 3');
}))->daily('12:00am')->named('task3');
$runner = $this->getRunner([$task1, $task3]);
ob_start();
$runner->withTestTime('12:00am')->run();
$output = ob_get_clean();
// Only task 3 should have run
$this->assertSame('', $output);
// Get info about the exception
$reflection = new ReflectionFunction($task3->getAction());
$file = $reflection->getFileName();
$line = $reflection->getStartLine() + 1;
// Should have logged the stats
$expected = [
[
'task' => 'task3',
'type' => 'closure',
'start' => date('Y-m-d H:i:s'),
'duration' => '0.00',
'output' => null,
'error' => "Exception: 0 - Example exception in Task 3\nfile: {$file}:{$line}",
],
];
$this->seeInDatabase('settings', [
'class' => 'CodeIgniter\Tasks\Config\Tasks',
'key' => 'log-task3',
'value' => serialize($expected),
]);
$this->dontSeeInDatabase('settings', [
'key' => 'log-task1',
]);
}
protected function getRunner(array $tasks = [])
{
$scheduler = service('scheduler');
$this->setPrivateProperty($scheduler, 'tasks', $tasks);
Services::injectMock('scheduler', $scheduler);
return new TaskRunner();
}
}