forked from codeigniter4/tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskTest.php
More file actions
162 lines (123 loc) · 4.1 KB
/
TaskTest.php
File metadata and controls
162 lines (123 loc) · 4.1 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
<?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\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'));
}
}