Passed
Push — master ( 264b27...87ebb2 )
by Dmitrii
03:44
created

FunctionalTest::assertFormHasCaptchaError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Karser\Recaptcha3Bundle\Tests;
4
5
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
6
use Karser\Recaptcha3Bundle\Tests\fixtures\RecaptchaMock;
7
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\Form\Extension\Core\Type\FormType;
11
use Symfony\Component\Form\Extension\Core\Type\TextType;
12
use Symfony\Component\Form\FormFactoryInterface;
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\Validator\Constraints\NotBlank;
15
16
class FunctionalTest extends TestCase
17
{
18
    /** @var TestKernel */
19
    private $kernel;
20
21
    private $formFactory;
22
    private $twig;
23
24
    public function setUp(): void
25
    {
26
        $this->kernel = new TestKernel(uniqid(), false);
27
    }
28
29
    public function testFormJavascriptPresent_ifEnabled()
30
    {
31
        //GIVEN
32
        $this->bootKernel('default.yml');
33
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
34
35
        $template = $this->twig->createTemplate('{{ form_widget(form) }}');
0 ignored issues
show
Bug introduced by
The method createTemplate() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $template = $this->twig->createTemplate('{{ form_widget(form) }}');

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...
36
        //WHEN
37
        $view = $template->render(['form' => $form->createView()]);
38
39
        //THEN
40
        self::assertContains('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
41
        self::assertContains('<script src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_captcha" async defer></script>', $view);
42
        self::assertContains("document.getElementById('form_captcha').value = token;", $view);
43
    }
44
45
    public function testFormJavascriptAbsent_ifDisabled()
46
    {
47
        //GIVEN
48
        $this->bootKernel('disabled.yml');
49
50
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
51
        $template = $this->twig->createTemplate('{{ form_widget(form) }}');
52
        //WHEN
53
        $view = $template->render(['form' => $form->createView()]);
54
55
        //THEN
56
        self::assertContains('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
57
        self::assertNotContains('<script src="https://www.google.com/recaptcha/api.js?render=key"></script>', $view);
58
        self::assertNotContains("document.getElementById('form_captcha').value = token;", $view);
59
    }
60
61
    public function testFormValid_ifEnabled()
62
    {
63
        //GIVEN
64
        $container = $this->bootKernel('default.yml');
65
66
        /** @var RecaptchaMock $recaptchaMock */
67
        $recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha');
68
        $recaptchaMock->nextSuccess = true;
69
70
        //WHEN
71
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
72
73
        //THEN
74
        $form->submit(['name' => 'John', 'captcha' => 'token']);
75
        self::assertTrue($form->isSubmitted());
76
        self::assertTrue($form->isValid());
77
    }
78
79
    public function testFormInvalid_ifCaptchaFails()
80
    {
81
        //GIVEN
82
        $container = $this->bootKernel('default.yml');
83
84
        /** @var RecaptchaMock $recaptchaMock */
85
        $recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha');
86
        $recaptchaMock->nextSuccess = false;
87
88
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
89
90
        //WHEN
91
        $form->submit(['name' => 'John', 'captcha' => 'token']);
92
        //THEN
93
        $this->assertFormHasCaptchaError($form);
94
    }
95
96
    public function testFormInvalid_ifCaptchaEmpty()
97
    {
98
        //GIVEN
99
        $container = $this->bootKernel('default.yml');
100
101
        /** @var RecaptchaMock $recaptchaMock */
102
        $recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha');
103
        $recaptchaMock->nextSuccess = false;
104
105
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
106
107
        //WHEN
108
        $form->submit(['name' => 'John', 'captcha' => '']);
109
        //THEN
110
        $this->assertFormHasCaptchaError($form);
111
    }
112
113
    public function testFormInvalid_ifCaptchaNull()
114
    {
115
        //GIVEN
116
        $container = $this->bootKernel('default.yml');
117
118
        /** @var RecaptchaMock $recaptchaMock */
119
        $recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha');
120
        $recaptchaMock->nextSuccess = false;
121
122
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
123
124
        //WHEN
125
        $form->submit(['name' => 'John', 'captcha' => null]);
126
        //THEN
127
        $this->assertFormHasCaptchaError($form);
128
    }
129
130
    public function testFormInvalid_ifCaptchaUndefined()
131
    {
132
        //GIVEN
133
        $container = $this->bootKernel('default.yml');
134
135
        /** @var RecaptchaMock $recaptchaMock */
136
        $recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha');
137
        $recaptchaMock->nextSuccess = false;
138
139
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
140
141
        //WHEN
142
        $form->submit(['name' => 'John']);
143
        //THEN
144
        $this->assertFormHasCaptchaError($form);
145
    }
146
147
    public function testFormValid_ifCaptchaFails_butDisabled()
148
    {
149
        //GIVEN
150
        $container = $this->bootKernel('disabled.yml');
151
152
        /** @var RecaptchaMock $recaptchaMock */
153
        $recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha');
154
        $recaptchaMock->nextSuccess = false;
155
156
        $form = $this->createContactForm($this->formFactory);
0 ignored issues
show
Bug introduced by
It seems like $this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
        $form = $this->createContactForm(/** @scrutinizer ignore-type */ $this->formFactory);
Loading history...
157
158
        //WHEN
159
        $form->submit(['name' => 'John', 'captcha' => 'token']);
160
        //THEN
161
        self::assertTrue($form->isSubmitted());
162
        self::assertTrue($form->isValid());
163
    }
164
165
    private function assertFormHasCaptchaError(FormInterface $form)
166
    {
167
        self::assertTrue($form->isSubmitted());
168
        self::assertFalse($form->isValid());
169
        self::assertSame('Your computer or network may be sending automated queries',
170
            $form->getErrors()[0]->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
            $form->getErrors()[0]->/** @scrutinizer ignore-call */ 
171
                                   getMessage());

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...
171
    }
172
173
    private function bootKernel(string $config): ContainerInterface
174
    {
175
        $this->kernel->setConfigurationFilename(__DIR__ . '/fixtures/config/'.$config);
176
        $this->kernel->boot();
177
        $container = $this->kernel->getContainer();
178
        $this->formFactory = $container->get('form.factory');
179
        $this->twig = $container->get('twig');
180
181
        return $container;
182
    }
183
184
    private function createContactForm(FormFactoryInterface $formFactory)
185
    {
186
        return $formFactory->createBuilder(FormType::class)
187
            ->add('name', TextType::class, [
188
                'constraints' => [
189
                    new NotBlank(),
190
                ],
191
            ])
192
            ->add('captcha', Recaptcha3Type::class, [
193
                'constraints' => new Recaptcha3(),
194
            ])
195
            ->getForm();
196
    }
197
}
198