|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Enjoys\Cookie; |
|
4
|
|
|
|
|
5
|
|
|
use Enjoys\Cookie\NotCorrectTtlString; |
|
6
|
|
|
use Enjoys\Cookie\Expires; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ExpiresTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
private $currentTimestamp = 1; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @dataProvider expiresData |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testGetExpires($ttl, $expect) |
|
18
|
|
|
{ |
|
19
|
|
|
$expires = new Expires($ttl, $this->currentTimestamp); |
|
20
|
|
|
$this->assertSame($expect, $expires->getExpires()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function expiresData(): array |
|
24
|
|
|
{ |
|
25
|
|
|
return [ |
|
26
|
|
|
[0, 0], |
|
27
|
|
|
[false, -1], |
|
28
|
|
|
['session', 0], |
|
29
|
|
|
['Session', 0], |
|
30
|
|
|
['sessioN', 0], |
|
31
|
|
|
['SeSsiOn', 0], |
|
32
|
|
|
[true, 0], |
|
33
|
|
|
[10, $this->currentTimestamp + 10], |
|
34
|
|
|
[-1, $this->currentTimestamp + -1], |
|
35
|
|
|
['10', $this->currentTimestamp + 10], |
|
36
|
|
|
['+1 year', $this->currentTimestamp + (60 * 60 * 24 * 365)], |
|
37
|
|
|
['-1 year', $this->currentTimestamp + (-60 * 60 * 24 * 365)], |
|
38
|
|
|
['+1 month', $this->currentTimestamp + (60 * 60 * 24 * 31)], |
|
39
|
|
|
['-1 month', $this->currentTimestamp + (-60 * 60 * 24 * 31)], |
|
40
|
|
|
['1 day', $this->currentTimestamp + (60 * 60 * 24)], |
|
41
|
|
|
['-1 day', $this->currentTimestamp + (-60 * 60 * 24)], |
|
42
|
|
|
[new \DateTime('1970-01-02'), 3600 * 24], |
|
43
|
|
|
[new \DateTimeImmutable('1970-01-05'), 4 * 3600 * 24], |
|
44
|
|
|
[new \DateTimeImmutable('@404'), 404], |
|
45
|
|
|
[(new \DateTimeImmutable('@0'))->modify('1 hour'), 3600], |
|
46
|
|
|
[(new \DateTime('@0'))->modify('1 minute'), 60], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testException() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->expectException(NotCorrectTtlString::class); |
|
53
|
|
|
new Expires('invalid string'); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|