2017-05-25 78 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

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