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\FormTypeExtensionInterface; |
21
|
|
|
use Symfony\Component\Form\FormView; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
24
|
|
|
|
25
|
|
|
class TimeFormExtensionTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
private $timeProvider; |
28
|
|
|
|
29
|
|
|
private $translator; |
30
|
|
|
|
31
|
|
|
protected function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->timeProvider = $this->prophesize(TimeProviderInterface::class); |
34
|
|
|
$this->translator = $this->prophesize(TranslatorInterface::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testItIsInstantiable(): void |
38
|
|
|
{ |
39
|
|
|
$this->assertInstanceOf(FormTypeExtensionInterface::class, new TimeFormExtension( |
40
|
|
|
$this->timeProvider->reveal(), |
41
|
|
|
$this->translator->reveal(), |
42
|
|
|
[] |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testBuildForm(): void |
47
|
|
|
{ |
48
|
|
|
$builder = $this->prophesize(FormBuilderInterface::class); |
49
|
|
|
$builder->addEventSubscriber(Argument::type(AntiSpamTimeListener::class)) |
50
|
|
|
->shouldBeCalled() |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
$extension = new TimeFormExtension( |
54
|
|
|
$this->timeProvider->reveal(), |
55
|
|
|
$this->translator->reveal(), |
56
|
|
|
[] |
57
|
|
|
); |
58
|
|
|
$extension->buildForm($builder->reveal(), [ |
59
|
|
|
'antispam_time' => true, |
60
|
|
|
'antispam_time_min' => 10, |
61
|
|
|
'antispam_time_max' => 30, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testBuildFormWithDisabledAntispam(): void |
66
|
|
|
{ |
67
|
|
|
$builder = $this->prophesize(FormBuilderInterface::class); |
68
|
|
|
|
69
|
|
|
$extension = new TimeFormExtension( |
70
|
|
|
$this->timeProvider->reveal(), |
71
|
|
|
$this->translator->reveal(), |
72
|
|
|
[] |
73
|
|
|
); |
74
|
|
|
$extension->buildForm($builder->reveal(), [ |
75
|
|
|
'antispam_time' => false, |
76
|
|
|
'antispam_time_min' => 10, |
77
|
|
|
'antispam_time_max' => 30, |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$this->assertTrue(true); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testFinishView(): void |
84
|
|
|
{ |
85
|
|
|
$view = $this->prophesize(FormView::class); |
86
|
|
|
$form = $this->prophesize(FormInterface::class); |
87
|
|
|
$form->getName() |
88
|
|
|
->willReturn('my_form') |
89
|
|
|
; |
90
|
|
|
|
91
|
|
|
$this->timeProvider->createFormProtection('my_form') |
92
|
|
|
->shouldBeCalled() |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
$extension = new TimeFormExtension( |
96
|
|
|
$this->timeProvider->reveal(), |
97
|
|
|
$this->translator->reveal(), |
98
|
|
|
[] |
99
|
|
|
); |
100
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
101
|
|
|
'compound' => true, |
102
|
|
|
'antispam_time' => true, |
103
|
|
|
'antispam_time_min' => 10, |
104
|
|
|
'antispam_time_max' => 30, |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testFinishViewForChildForm(): void |
109
|
|
|
{ |
110
|
|
|
$view = $this->prophesize(FormView::class); |
111
|
|
|
$view->parent = $this->prophesize(FormView::class)->reveal(); |
112
|
|
|
$form = $this->prophesize(FormInterface::class); |
113
|
|
|
|
114
|
|
|
$extension = new TimeFormExtension( |
115
|
|
|
$this->timeProvider->reveal(), |
116
|
|
|
$this->translator->reveal(), |
117
|
|
|
[] |
118
|
|
|
); |
119
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
120
|
|
|
'compound' => true, |
121
|
|
|
'antispam_time' => true, |
122
|
|
|
'antispam_time_min' => 10, |
123
|
|
|
'antispam_time_max' => 30, |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
$this->assertTrue(true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testFinishViewWithDisbaledAntispam(): void |
130
|
|
|
{ |
131
|
|
|
$view = $this->prophesize(FormView::class); |
132
|
|
|
$form = $this->prophesize(FormInterface::class); |
133
|
|
|
|
134
|
|
|
$extension = new TimeFormExtension( |
135
|
|
|
$this->timeProvider->reveal(), |
136
|
|
|
$this->translator->reveal(), |
137
|
|
|
[] |
138
|
|
|
); |
139
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
140
|
|
|
'compound' => true, |
141
|
|
|
'antispam_time' => false, |
142
|
|
|
'antispam_time_min' => 10, |
143
|
|
|
'antispam_time_max' => 30, |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
$this->assertTrue(true); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testConfigureOptions(): void |
150
|
|
|
{ |
151
|
|
|
$resolver = new OptionsResolver(); |
152
|
|
|
|
153
|
|
|
$extension = new TimeFormExtension( |
154
|
|
|
$this->timeProvider->reveal(), |
155
|
|
|
$this->translator->reveal(), |
156
|
|
|
[ |
157
|
|
|
'global' => true, |
158
|
|
|
'min' => 10, |
159
|
|
|
'max' => 30, |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
$extension->configureOptions($resolver); |
163
|
|
|
|
164
|
|
|
$result = $resolver->resolve(); |
165
|
|
|
|
166
|
|
|
$this->assertTrue($result['antispam_time']); |
167
|
|
|
$this->assertSame(10, $result['antispam_time_min']); |
168
|
|
|
$this->assertSame(30, $result['antispam_time_max']); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testExtendedTypes(): void |
172
|
|
|
{ |
173
|
|
|
$this->assertSame([FormType::class], TimeFormExtension::getExtendedTypes()); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|