2017-05-25 77 views
0

我正在PhpUnit中进行功能测试,以测试只有一个字段和两个按钮的简单模式窗口功能(我尝试过各种方法,因此代码可能不干净,米粘贴只是为了显示的想法):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形式,它示出了值被添加到形式: 串(25)“appbundle_classinfo [名称]” [“值” :protected] => string(2)“5a”

所以表单没有提交。你能帮我找出原因吗?

+1

您是否检查过API? 'form()'返回一个数组吗? –

回答

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

希望是有用的,让我知道。