|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Phive Queue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Phive\Queue\Tests\Queue; |
|
13
|
|
|
|
|
14
|
|
|
use Phive\Queue\QueueUtils; |
|
15
|
|
|
|
|
16
|
|
|
class QueueUtilsTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider provideValidEtas |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testNormalizeEta($eta, $timestamp) |
|
22
|
|
|
{ |
|
23
|
|
|
if (is_callable($timestamp)) { |
|
24
|
|
|
$timestamp = $timestamp(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$this->assertEquals($timestamp, QueueUtils::normalizeEta($eta)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @dataProvider provideInvalidEtas |
|
32
|
|
|
* @expectedException InvalidArgumentException |
|
33
|
|
|
* @expectedExceptionMessage The eta parameter is not valid. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testNormalizeEtaThrowsException($eta) |
|
36
|
|
|
{ |
|
37
|
|
|
QueueUtils::normalizeEta($eta); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @dataProvider provideValidEtas |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testCalcDelay($eta, $_, $delay) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->assertEquals($delay, QueueUtils::calculateDelay($eta)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function provideValidEtas() |
|
49
|
|
|
{ |
|
50
|
|
|
$date = new \DateTime(); |
|
51
|
|
|
$now = $date->getTimestamp(); |
|
52
|
|
|
|
|
53
|
|
|
return [ |
|
54
|
|
|
[0, 0, 0], |
|
55
|
|
|
[-1, -1, 0], |
|
56
|
|
|
[null, function () { return time(); }, 0], |
|
57
|
|
|
[$now, $now, 0], |
|
58
|
|
|
['@'.$now, $now, 0], |
|
59
|
|
|
[$date->format(\DateTime::ISO8601), $now, 0], |
|
60
|
|
|
['+1 hour', function () { return time() + 3600; }, 3600], |
|
61
|
|
|
[$date, $now, 0], |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function provideInvalidEtas() |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
[new \stdClass()], |
|
69
|
|
|
['invalid eta string'], |
|
70
|
|
|
[[]], |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|