1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\AntiSpamBundle\Tests\Provider; |
11
|
|
|
|
12
|
|
|
use Core23\AntiSpamBundle\Provider\SessionTimeProvider; |
13
|
|
|
use DateTime; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Argument; |
16
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
17
|
|
|
|
18
|
|
|
final class SessionTimeProviderTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testCreateFromString(): void |
21
|
|
|
{ |
22
|
|
|
$session = $this->prophesize(Session::class); |
23
|
|
|
$session->set('antispam_foobar', Argument::type(DateTime::class)) |
24
|
|
|
->shouldBeCalled() |
25
|
|
|
; |
26
|
|
|
|
27
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
28
|
|
|
$provider->createFormProtection('foobar'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testIsValid(): void |
32
|
|
|
{ |
33
|
|
|
$session = $this->prepareValidSessionKey(); |
34
|
|
|
|
35
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
36
|
|
|
|
37
|
|
|
static::assertTrue($provider->isValid('foobar', [])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testIsValidWithMinTime(): void |
41
|
|
|
{ |
42
|
|
|
$session = $this->prepareValidSessionKey(); |
43
|
|
|
|
44
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
45
|
|
|
|
46
|
|
|
static::assertTrue($provider->isValid('foobar', [ |
47
|
|
|
'min' => 10, |
48
|
|
|
])); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testIsValidWithMaxTime(): void |
52
|
|
|
{ |
53
|
|
|
$session = $this->prepareValidSessionKey(); |
54
|
|
|
|
55
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
56
|
|
|
|
57
|
|
|
static::assertTrue($provider->isValid('foobar', [ |
58
|
|
|
'max' => 60, |
59
|
|
|
])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testIsInvalid(): void |
63
|
|
|
{ |
64
|
|
|
$session = $this->prophesize(Session::class); |
65
|
|
|
$session->has('antispam_foobar') |
66
|
|
|
->willReturn(false) |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
70
|
|
|
|
71
|
|
|
static::assertFalse($provider->isValid('foobar', [])); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testIsInvalidBecauseOfMinTime(): void |
75
|
|
|
{ |
76
|
|
|
$session = $this->prepareValidSessionKey(); |
77
|
|
|
|
78
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
79
|
|
|
static::assertFalse($provider->isValid('foobar', [ |
80
|
|
|
'min' => 60, |
81
|
|
|
])); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testIsInvalidBecauseOfMaxTime(): void |
85
|
|
|
{ |
86
|
|
|
$session = $this->prepareValidSessionKey(); |
87
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
88
|
|
|
|
89
|
|
|
static::assertFalse($provider->isValid('foobar', [ |
90
|
|
|
'max' => 10, |
91
|
|
|
])); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testRemoveFormProtection(): void |
95
|
|
|
{ |
96
|
|
|
$session = $this->prophesize(Session::class); |
97
|
|
|
$session->remove('antispam_foobar') |
98
|
|
|
->shouldBeCalled() |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
102
|
|
|
$provider->removeFormProtection('foobar'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function prepareValidSessionKey() |
106
|
|
|
{ |
107
|
|
|
$session = $this->prophesize(Session::class); |
108
|
|
|
$session->has('antispam_foobar') |
109
|
|
|
->willReturn(true) |
110
|
|
|
; |
111
|
|
|
$session->get('antispam_foobar') |
112
|
|
|
->willReturn(new DateTime('- 15 seconds')) |
113
|
|
|
; |
114
|
|
|
|
115
|
|
|
return $session; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|