1 | <?php |
||||||
2 | |||||||
3 | /** |
||||||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||||||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||||||
6 | */ |
||||||
7 | |||||||
8 | namespace SprykerTest\Shared\Symfony\Form\Extension; |
||||||
9 | |||||||
10 | use Codeception\Test\Unit; |
||||||
11 | use Spryker\Shared\Symfony\Form\Extension\DoubleSubmitProtection\DoubleSubmitProtectionExtension; |
||||||
12 | use Spryker\Shared\Symfony\Form\Extension\DoubleSubmitProtection\RequestTokenProvider\StorageInterface; |
||||||
13 | use Spryker\Shared\Symfony\Form\Extension\DoubleSubmitProtection\RequestTokenProvider\TokenGeneratorInterface; |
||||||
14 | use Symfony\Component\Form\Extension\Core\Type\FormType; |
||||||
15 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
||||||
16 | use Symfony\Component\Form\Forms; |
||||||
17 | use Symfony\Contracts\Translation\TranslatorInterface; |
||||||
18 | |||||||
19 | /** |
||||||
20 | * Auto-generated group annotations |
||||||
21 | * |
||||||
22 | * @group SprykerTest |
||||||
23 | * @group Shared |
||||||
24 | * @group Symfony |
||||||
25 | * @group Form |
||||||
26 | * @group Extension |
||||||
27 | * @group DoubleSubmitProtectionExtensionTest |
||||||
28 | * Add your own group annotations below this line |
||||||
29 | */ |
||||||
30 | class DoubleSubmitProtectionExtensionTest extends Unit |
||||||
31 | { |
||||||
32 | /** |
||||||
33 | * @var \Spryker\Shared\Symfony\Form\Extension\DoubleSubmitProtection\Type\DoubleSubmitFormType |
||||||
34 | */ |
||||||
35 | protected $doubleSubmitFormType; |
||||||
36 | |||||||
37 | /** |
||||||
38 | * @var \Spryker\Shared\Symfony\Form\Extension\DoubleSubmitProtection\RequestTokenProvider\StorageInterface |
||||||
39 | */ |
||||||
40 | protected $storage; |
||||||
41 | |||||||
42 | /** |
||||||
43 | * @var \Spryker\Shared\Symfony\Form\Extension\DoubleSubmitProtection\RequestTokenProvider\TokenGeneratorInterface |
||||||
44 | */ |
||||||
45 | protected $generator; |
||||||
46 | |||||||
47 | /** |
||||||
48 | * @var \Symfony\Component\OptionsResolver\OptionsResolver |
||||||
49 | */ |
||||||
50 | protected $optionResolver; |
||||||
51 | |||||||
52 | /** |
||||||
53 | * @var \Symfony\Contracts\Translation\TranslatorInterface |
||||||
54 | */ |
||||||
55 | protected $translator; |
||||||
56 | |||||||
57 | /** |
||||||
58 | * @var \Symfony\Component\Form\FormFactoryInterface |
||||||
59 | */ |
||||||
60 | protected $formFactory; |
||||||
61 | |||||||
62 | /** |
||||||
63 | * @return void |
||||||
64 | */ |
||||||
65 | protected function setUp(): void |
||||||
66 | { |
||||||
67 | parent::setUp(); |
||||||
68 | |||||||
69 | $this->generator = $this->getMockBuilder(TokenGeneratorInterface::class)->setMethods(['checkTokenEquals', 'generateToken'])->getMock(); |
||||||
70 | $this->storage = $this->getMockBuilder(StorageInterface::class)->setMethods(['getToken', 'setToken', 'deleteToken', 'checkTokenEquals'])->getMock(); |
||||||
71 | $this->translator = $this->getMockBuilder(TranslatorInterface::class)->setMethods(['trans', 'getLocale'])->getMock(); |
||||||
72 | $this->translator->method('trans')->willReturnArgument(0); |
||||||
73 | |||||||
74 | $this->formFactory = Forms::createFormFactoryBuilder() |
||||||
75 | ->addExtensions($this->getFormExtensions()) |
||||||
76 | ->getFormFactory(); |
||||||
77 | } |
||||||
78 | |||||||
79 | /** |
||||||
80 | * @return void |
||||||
81 | */ |
||||||
82 | public function testFinishViewIgnoredForNotFormRoot(): void |
||||||
83 | { |
||||||
84 | $view = $this->formFactory |
||||||
85 | ->createNamedBuilder('root', FormType::class) |
||||||
86 | ->add( |
||||||
87 | $this->formFactory->createNamedBuilder( |
||||||
88 | 'form', |
||||||
89 | FormType::class, |
||||||
90 | null, |
||||||
91 | ['token_field_name' => '_requestToken'], |
||||||
92 | ), |
||||||
93 | ) |
||||||
94 | ->getForm() |
||||||
95 | ->get('form') |
||||||
96 | ->createView(); |
||||||
97 | |||||||
98 | $this->assertFalse(isset($view['_requestToken'])); |
||||||
99 | } |
||||||
100 | |||||||
101 | /** |
||||||
102 | * @return void |
||||||
103 | */ |
||||||
104 | public function testFinishFormViewSuccess(): void |
||||||
105 | { |
||||||
106 | $expectedToken = 'TOKEN'; |
||||||
107 | $this->generator->expects($this->once()) |
||||||
0 ignored issues
–
show
|
|||||||
108 | ->method('generateToken') |
||||||
109 | ->willReturn($expectedToken); |
||||||
110 | |||||||
111 | $view = $this->formFactory |
||||||
112 | ->createNamed('FORM_NAME', FormType::class, null, ['token_field_name' => '_requestToken']) |
||||||
113 | ->createView(); |
||||||
114 | |||||||
115 | $this->assertSame($expectedToken, $view['_requestToken']->vars['value']); |
||||||
116 | } |
||||||
117 | |||||||
118 | /** |
||||||
119 | * @dataProvider booleanDataProvider |
||||||
120 | * |
||||||
121 | * @param bool $valid |
||||||
122 | * |
||||||
123 | * @return void |
||||||
124 | */ |
||||||
125 | public function testValidateTokenOnSubmit(bool $valid): void |
||||||
126 | { |
||||||
127 | $expectedToken = 'TOKEN'; |
||||||
128 | |||||||
129 | $this->storage->expects($this->once()) |
||||||
0 ignored issues
–
show
The method
expects() does not exist on Spryker\Shared\Symfony\F...ovider\StorageInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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.
Loading history...
|
|||||||
130 | ->method('getToken') |
||||||
131 | ->willReturn($expectedToken); |
||||||
132 | |||||||
133 | $this->generator->expects($this->once()) |
||||||
134 | ->method('checkTokenEquals') |
||||||
135 | ->with($expectedToken, $expectedToken) |
||||||
136 | ->will($this->returnValue($valid)); |
||||||
137 | |||||||
138 | $form = $this->formFactory |
||||||
139 | ->createBuilder(FormType::class, null, ['token_field_name' => '_requestToken']) |
||||||
140 | ->add('child', TextType::class) |
||||||
141 | ->getForm(); |
||||||
142 | |||||||
143 | $form->submit(['child' => 'foobar', '_requestToken' => $expectedToken]); |
||||||
144 | |||||||
145 | // Remove token from data |
||||||
146 | $this->assertSame(['child' => 'foobar'], $form->getData()); |
||||||
147 | |||||||
148 | // Validate accordingly |
||||||
149 | $this->assertSame($valid, $form->isValid()); |
||||||
150 | } |
||||||
151 | |||||||
152 | /** |
||||||
153 | * @return array |
||||||
154 | */ |
||||||
155 | protected function getFormExtensions(): array |
||||||
156 | { |
||||||
157 | return [ |
||||||
158 | new DoubleSubmitProtectionExtension($this->generator, $this->storage, $this->translator), |
||||||
159 | ]; |
||||||
160 | } |
||||||
161 | |||||||
162 | /** |
||||||
163 | * @return array |
||||||
164 | */ |
||||||
165 | public function booleanDataProvider(): array |
||||||
166 | { |
||||||
167 | return [ |
||||||
168 | [true], |
||||||
169 | [false], |
||||||
170 | ]; |
||||||
171 | } |
||||||
172 | } |
||||||
173 |
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.