1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Basebuilder\Scheduling\Event; |
4
|
|
|
|
5
|
|
|
class EventTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @test |
9
|
|
|
*/ |
10
|
|
|
function it_can_compile_the_command() |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$event = new Event('php -i'); |
13
|
|
|
|
14
|
|
|
$this->assertSame("sh -c 'php -i 1>> '/dev/null' 2>> '/dev/null''", $event->compileCommand()); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
*/ |
20
|
|
|
function it_can_switch_user() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$event = new Event('php -i'); |
23
|
|
|
$event->asUser('foo'); |
24
|
|
|
|
25
|
|
|
$this->assertSame("sudo -u foo -- sh -c 'php -i 1>> '/dev/null' 2>> '/dev/null''", $event->compileCommand()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
|
function it_can_switch_directory() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$event = new Event('php -i'); |
34
|
|
|
$event->in('/foo/bar'); |
35
|
|
|
|
36
|
|
|
$this->assertSame("cd /foo/bar; sh -c 'php -i 1>> '/dev/null' 2>> '/dev/null''", $event->compileCommand()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @test |
41
|
|
|
*/ |
42
|
|
|
function it_can_append_output() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$event = new Event('php -i'); |
45
|
|
|
$event->appendOutput(true); |
46
|
|
|
|
47
|
|
|
$this->assertSame("sh -c 'php -i 1>> '/dev/null' 2>> '/dev/null''", $event->compileCommand()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
*/ |
53
|
|
|
function it_can_overwrite_output() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$event = new Event('php -i'); |
56
|
|
|
$event->appendOutput(false); |
57
|
|
|
|
58
|
|
|
$this->assertSame("sh -c 'php -i 1> '/dev/null' 2> '/dev/null''", $event->compileCommand()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test |
63
|
|
|
*/ |
64
|
|
|
function it_can_run_every_minute() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$event = new Event('php -i'); |
67
|
|
|
$event->everyMinute(); |
68
|
|
|
|
69
|
|
|
$this->assertSame('* * * * * *', (string) $event->getCronExpression()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
*/ |
75
|
|
|
function it_can_run_n_minutes() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$event = new Event('php -i'); |
78
|
|
|
$event->everyNMinutes(5); |
79
|
|
|
|
80
|
|
|
$this->assertSame("*/5 * * * * *", (string) $event->getCronExpression()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
|
|
function it_can_run_hourly() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$event = new Event('php -i'); |
89
|
|
|
$event->hourly(); |
90
|
|
|
|
91
|
|
|
$this->assertSame('0 * * * * *', (string) $event->getCronExpression()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @test |
96
|
|
|
*/ |
97
|
|
|
function it_can_run_on_every_hour() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$event = new Event('php -i'); |
100
|
|
|
$event->hour(1); |
101
|
|
|
|
102
|
|
|
$this->assertSame('* 1 * * * *', (string) $event->getCronExpression()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @test |
107
|
|
|
*/ |
108
|
|
|
function it_can_run_daily() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$event = new Event('php -i'); |
111
|
|
|
$event->daily(); |
112
|
|
|
|
113
|
|
|
$this->assertSame('0 0 * * * *', (string) $event->getCronExpression()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @test |
118
|
|
|
*/ |
119
|
|
|
function it_can_run_daily_at_a_specific_time() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$event = new Event('php -i'); |
122
|
|
|
$event->dailyAt('10:05'); |
123
|
|
|
$this->assertSame('5 10 * * * *', (string) $event->getCronExpression()); |
124
|
|
|
|
125
|
|
|
$event->dailyAt('10'); |
126
|
|
|
$this->assertSame('0 10 * * * *', (string) $event->getCronExpression()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @test |
131
|
|
|
*/ |
132
|
|
|
function it_can_run_on_specific_days() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$event = new Event('php -i'); |
135
|
|
|
|
136
|
|
|
$event->days([1, 2 ,3 ]); |
137
|
|
|
$this->assertSame('* * * * 1,2,3 *', (string) $event->getCronExpression()); |
138
|
|
|
|
139
|
|
|
$event->days(4, 5, 6); |
140
|
|
|
$this->assertSame('* * * * 4,5,6 *', (string) $event->getCronExpression()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
*/ |
146
|
|
|
function it_can_run_on_weekdays_only() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$event = new Event('php -i'); |
149
|
|
|
$event->weekdays(); |
150
|
|
|
|
151
|
|
|
$this->assertSame('* * * * 1-5 *', (string) $event->getCronExpression()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
*/ |
157
|
|
|
function it_can_run_weekly() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$event = new Event('php -i'); |
160
|
|
|
$event->weekly(); |
161
|
|
|
|
162
|
|
|
$this->assertSame('0 0 * * 0 *', (string) $event->getCronExpression()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @test |
167
|
|
|
*/ |
168
|
|
|
function it_can_run_monthly() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$event = new Event('php -i'); |
171
|
|
|
$event->monthly(); |
172
|
|
|
|
173
|
|
|
$this->assertSame('0 0 1 * * *', (string) $event->getCronExpression()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @test |
178
|
|
|
*/ |
179
|
|
|
function it_can_run_quarterly() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$event = new Event('php -i'); |
182
|
|
|
$event->quarterly(); |
183
|
|
|
|
184
|
|
|
$this->assertSame('0 0 1 */3 * *', (string) $event->getCronExpression()); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @test |
189
|
|
|
*/ |
190
|
|
|
function it_can_run_yearly() |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
$event = new Event('php -i'); |
193
|
|
|
$event->yearly(); |
194
|
|
|
|
195
|
|
|
$this->assertSame('0 0 1 1 * *', (string) $event->getCronExpression()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @test |
200
|
|
|
*/ |
201
|
|
|
function it_allows_for_filtering() |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$event = new Event('php -i'); |
204
|
|
|
$event->everyMinute(); |
205
|
|
|
|
206
|
|
|
$this->assertTrue($event->isDue()); |
207
|
|
|
|
208
|
|
|
$event->skip(function () { |
209
|
|
|
return true; |
210
|
|
|
}); |
211
|
|
|
|
212
|
|
|
$this->assertFalse($event->isDue()); |
213
|
|
|
|
214
|
|
|
$event = new Event('php -i'); |
215
|
|
|
$event->everyMinute(); |
216
|
|
|
|
217
|
|
|
$this->assertTrue($event->isDue()); |
218
|
|
|
|
219
|
|
|
$event->when(function () { |
220
|
|
|
return false; |
221
|
|
|
}); |
222
|
|
|
|
223
|
|
|
$this->assertFalse($event->isDue()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @group fix |
228
|
|
|
*/ |
229
|
|
|
function it_can_prevent_overlapping() |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
$event = new Event('php -i'); |
232
|
|
|
$event->preventOverlapping(); |
233
|
|
|
|
234
|
|
|
preg_match('/\(touch \w*; php -i; rm \w*\); 1>> /dev/null 2>> /dev/null/', $event->compileCommand()); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.