Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create and run test #58

Open
featuriz opened this issue Nov 23, 2022 · 0 comments
Open

Unable to create and run test #58

featuriz opened this issue Nov 23, 2022 · 0 comments

Comments

@featuriz
Copy link

I followed your readme and successfully integrate google recaptcha3 to my site. It works fine. But when testing it throws error.

Errror:

App\Tests\Form\ContactTypeTest::testSubmitValidData                          
ArgumentCountError: Too few arguments to function Karser\Recaptcha3Bundle\Form\Recaptcha3Type::__construct(), 0 passed in /home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormRegistry.php on line 81 and exactly 3 expected         
                                                                                
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/karser/karser-recaptcha3-bundle/Form/Recaptcha3Type.php:22                                                        
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormRegistry.php:81  
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormFactory.php:67   
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormBuilder.php:91   
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormBuilder.php:238  
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormBuilder.php:189  
/home/sudhakar/SudhakarK/NB/2022/FZMFW/vendor/symfony/form/FormFactory.php:31   
/home/sudhakar/SudhakarK/NB/2022/FZMFW/tests/Form/ContactTypeTest.php:33 

My works:
I'm not using .env for config. I build symfony from microkernel as per Building your own Framework with the MicroKernelTrait

CODE:
ContactType:

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use App\Entity\Contact;
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;

class ContactType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void {
        $builder
                ->add('name', TextType::class, ['attr' => ['class' => 'form-control', 'placeholder' => 'name']])
                ->add('email', EmailType::class, ['attr' => ['class' => 'form-control', 'placeholder' => 'email']])
                ->add('phone', TextType::class, ['attr' => ['class' => 'form-control', 'placeholder' => 'contact']])
                ->add('message', TextareaType::class, ['attr' => ['class' => 'form-control', 'placeholder' => 'info', 'rows' => '5']])
# TEST PASSES WHEN I REMOVE 'captcha' (following lines completely)
                ->add('captcha', Recaptcha3Type::class, [
                    'constraints' => new Recaptcha3(['message' => 'There were problems with your captcha. Please try again or contact with support and provide following code(s): {{ errorCodes }}']),
                    'action_name' => 'contactpage',
                    'locale' => 'en',
        ]);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver): void {
        $resolver->setDefaults(array(
            'data_class' => Contact::class,
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            'csrf_token_id' => 'task_item',
        ));
    }

}

Here # TEST PASSES WHEN I REMOVE 'captcha' (I mentioned in above code)

ContactTypeTest:

<?php

/*
 * Copyright (C) 2009 - 2022
 * Author:   Sudhakar Krishnan <[email protected]>
 * License:  http://www.featuriz.in/licenses/LICENSE-2.0
 * Created:  Mon, 14 Nov 2022 13:30:28 IST
 */

namespace App\Tests\Form;

use App\Form\ContactType;
use App\Entity\Contact;
use Symfony\Component\Form\Test\TypeTestCase;

/**
 * Description of ContactTypeTest
 *
 * @author Sudhakar Krishnan <[email protected]>
 */
class ContactTypeTest extends TypeTestCase {

    public function testSubmitValidData() {
        $formData = [
            'name' => 'My Name',
            'email' => 'info',
            'phone' => '123',
            'message' => 'hi',
        ];

        $model = new Contact();
        // $model will retrieve data from the form submission; pass it as the second argument
        $form = $this->factory->create(ContactType::class, $model);

        $expected = new Contact();
        // ...populate $expected properties with the data stored in $formData
        $expected->setName("My Name")->setEmail("info")->setPhone("123")->setMessage("hi");

        // submit the data to the form directly
        $form->submit($formData);

        // This check ensures there are no transformation failures
        $this->assertTrue($form->isSynchronized());

        // check that $model was modified as expected when the form was submitted
        $this->assertEquals($expected, $model);

        $this->assertEquals($expected, $form->getData());

        $view = $form->createView();
        $children = $view->children;

        foreach (array_keys($formData) as $key) {
            $this->assertArrayHasKey($key, $children);
        }
    }

    public function testCustomFormView() {
        $formData = new Contact();
        // ... prepare the data as you need
        $formData->setName("My Name")->setEmail("info")->setPhone("123")->setMessage("hi");

        // The initial data may be used to compute custom view variables
        $view = $this->factory->create(ContactType::class, $formData)
                ->createView();

        $this->assertArrayHasKey('id', $view->vars);
        $this->assertArrayHasKey('name', $view->vars);
        $this->assertArrayHasKey('method', $view->vars);
        $this->assertSame('contact', $view->vars['id']);
        $this->assertSame('contact', $view->vars['name']);
        $this->assertSame('POST', $view->vars['method']);
    }

}

I don't know what I'm doing wrong here.
My config:

karser_recaptcha3:
    site_key: "XXXSECRET"
    secret_key: "XXXSECRET"
    score_threshold: 0.5
    enabled: true

when@prod:
    karser_recaptcha3:
        site_key: "XXXSECRET"
        secret_key: "XXXSECRET"
        score_threshold: 0.5
        enabled: true        
        
when@test:
    karser_recaptcha3:
        enabled: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant