Passed
Push — master ( 91accf...e7194d )
by Siad
06:49
created

DateSelectorTest::testDateTimeWithWhenAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.9332
c 1
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
21
/**
22
 * Class SelectorUtilsTest
23
 *
24
 * Test cases for SelectorUtils
25
 */
26
class DateSelectorTest extends BuildFileTest
27
{
28
    const TWENTY_FOUR_HOURS_IN_SECONDS = (24 * 60 * 60);
29
30
    private $inputDir;
31
    private $outputDir;
32
33
    private static $fileStateMsgs = array();
34
35
36
    /**
37
     * {@inheritDoc}
38
     * @see \PHPUnit\Framework\TestCase::setUpBeforeClass()
39
     */
40
    public static function setUpBeforeClass(): void
41
    {
42
        self::$fileStateMsgs['Before'] = array();
43
        self::$fileStateMsgs['Now'] = array();
44
        self::$fileStateMsgs['After'] = array();
45
46
        self::$fileStateMsgs['Before'][true] = 'Before file should not exist in the output directory';
47
        self::$fileStateMsgs['Before'][false] = 'Before file should exist in the output directory';
48
49
        self::$fileStateMsgs['Now'][true] = 'Now file should not exist in the output directory';
50
        self::$fileStateMsgs['Now'][false] = 'Now file should exist in the output directory';
51
52
        self::$fileStateMsgs['After'][true] = 'After file should not exist in the output directory';
53
        self::$fileStateMsgs['After'][false] = 'After file should exist in the output directory';
54
    }
55
56
    private static function getFileStateMsg(string $file, bool $isNot): string
57
    {
58
        return self::$fileStateMsgs[$file][$isNot];
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     * @see \PHPUnit\Framework\TestCase::tearDownAfterClass()
64
     */
65
    public static function tearDownAfterClass(): void
66
    {
67
        self::$fileStateMsgs = array();
68
    }
69
70
    public function setUp(): void
71
    {
72
        $this->configureProject(
73
            PHING_TEST_BASE . '/etc/types/selectors/DateSelectorTest.xml'
74
        );
75
        $this->executeTarget('setup');
76
77
        $project = $this->getProject();
78
        $this->inputDir = $project->getProperty('input.dir');
79
        $this->outputDir = rtrim($project->getProperty('output.dir'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
80
    }
81
82
    public function tearDown(): void
83
    {
84
        $this->executeTarget('clean');
85
    }
86
87
    /**
88
     * Creates a test file in the specified directory with the specified
89
     * creation time.
90
     *
91
     * @param string $dir The directory where the test file should be created.
92
     * @param int $timestamp The timestamp to touch the file with (in seconds
93
     *              since the epoch).
94
     *
95
     * @return string The fully qualified name of the test file.
96
     */
97
    private function createTestFile(string $dir, int $timestamp): string
98
    {
99
        $file = tempnam($dir, 'dst');
100
        $writeCnt = file_put_contents($file, 'DateSelectorTest file');
101
        if (false !== $writeCnt) {
102
            touch($file, $timestamp);
103
        } else {
104
            $this->fail('Unable to create test file: ' . $file);
105
        }
106
107
        return $file;
108
    }
109
110
    /*
111
     * Test using seconds attribute
112
     *
113
     * when defaults to equal
114
     * granularity defaults to 0
115
     */
116
    public function testSecondsWithDefaults()
117
    {
118
        $epochSeconds = time();
119
120
        $this->getProject()->setProperty('epoch.seconds', $epochSeconds);
121
122
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
123
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
124
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
125
126
        $this->executeTarget(__FUNCTION__);
127
128
        $this->assertFileDoesNotExist($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', true));
129
        $this->assertFileDoesNotExist($this->outputDir . basename($afterFile), self::getFileStateMsg('After', true));
130
        $this->assertFileExists($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', false));
131
    }
132
133
    /*
134
     * Test using seconds attribute with when set to after
135
     *
136
     * granularity defaults to 0
137
     */
138
    public function testSecondsWithWhenAfter()
139
    {
140
        $epochSeconds = time();
141
142
        $this->getProject()->setProperty('epoch.seconds', $epochSeconds);
143
144
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
145
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
146
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
147
148
        $this->executeTarget(__FUNCTION__);
149
150
        $this->assertFileDoesNotExist($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', true));
151
        $this->assertFileDoesNotExist($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', true));
152
        $this->assertFileExists($this->outputDir . basename($afterFile), self::getFileStateMsg('After', false));
153
    }
154
155
    /*
156
     * Test using seconds attribute with when set to before
157
     *
158
     * granularity defaults to 0
159
     */
160
    public function testSecondsWithWhenBefore()
161
    {
162
        $epochSeconds = time();
163
164
        $this->getProject()->setProperty('epoch.seconds', $epochSeconds);
165
166
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
167
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
168
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
169
170
        $this->executeTarget(__FUNCTION__);
171
172
        $this->assertFileDoesNotExist($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', true));
173
        $this->assertFileDoesNotExist($this->outputDir . basename($afterFile), self::getFileStateMsg('After', true));
174
        $this->assertFileExists($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', false));
175
    }
176
177
    /*
178
     * Test using seconds attribute with granularity at 60
179
     *
180
     * when defaults to equal
181
     */
182
    public function testSecondsGranularitySixtySeconds()
183
    {
184
        $epochSeconds = time();
185
186
        $this->getProject()->setProperty('epoch.seconds', $epochSeconds);
187
188
        $inWindowFiles = array(
189
            '-60s' => $this->createTestFile($this->inputDir, $epochSeconds - 60),
190
            '-59s' => $this->createTestFile($this->inputDir, $epochSeconds - 59),
191
            '-30s' => $this->createTestFile($this->inputDir, $epochSeconds - 30),
192
            '-+0s' => $this->createTestFile($this->inputDir, $epochSeconds),
193
            '+30s' => $this->createTestFile($this->inputDir, $epochSeconds + 30),
194
            '+59s' => $this->createTestFile($this->inputDir, $epochSeconds + 59),
195
            '+60s' => $this->createTestFile($this->inputDir, $epochSeconds + 60)
196
        );
197
198
        $outOfWindowFiles = array(
199
            '-24h' => $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS),
200
            '-61s' => $this->createTestFile($this->inputDir, $epochSeconds - 61),
201
            '+61s' => $this->createTestFile($this->inputDir, $epochSeconds + 61),
202
            '+24h' => $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS)
203
        );
204
205
        $this->executeTarget(__FUNCTION__);
206
207
        foreach ($inWindowFiles as $offset => $file) {
208
            $this->assertFileExists($this->outputDir . basename($file), $offset . ' file missing from output directory');
209
        }
210
211
        foreach ($outOfWindowFiles as $file) {
212
            $this->assertFileDoesNotExist($this->outputDir . basename($file), $offset . ' file unexpected in output directory');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $offset seems to be defined by a foreach iteration on line 207. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
213
        }
214
    }
215
216
    /*
217
     * Test using datetime attribute
218
     *
219
     * when defaults to equal
220
     * granularity defaults to 0
221
     */
222
    public function testDateTimeWithDefaults()
223
    {
224
        $dateTime = '01/01/2001 12:00 AM';
225
226
        $epochSeconds = strtotime($dateTime);
227
228
        $this->getProject()->setProperty('datetime', $dateTime);
229
230
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
231
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
232
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
233
234
        $this->executeTarget(__FUNCTION__);
235
236
        $this->assertFileDoesNotExist($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', true));
237
        $this->assertFileDoesNotExist($this->outputDir . basename($afterFile), self::getFileStateMsg('After', true));
238
        $this->assertFileExists($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', false));
239
    }
240
241
    /*
242
     * Test using datetime attribute with when set to after
243
     *
244
     * granularity defaults to 0
245
     */
246
    public function testDateTimeWithWhenAfter()
247
    {
248
        $dateTime = '01/01/1999 12:00 AM';
249
250
        $epochSeconds = strtotime($dateTime);
251
252
        $this->getProject()->setProperty('datetime', '01/01/1999 12:00 AM');
253
254
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
255
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
256
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
257
258
        $this->executeTarget(__FUNCTION__);
259
260
        $this->assertFileDoesNotExist($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', true));
261
        $this->assertFileDoesNotExist($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', true));
262
        $this->assertFileExists($this->outputDir . basename($afterFile), self::getFileStateMsg('After', false));
263
    }
264
265
    /*
266
     * Test using datetime attribute with when set to before
267
     *
268
     * granularity defaults to 0
269
     */
270
    public function testDateTimeWithWhenBefore()
271
    {
272
        $dateTime = '01/01/1975 12:00 PM';
273
274
        $epochSeconds = strtotime($dateTime);
275
276
        $this->getProject()->setProperty('datetime', $dateTime);
277
278
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
279
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
280
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
281
282
        $this->executeTarget(__FUNCTION__);
283
284
        $this->assertFileDoesNotExist($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', true));
285
        $this->assertFileDoesNotExist($this->outputDir . basename($afterFile), self::getFileStateMsg('After', true));
286
        $this->assertFileExists($this->outputDir . basename($beforeFile), self::getFileStateMsg('Now', false));
287
    }
288
289
    /*
290
     * Test using datetime attribute with granularity at 60
291
     *
292
     * when defaults to equal
293
     */
294
    public function testDateTimeGranularityThirtySeconds()
295
    {
296
        $dateTime = '01/01/1971 05:12 AM';
297
298
        $epochSeconds = strtotime($dateTime);
299
300
        $this->getProject()->setProperty('datetime', $dateTime);
301
302
        $inWindowFiles = array(
303
            '-30s' => $this->createTestFile($this->inputDir, $epochSeconds - 30),
304
            '-29s' => $this->createTestFile($this->inputDir, $epochSeconds - 29),
305
            '-15s' => $this->createTestFile($this->inputDir, $epochSeconds - 15),
306
            '-+0s' => $this->createTestFile($this->inputDir, $epochSeconds),
307
            '+15s' => $this->createTestFile($this->inputDir, $epochSeconds + 15),
308
            '+29s' => $this->createTestFile($this->inputDir, $epochSeconds + 29),
309
            '+30s' => $this->createTestFile($this->inputDir, $epochSeconds + 30)
310
        );
311
312
        $outOfWindowFiles = array(
313
            '-24h' => $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS),
314
            '-31s' => $this->createTestFile($this->inputDir, $epochSeconds - 31),
315
            '+31s' => $this->createTestFile($this->inputDir, $epochSeconds + 31),
316
            '+24h' => $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS)
317
        );
318
319
        $this->executeTarget(__FUNCTION__);
320
321
        foreach ($inWindowFiles as $offset => $file) {
322
            $this->assertFileExists($this->outputDir . basename($file), $offset . ' file missing from output directory');
323
        }
324
325
        foreach ($outOfWindowFiles as $offset => $file) {
326
            $this->assertFileDoesNotExist($this->outputDir . basename($file), $offset . ' file unexpected in output directory');
327
        }
328
    }
329
330
    /*
331
     * Test using millis attribute
332
     *
333
     * when defaults to equal
334
     * granularity defaults to 0
335
     */
336
    public function testMillisWithDefaults()
337
    {
338
        $epochSeconds = time();
339
        $epochMillis = ($epochSeconds * 1000) + rand(0, 999);
340
341
        $this->getProject()->setProperty('epoch.millis', $epochMillis);
342
343
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - 60 * 1000);
344
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
345
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + 60 * 1000);
346
347
        $this->executeTarget(__FUNCTION__);
348
349
        $this->assertFileDoesNotExist($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', true));
350
        $this->assertFileDoesNotExist($this->outputDir . basename($afterFile), self::getFileStateMsg('After', true));
351
        $this->assertFileExists($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', false));
352
    }
353
354
355
    /*
356
     * Test using millis attribute with when set to after
357
     *
358
     * granularity defaults to 0
359
     */
360
    public function testMillisWithWhenAfter()
361
    {
362
        $epochSeconds = time();
363
        $epochMillis = ($epochSeconds * 1000) + rand(0, 999);
364
365
        $this->getProject()->setProperty('epoch.millis', $epochMillis);
366
367
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
368
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
369
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
370
371
        $this->executeTarget(__FUNCTION__);
372
373
        $this->assertFileDoesNotExist($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', true));
374
        $this->assertFileDoesNotExist($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', true));
375
        $this->assertFileExists($this->outputDir . basename($afterFile), self::getFileStateMsg('After', false));
376
    }
377
378
    /*
379
     * Test using millis attribute with when set to before
380
     *
381
     * granularity defaults to 0
382
     */
383
    public function testMillisWithWhenBefore()
384
    {
385
        $epochSeconds = time();
386
        $epochMillis = ($epochSeconds * 1000) + rand(0, 999);
387
388
        $this->getProject()->setProperty('epoch.millis', $epochMillis);
389
390
        $beforeFile = $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS);
391
        $nowFile = $this->createTestFile($this->inputDir, $epochSeconds);
392
        $afterFile = $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS);
393
394
        $this->executeTarget(__FUNCTION__);
395
396
        $this->assertFileDoesNotExist($this->outputDir . basename($nowFile), self::getFileStateMsg('Now', true));
397
        $this->assertFileDoesNotExist($this->outputDir . basename($afterFile), self::getFileStateMsg('After', true));
398
        $this->assertFileExists($this->outputDir . basename($beforeFile), self::getFileStateMsg('Before', false));
399
    }
400
401
    /*
402
     * Test using millis attribute with granularity at 60
403
     *
404
     * when defaults to equal
405
     */
406
    public function testMillisGranularitySixSeconds()
407
    {
408
        $epochSeconds = time();
409
        $epochMillis = ($epochSeconds * 1000) + rand(0, 999);
410
411
        $this->getProject()->setProperty('epoch.millis', $epochMillis);
412
413
        $inWindowFiles = array(
414
            '-6s' => $this->createTestFile($this->inputDir, $epochSeconds - 6),
415
            '-5s' => $this->createTestFile($this->inputDir, $epochSeconds - 5),
416
            '-3s' => $this->createTestFile($this->inputDir, $epochSeconds - 3),
417
            '+-0s' => $this->createTestFile($this->inputDir, $epochSeconds),
418
            '+3s' => $this->createTestFile($this->inputDir, $epochSeconds + 3),
419
            '+5s' => $this->createTestFile($this->inputDir, $epochSeconds + 5),
420
            '+6s' => $this->createTestFile($this->inputDir, $epochSeconds + 6)
421
        );
422
423
        $outOfWindowFiles = array(
424
            '-24h' => $this->createTestFile($this->inputDir, $epochSeconds - self::TWENTY_FOUR_HOURS_IN_SECONDS),
425
            '-7s' => $this->createTestFile($this->inputDir, $epochSeconds - 7),
426
            '+7s' => $this->createTestFile($this->inputDir, $epochSeconds + 7),
427
            '+24h' => $this->createTestFile($this->inputDir, $epochSeconds + self::TWENTY_FOUR_HOURS_IN_SECONDS)
428
        );
429
430
        $this->executeTarget(__FUNCTION__);
431
432
        foreach ($inWindowFiles as $offset => $file) {
433
            $this->assertFileExists($this->outputDir . basename($file), $offset . ' file missing from the output directory');
434
        }
435
436
        foreach ($outOfWindowFiles as $offset => $file) {
437
            $this->assertFileDoesNotExist($this->outputDir . basename($file), $offset . ' file unexpected in the output directory');
438
        }
439
    }
440
441
    /*
442
     * Test using seconds attribute
443
     *
444
     * when defaults to equal
445
     * granularity defaults to 0
446
     */
447
    public function testSecondsInvalidSeconds()
448
    {
449
        $this->expectBuildException(__FUNCTION__, 'seconds has invalid value');
450
    }
451
452
    /*
453
     * Test using datetime attribute with and invalid value
454
     */
455
    public function testDateTimeInvalidDateTime()
456
    {
457
        $this->expectBuildException(__FUNCTION__, 'datetime has invalid value');
458
    }
459
460
    /*
461
     * Test using invalid datetime attribute
462
     */
463
    public function testDateTimeNotDateTime()
464
    {
465
        $this->expectBuildException(__FUNCTION__, 'datetime has invalid value');
466
    }
467
468
    /*
469
     * Test using invalid millis attribute
470
     */
471
    public function testMillisInvalidMillis()
472
    {
473
        $this->expectBuildException(__FUNCTION__, 'millis has invalid value');
474
    }
475
476
    /*
477
     * Test using an invalid when value
478
     */
479
    public function testInvalidWhen()
480
    {
481
        $this->expectBuildException(__FUNCTION__, 'when attribute has invalid value');
482
    }
483
484
    /*
485
     * Test using an invalid when value
486
     */
487
    public function testInvalidAttribute()
488
    {
489
        $this->expectBuildException(__FUNCTION__, 'invalid attribute for task');
490
    }
491
}
492