2016-02-16 31 views
0

嗨Guyz我在php單元測試中遇到了困難,任何人都可以創建一個窗體和單元測試,它與控制器相同,對我來說這將是一個很大的幫助作爲一名學生,我們正在學習框架,symfony2就是其中之一,php單元測試是其中的一個特性,所以我們需要了解它,我希望有人能幫助我不建議任何鏈接閱讀,因爲我厭倦了閱讀需要一個使用模擬對象的簡單示例。Symfony php單元測試在窗體和控制器中使用模擬對象

我在symfony 2中創建了一個簡單的表單,任何人都可以使用模擬對象爲此創建一個phpunit測試。

<?php 
namespace App\MainBundle\Form\Exercises; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 


class TennisType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('sets', 'text',['attr'=>['maxlength'=>'3', 'size'=>'3'],'required' => false,]) 
      ->add('duration', 'text', ['attr'=> ['maxlength'=>'3', 'size'=>'3'],'required' => True,]) 
      ->add('intensity', 'choice', ['choices'=> ['low'=>'low', 'medium'=>'medium', 'high'=>'high', 'intense'=>'intense'],'required' => True,]) 
      ->add('unit', 'choice', ['choices'=> ['hour'=>'hour', 'min'=>'min'],'required' => True,]) 
      ->add('field', new ExerciseLogType()) 
      ; 
    } 


    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'App\MainBundle\Entity\Exercises\ExerciseLog' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'tennis_exerciselog'; 
    } 
} 
+0

我創建了形式簡單的代碼,你可以讓我爲這個單元測試,使用mock對象 –

回答

1

這是一個簡單表單提交測試的例子。您可以使用提醒工具提示通知創建是否正確,並使用抓取工具獲取響應表單提交。

$client = static::makeClient()); 

$crawler = $client->request('GET', self::ROUTE); 

$form = $crawler->selectButton('Acme_mainbundle_produit[submit]')->form(); 
$form['Acme_mainbundle_produit[name]'] = 'PRODUIT TEST CREATE'; 

$client->submit($form); 

$this->assertTrue($client->getResponse()->isRedirect(self::ROUTE)); 

$crawler = $client->followRedirect(); 

$this->assertTrue($crawler->filter('.alert.alert-success')->count() == 1); 
+0

感謝你爲這個,但是這不是我期待的,這是,測試表單提交,但這對我來說是一個很高的水平,所以我在我的問題中添加一個簡單的程序 –

相關問題