PhpUnit表单不提交,没有错误

时间:2017-05-25 10:09:35

标签: forms symfony phpunit functional-testing

我正在PhpUnit中进行功能测试,以测试简单的模态窗口功能,它只有一个字段和两个按钮(我尝试了各种方式,因此代码可能不干净,我只是为了表明这个想法而粘贴) :

    $form = $crawler
         // find all buttons with the text "Pridėti"
        ->filter('button:contains("Pridėti")') 
        ->eq(0)
        ->form()// select the first button in the list
    ;

    $form['appbundle_classinfo[name]'] = '5a';
    $crawler = $client->submit($form);

    //It doesn't even save to database 
    $container = self::$kernel->getContainer();
    $em = $container->get('doctrine')->getManager();
    $classinfo = $em->getRepository('AppBundle:ClassInfo');

    //Echoes modal window
    echo $client->getResponse()->getContent() ;die;

我也试过var_dump表单,它表明该值被添加到表单中: string(25)“appbundle_classinfo [name]”           [ “值”:保护] =>           string(2)“5a”

因此表单未提交。你能帮我找出原因吗?

1 个答案:

答案 0 :(得分:0)

<?php

namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PostTest extends WebTestCase
{
    public function testShowPost()
    {
        //returns a Client, your browser use your web site
        $client = static::createClient();

         // The request() method (read more about the request method) returns a Crawler object which can be used to select elements in the response, click on links and submit forms.
        $crawler = $client->request('GET', '/your/route/controller/action');

        // select button by name or id
         $form = $crawler->selectButton('Login')->submit();

         //submit the form
         $crawler->submit($form);

         $this->assertGreaterThan(
             0,
             $crawler->filter('html:contains("hello world")')->count()
         );

        // get HTML content returned
        dump($client->getResponse()->getContent());

    }

http://symfony.com/doc/current/testing.html#functional-tests

希望这很有用,请告诉我。