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\Form\EventListener; |
11
|
|
|
|
12
|
|
|
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamTimeListener; |
13
|
|
|
use Core23\AntiSpamBundle\Provider\TimeProviderInterface; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Argument; |
16
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
17
|
|
|
use Symfony\Component\Form\FormConfigInterface; |
18
|
|
|
use Symfony\Component\Form\FormError; |
19
|
|
|
use Symfony\Component\Form\FormEvent; |
20
|
|
|
use Symfony\Component\Form\FormEvents; |
21
|
|
|
use Symfony\Component\Form\FormInterface; |
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
23
|
|
|
|
24
|
|
|
final class AntiSpamTimeListenerTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
private $timeProvider; |
27
|
|
|
|
28
|
|
|
private $translator; |
29
|
|
|
|
30
|
|
|
protected function setUp(): void |
31
|
|
|
{ |
32
|
|
|
$this->timeProvider = $this->prophesize(TimeProviderInterface::class); |
33
|
|
|
$this->translator = $this->prophesize(TranslatorInterface::class); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetSubscribedEvents(): void |
37
|
|
|
{ |
38
|
|
|
static::assertSame([ |
39
|
|
|
FormEvents::PRE_SUBMIT => 'preSubmit', |
40
|
|
|
], AntiSpamTimeListener::getSubscribedEvents()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testPreSubmit(): void |
44
|
|
|
{ |
45
|
|
|
$this->timeProvider->isValid('my-form', ['foo' => 'bar']) |
46
|
|
|
->willReturn(true) |
47
|
|
|
; |
48
|
|
|
$this->timeProvider->removeFormProtection('my-form') |
49
|
|
|
->shouldBeCalled() |
50
|
|
|
; |
51
|
|
|
|
52
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
53
|
|
|
$config->getOption('compound') |
54
|
|
|
->willReturn(true) |
55
|
|
|
; |
56
|
|
|
|
57
|
|
|
$form = $this->prepareForm($config, true); |
58
|
|
|
|
59
|
|
|
$event = $this->prophesize(FormEvent::class); |
60
|
|
|
$event->getForm() |
61
|
|
|
->willReturn($form) |
62
|
|
|
; |
63
|
|
|
|
64
|
|
|
$listener = new AntiSpamTimeListener( |
65
|
|
|
$this->timeProvider->reveal(), |
66
|
|
|
$this->translator->reveal(), |
67
|
|
|
['foo' => 'bar'] |
68
|
|
|
); |
69
|
|
|
$listener->preSubmit($event->reveal()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testPreSubmitInvalidForm(): void |
73
|
|
|
{ |
74
|
|
|
$this->translator->trans('time_error', [], 'Core23AntiSpamBundle') |
75
|
|
|
->willReturn('There is an error') |
76
|
|
|
; |
77
|
|
|
|
78
|
|
|
$this->timeProvider->isValid('my-form', ['foo' => 'bar']) |
79
|
|
|
->willReturn(false) |
80
|
|
|
; |
81
|
|
|
$this->timeProvider->removeFormProtection('my-form') |
82
|
|
|
->shouldBeCalled() |
83
|
|
|
; |
84
|
|
|
|
85
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
86
|
|
|
$config->getOption('compound') |
87
|
|
|
->willReturn(true) |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
$form = $this->prepareForm($config, true); |
91
|
|
|
$form->addError(Argument::type(FormError::class)) |
|
|
|
|
92
|
|
|
->shouldBeCalled() |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
$event = $this->prophesize(FormEvent::class); |
96
|
|
|
$event->getForm() |
97
|
|
|
->willReturn($form) |
98
|
|
|
; |
99
|
|
|
|
100
|
|
|
$listener = new AntiSpamTimeListener( |
101
|
|
|
$this->timeProvider->reveal(), |
102
|
|
|
$this->translator->reveal(), |
103
|
|
|
['foo' => 'bar'] |
104
|
|
|
); |
105
|
|
|
$listener->preSubmit($event->reveal()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testPreSubmitChildForm(): void |
109
|
|
|
{ |
110
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
111
|
|
|
$config->getOption('compound') |
112
|
|
|
->willReturn(false) |
113
|
|
|
; |
114
|
|
|
|
115
|
|
|
$form = $this->prepareForm($config); |
116
|
|
|
|
117
|
|
|
$event = $this->prophesize(FormEvent::class); |
118
|
|
|
$event->getForm() |
119
|
|
|
->willReturn($form) |
120
|
|
|
; |
121
|
|
|
|
122
|
|
|
$listener = new AntiSpamTimeListener( |
123
|
|
|
$this->timeProvider->reveal(), |
124
|
|
|
$this->translator->reveal(), |
125
|
|
|
[] |
126
|
|
|
); |
127
|
|
|
$listener->preSubmit($event->reveal()); |
128
|
|
|
|
129
|
|
|
$this->timeProvider->removeFormProtection('my-form') |
130
|
|
|
->shouldNotHaveBeenCalled() |
131
|
|
|
; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testPreSubmitCompoundForm(): void |
135
|
|
|
{ |
136
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
137
|
|
|
$config->getOption('compound') |
138
|
|
|
->willReturn(true) |
139
|
|
|
; |
140
|
|
|
|
141
|
|
|
$form = $this->prepareForm($config); |
142
|
|
|
|
143
|
|
|
$event = $this->prophesize(FormEvent::class); |
144
|
|
|
$event->getForm() |
145
|
|
|
->willReturn($form) |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$listener = new AntiSpamTimeListener( |
149
|
|
|
$this->timeProvider->reveal(), |
150
|
|
|
$this->translator->reveal(), |
151
|
|
|
[] |
152
|
|
|
); |
153
|
|
|
$listener->preSubmit($event->reveal()); |
154
|
|
|
|
155
|
|
|
$this->timeProvider->removeFormProtection('my-form') |
156
|
|
|
->shouldNotHaveBeenCalled() |
157
|
|
|
; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param FormConfigInterface|ObjectProphecy $config |
162
|
|
|
* |
163
|
|
|
* @return FormInterface|ObjectProphecy |
164
|
|
|
*/ |
165
|
|
|
private function prepareForm($config, bool $root = false) |
166
|
|
|
{ |
167
|
|
|
$form = $this->prophesize(FormInterface::class); |
168
|
|
|
$form->isRoot() |
169
|
|
|
->willReturn($root) |
170
|
|
|
; |
171
|
|
|
$form->getConfig() |
172
|
|
|
->willReturn($config) |
173
|
|
|
; |
174
|
|
|
$form->getName() |
175
|
|
|
->willReturn('my-form') |
176
|
|
|
; |
177
|
|
|
|
178
|
|
|
return $form; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: