我想PHPUnit的測試方法聯繫人形式的接觸形式PHPUnit的
public function testContact()
{
$client = static::createClient();
$form = $crawler->selectButton('Submit')->form();
$form['blogbundle_enquirytype[name]'] = 'name';
// other form field assignments here
$crawler = $client->submit($form);
$this->assertEquals(1, $crawler->filter('.blogger-notice:contains("Your contact enquiry was successfully sent. Thank you!")')->count());
}
的PHPUnit不承認blogbundle_enquirytype
(無法到達現場),大概是因爲控制器實例化這讓喜歡本
$enquiry = new Enquiry();
$form = $this->createForm(EnquiryType::class, $enquiry);
儘管AbstractType
:
class EnquiryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('email', EmailType::class);
$builder->add('subject');
$builder->add('body', TextareaType::class);
}
public function getName(){
return 'blogbundle_enquirytype';
}
有沒有另外一種方法來設置EnquiryType
的名稱,或者讓Phpunit識別表單?
這裏是形式
<form action="/contact" method="post" class="blogger">
<div><label for="enquiry_name" class="required">Name</label><input type="text" id="enquiry_name" name="enquiry[name]" required="required" maxlength="255" /></div>
<div><label for="enquiry_email" class="required">Email</label><input type="email" id="enquiry_email" name="enquiry[email]" required="required" /></div>
<div><label for="enquiry_subject" class="required">Subject</label><input type="text" id="enquiry_subject" name="enquiry[subject]" required="required" maxlength="50" /></div>
<div><label for="enquiry_body" class="required">Body</label><textarea id="enquiry_body" name="enquiry[body]" required="required"></textarea></div>
<input type="hidden" id="enquiry__token" name="enquiry[_token]" value="-eZq7Go6ELXykluf0Fca_CPvzeB3yEUj2yuOnyamYBU" />
<input type="submit" value="Submit" />
</form>
檢查生成的頁面是否爲有效的html表單:非缺失的閉合標記可能會掛起選擇器。 – Matteo
檢查你的頁面中只有一個表單:你選擇一個名爲submit的按鈕,你確定你只有一個? – Matteo
@Matteo好主意。 1.我運行'var_dump($ client-> getResponse() - > getContent())'並將phpunit輸出放入'validator.w3.org'中,但頁面或表單上沒有錯誤。 2.頁面上只有一個提交按鈕 – Michael