tr.am
tr.am

Reputation: 295

Error Unit test symfony

When I try to execute this unit test I have a problem so this is my function testErrors
.

    public function testErrors(){

    $client = static::createClient();

    $crawler = $client->request('GET', '/add');
    $form = $crawler->selectButton('save')->form(array(
    'user[firstName]'      => 'test1',
    'user[lastName]'       => 'test',
    'user[email]'          => '[email protected]',
    ));
    $crawler = $client->submit($form);

    //  3 errors
    $this->assertTrue($crawler->filter('.error_list')->count() == 3);
    // Error firstName field
    $this->assertTrue($crawler->filter('#firstName')->siblings()->first()->filter('.error_list')->count() == 1);
    // Error lasName field
    $this->assertTrue($crawler->filter('#lastName')->siblings()->first()->filter('.error_list')->count() == 1);
    // Error email field
    $this->assertTrue($crawler->filter('#email')->siblings()->first()->filter('.error_list')->count() == 1);

}

I have this problem

InvalidArgumentException: The current node list is empty .

this is my Controller

   /**
 * @Route("/add", name="addPage")
 */
public function AddAction(Request $request)
{

    $user = new User();

    $form = $this->createFormBuilder($user)
        ->add('email', TextType::class)
        ->add('firstName', TextType::class)
        ->add('lastName', TextType::class)
        ->add('save', SubmitType::class, array('label' => 'Add'))
        ->getForm();


    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {


    $firstName = $form['firstName']->getData();
    $lastName = $form['lastName']->getData();
    $email = $form['email']->getData();
    $user->setFirstName($firstName);
    $user->setLastName($lastName);
    $user->setEmail($email);
    $em = $this->getDoctrine()->getManager();
    $em->persist($user);
    $em->flush();
    $this->addFlash('notice','user added' );
    return $this->redirectToRoute('listPage');
    }

Upvotes: 2

Views: 173

Answers (1)

jkrnak
jkrnak

Reputation: 956

I think it's your selectButton('save') what gives you the error. Try it with your button label Add instead of save

Upvotes: 1

Related Questions