dimanche 28 janvier 2018

Functional test using Phpunit : InvalidArgumentException: Unreachable field

I'm trying to test a form but i got unreachable field exception.

My controller's code :

class StudentController extends Controller
{
/**
 * @Route("/student/new",name="create_new_student")
 */
   public function newAction(Request $request){



      $student = new Student();

      $form = $this->createFormBuilder($student)->add('name',TextType::class)
          ->add('save',SubmitType::class,['label' => 'Create student'])->getForm();

      $form->handleRequest($request);

      if($form->isSubmitted()){
           $student = $form->getData();
            $name = $student->getName();

            echo "Your name is ".$name;

            die();

      }

     return $this->render(':Student:new.html.twig',['form' => $form->createView()]);
   }
  }

My StudentControllerTest :

class StudentControllerTest extends WebTestCase
    {

  public function testNew(){

      $client = static::createClient();



      $crawler = $client->request('POST','/student/new');

      $form = $crawler->selectButton('Create student')->form();

      $form['name'] = 'Student1';


      $crawler = $client->submit($form);



      $this->assertGreaterThan(0,$crawler->filter('html:contains("Your name is Student1")')->count());


  }
    }

When i run the test using phpunit i got :

InvalidArgumentException: Unreachable field "name"

I'm following the tutorial from https://symfony.com/doc/current/testing.html

Aucun commentaire:

Enregistrer un commentaire