【发布时间】:2017-11-06 17:11:28
【问题描述】:
我的功能测试中有这个:
$questionnaires = [4, 5];
$boxes = $this->crawler->filter('#' . CustomerQuestionnairesType::BLOCK_PREFIX . '_questionnaires input[type=checkbox]');
$count = $boxes->count();
$this->assertGreaterThan(0, $count, "Couldn't find questionnaire check boxes");
for ($i = 0; $i < $count; $i++) {
$box = $boxes->getNode($i);
$value = $box->getAttribute('value');
$checked = $box->getAttribute('checked');
$in_array = in_array($value, $questionnaires);
echo ";test $i: with value=$value checked=" . $checked . " should be=$in_array ". $box->getAttribute('id')."\n";
$this->assertEquals($in_array ? 'checked' : '', $checked, "Invalid questionnaire map");
}
这是 HTML(保存自 $client->getResponse()->getContent()):
<div id="mobilformbundle_customer_questionnaires_questionnaires">
<div class="checkbox">
<label class=""><input checked="checked" id="mobilformbundle_customer_questionnaires_questionnaires_4" name="mobilformbundle_customer_questionnaires[questionnaires][]"
type="checkbox" value="4"> questionnaire1</label>
</div>
<div class="checkbox">
<label class=""><input checked="checked" id="mobilformbundle_customer_questionnaires_questionnaires_5" name="mobilformbundle_customer_questionnaires[questionnaires][]"
type="checkbox" value="5"> questionnaire2</label>
</div>
<div class="checkbox">
<label class=""><input checked="checked" id="mobilformbundle_customer_questionnaires_questionnaires_6" name="mobilformbundle_customer_questionnaires[questionnaires][]"
type="checkbox" value="6"> questionnaire3</label>
</div>
<div class="checkbox">
<label class=""><input id="mobilformbundle_customer_questionnaires_questionnaires_10" name="mobilformbundle_customer_questionnaires[questionnaires][]"
type="checkbox" value="10"> generic questionnaire</label>
</div>
</div>
如您所见,前 3 个框已选中(HTML 属性 checked 的值为 checked)。
这是我的测试的输出:
;test 0: with value=4 checked=checked should be=1 mobilformbundle_customer_questionnaires_questionnaires_4
;test 1: with value=5 checked= should be=1 mobilformbundle_customer_questionnaires_questionnaires_5
Expected :'checked'
Actual :''
我不明白为什么$checked PHP 变量是空的。它应该包含checked。
我的循环中是否存在某种错误?
【问题讨论】:
-
循环对我来说看起来不错。有点奇怪吧?
标签: php symfony functional-testing