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\Extension; |
11
|
|
|
|
12
|
|
|
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamTimeListener; |
13
|
|
|
use Core23\AntiSpamBundle\Form\Extension\TimeFormExtension; |
14
|
|
|
use Core23\AntiSpamBundle\Provider\TimeProviderInterface; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Prophecy\Argument; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
18
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
19
|
|
|
use Symfony\Component\Form\FormInterface; |
20
|
|
|
use Symfony\Component\Form\FormView; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
23
|
|
|
|
24
|
|
|
final class TimeFormExtensionTest 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 testBuildForm(): void |
37
|
|
|
{ |
38
|
|
|
$builder = $this->prophesize(FormBuilderInterface::class); |
39
|
|
|
$builder->addEventSubscriber(Argument::type(AntiSpamTimeListener::class)) |
40
|
|
|
->shouldBeCalled() |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$extension = new TimeFormExtension( |
44
|
|
|
$this->timeProvider->reveal(), |
45
|
|
|
$this->translator->reveal(), |
46
|
|
|
[] |
47
|
|
|
); |
48
|
|
|
$extension->buildForm($builder->reveal(), [ |
49
|
|
|
'antispam_time' => true, |
50
|
|
|
'antispam_time_min' => 10, |
51
|
|
|
'antispam_time_max' => 30, |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$builder->addEventSubscriber(Argument::type(AntiSpamTimeListener::class)) |
55
|
|
|
->shouldBeCalled() |
56
|
|
|
; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testBuildFormWithDisabledAntispam(): void |
60
|
|
|
{ |
61
|
|
|
$builder = $this->prophesize(FormBuilderInterface::class); |
62
|
|
|
$builder->addEventSubscriber(Argument::type(AntiSpamTimeListener::class)) |
63
|
|
|
->shouldNotBeCalled() |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$extension = new TimeFormExtension( |
67
|
|
|
$this->timeProvider->reveal(), |
68
|
|
|
$this->translator->reveal(), |
69
|
|
|
[] |
70
|
|
|
); |
71
|
|
|
$extension->buildForm($builder->reveal(), [ |
72
|
|
|
'antispam_time' => false, |
73
|
|
|
'antispam_time_min' => 10, |
74
|
|
|
'antispam_time_max' => 30, |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testFinishView(): void |
79
|
|
|
{ |
80
|
|
|
$view = $this->prophesize(FormView::class); |
81
|
|
|
$form = $this->prophesize(FormInterface::class); |
82
|
|
|
$form->getName() |
83
|
|
|
->willReturn('my_form') |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
$this->timeProvider->createFormProtection('my_form') |
87
|
|
|
->shouldBeCalled() |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
$extension = new TimeFormExtension( |
91
|
|
|
$this->timeProvider->reveal(), |
92
|
|
|
$this->translator->reveal(), |
93
|
|
|
[] |
94
|
|
|
); |
95
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
96
|
|
|
'compound' => true, |
97
|
|
|
'antispam_time' => true, |
98
|
|
|
'antispam_time_min' => 10, |
99
|
|
|
'antispam_time_max' => 30, |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testFinishViewForChildForm(): void |
104
|
|
|
{ |
105
|
|
|
$view = $this->prophesize(FormView::class); |
106
|
|
|
$view->parent = $this->prophesize(FormView::class)->reveal(); |
107
|
|
|
$form = $this->prophesize(FormInterface::class); |
108
|
|
|
$form->getName() |
109
|
|
|
->willReturn('my_form') |
110
|
|
|
; |
111
|
|
|
|
112
|
|
|
$extension = new TimeFormExtension( |
113
|
|
|
$this->timeProvider->reveal(), |
114
|
|
|
$this->translator->reveal(), |
115
|
|
|
[] |
116
|
|
|
); |
117
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
118
|
|
|
'compound' => true, |
119
|
|
|
'antispam_time' => true, |
120
|
|
|
'antispam_time_min' => 10, |
121
|
|
|
'antispam_time_max' => 30, |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
$this->timeProvider->createFormProtection('my_form') |
125
|
|
|
->shouldNotHaveBeenCalled() |
126
|
|
|
; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testFinishViewWithDisbaledAntispam(): void |
130
|
|
|
{ |
131
|
|
|
$view = $this->prophesize(FormView::class); |
132
|
|
|
$form = $this->prophesize(FormInterface::class); |
133
|
|
|
$form->getName() |
134
|
|
|
->willReturn('my_form') |
135
|
|
|
; |
136
|
|
|
|
137
|
|
|
$extension = new TimeFormExtension( |
138
|
|
|
$this->timeProvider->reveal(), |
139
|
|
|
$this->translator->reveal(), |
140
|
|
|
[] |
141
|
|
|
); |
142
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
143
|
|
|
'compound' => true, |
144
|
|
|
'antispam_time' => false, |
145
|
|
|
'antispam_time_min' => 10, |
146
|
|
|
'antispam_time_max' => 30, |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
$this->timeProvider->createFormProtection('my_form') |
150
|
|
|
->shouldNotHaveBeenCalled() |
151
|
|
|
; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testConfigureOptions(): void |
155
|
|
|
{ |
156
|
|
|
$resolver = new OptionsResolver(); |
157
|
|
|
|
158
|
|
|
$extension = new TimeFormExtension( |
159
|
|
|
$this->timeProvider->reveal(), |
160
|
|
|
$this->translator->reveal(), |
161
|
|
|
[ |
162
|
|
|
'global' => true, |
163
|
|
|
'min' => 10, |
164
|
|
|
'max' => 30, |
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
$extension->configureOptions($resolver); |
168
|
|
|
|
169
|
|
|
$result = $resolver->resolve(); |
170
|
|
|
|
171
|
|
|
static::assertTrue($result['antispam_time']); |
172
|
|
|
static::assertSame(10, $result['antispam_time_min']); |
173
|
|
|
static::assertSame(30, $result['antispam_time_max']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testExtendedTypes(): void |
177
|
|
|
{ |
178
|
|
|
static::assertSame([FormType::class], TimeFormExtension::getExtendedTypes()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testExtendedType(): void |
182
|
|
|
{ |
183
|
|
|
$extension = new TimeFormExtension( |
184
|
|
|
$this->timeProvider->reveal(), |
185
|
|
|
$this->translator->reveal(), |
186
|
|
|
[] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
static::assertSame(FormType::class, $extension->getExtendedType()); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|