mardi 12 mars 2019

Symfony writing a FormType test that uses autowired services

I'm trying to write a test for a FormType that makes use of a couple of services that are autowired to it.

use AppBundle\Service\UserHydrator;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class UserType extends AbstractType {

    protected $auth;
    protected $userHydrator;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker, UserHydrator $userHydrator) {
        $this->auth = $authorizationChecker;
        $this->userHydrator = $userHydrator;
    }

    public function buildForm(...) {...}
}

The test I have written comes mostly from this Symfony guide

class UserTypeTest extends TypeTestCase {

    public function testSubmitValidData() {
        $formData = array("email" => "test@example.com");

        $userResult = new User();
        $form = $this->factory->create(UserType::class, $userResult);

        $user = new User();
        $user->setEmail("test@example.com");

        $form->submit($formData);

        $this->assertTrue($form->isSynchronized());
        $this->assertEquals($user, $userResult);
    }

}

However this fails with the following error when running the test.

ArgumentCountError : Too few arguments to function AppBundle\Form\UserType::__construct(), 0 passed in ... on line 92 and exactly 2 expected

I can't seem to figure out how to autowire in these serivices when running the test. Is there something I'm missing, or should I even be trying to autowire these services to begin with?

Aucun commentaire:

Enregistrer un commentaire