2

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');
    }
8
  • which statement/line give this error? Commented Feb 9, 2017 at 15:58
  • and why you expect that the form get 3 validation error when don't have any constraint? Commented Feb 9, 2017 at 16:00
  • Error in this line "" )); "" so in this statement $form = $crawler->selectButton('save')->form(array( 'user[firstName]' => 'test1', 'user[lastName]' => 'test', 'user[email]' => '[email protected]', )); Commented Feb 9, 2017 at 16:12
  • 1
    and I will to add a constraints for fields Commented Feb 9, 2017 at 16:12
  • 1
    before accessing the crawler, check that the request went fine with something like $this->assertTrue($client->getResponse()->isSuccessful()); after client->request Commented Feb 9, 2017 at 16:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

On which line? let's try separating the statements to track down your error to something like this: ``` $button = $crawler->selectButton('Add'); $form = $button->form(); $form->get('form[firstName]')->setValue('test1'); $form->get('form[lastName]')->setValue('test'); $form->get('form[email]')->setValue('[email protected]'); ``` Also I'm using 'form' as the fieldnames selector because for me that's how it's rendering the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.