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 Prophecy\Prophecy\ObjectProphecy; |
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
18
|
|
|
|
19
|
|
|
final class SessionTimeProviderTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testCreateFromString(): void |
22
|
|
|
{ |
23
|
|
|
$session = $this->prophesize(Session::class); |
24
|
|
|
$session->set('antispam_foobar', Argument::type(DateTime::class)) |
25
|
|
|
->shouldBeCalled() |
26
|
|
|
; |
27
|
|
|
|
28
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
29
|
|
|
$provider->createFormProtection('foobar'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testIsValid(): void |
33
|
|
|
{ |
34
|
|
|
$session = $this->prepareValidSessionKey(); |
35
|
|
|
|
36
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
|
|
|
|
37
|
|
|
|
38
|
|
|
static::assertTrue($provider->isValid('foobar', [])); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testIsValidWithMinTime(): void |
42
|
|
|
{ |
43
|
|
|
$session = $this->prepareValidSessionKey(); |
44
|
|
|
|
45
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
|
|
|
|
46
|
|
|
|
47
|
|
|
static::assertTrue($provider->isValid('foobar', [ |
48
|
|
|
'min' => 10, |
49
|
|
|
])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testIsValidWithMaxTime(): void |
53
|
|
|
{ |
54
|
|
|
$session = $this->prepareValidSessionKey(); |
55
|
|
|
|
56
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
|
|
|
|
57
|
|
|
|
58
|
|
|
static::assertTrue($provider->isValid('foobar', [ |
59
|
|
|
'max' => 60, |
60
|
|
|
])); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testIsInvalid(): void |
64
|
|
|
{ |
65
|
|
|
$session = $this->prophesize(Session::class); |
66
|
|
|
$session->has('antispam_foobar') |
67
|
|
|
->willReturn(false) |
68
|
|
|
; |
69
|
|
|
|
70
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
71
|
|
|
|
72
|
|
|
static::assertFalse($provider->isValid('foobar', [])); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testIsInvalidBecauseOfMinTime(): void |
76
|
|
|
{ |
77
|
|
|
$session = $this->prepareValidSessionKey(); |
78
|
|
|
|
79
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
|
|
|
|
80
|
|
|
static::assertFalse($provider->isValid('foobar', [ |
81
|
|
|
'min' => 60, |
82
|
|
|
])); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testIsInvalidBecauseOfMaxTime(): void |
86
|
|
|
{ |
87
|
|
|
$session = $this->prepareValidSessionKey(); |
88
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
|
|
|
|
89
|
|
|
|
90
|
|
|
static::assertFalse($provider->isValid('foobar', [ |
91
|
|
|
'max' => 10, |
92
|
|
|
])); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testRemoveFormProtection(): void |
96
|
|
|
{ |
97
|
|
|
$session = $this->prophesize(Session::class); |
98
|
|
|
$session->remove('antispam_foobar') |
99
|
|
|
->shouldBeCalled() |
100
|
|
|
; |
101
|
|
|
|
102
|
|
|
$provider = new SessionTimeProvider($session->reveal()); |
103
|
|
|
$provider->removeFormProtection('foobar'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return ObjectProphecy|Session |
108
|
|
|
*/ |
109
|
|
|
private function prepareValidSessionKey() |
110
|
|
|
{ |
111
|
|
|
$session = $this->prophesize(Session::class); |
112
|
|
|
$session->has('antispam_foobar') |
113
|
|
|
->willReturn(true) |
114
|
|
|
; |
115
|
|
|
$session->get('antispam_foobar') |
116
|
|
|
->willReturn(new DateTime('- 15 seconds')) |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
return $session; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.