循环过滤结果会产生伪造的空HTML属性

时间:2017-11-06 17:11:29

标签: php symfony functional-testing

我在功能测试中得到了这个:

$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

我的循环中是否存在某种错误?

1 个答案:

答案 0 :(得分:0)

$this->crawler对象来自之前的请求。

我替换了这个(我粘贴的代码块上面的语句):

$this->client->submit($form, $formData);

使用:

$this->crawler = $this->client->submit($form, $formData);

它起作用了:

;test 0: with value=4 checked=checked should be=1 mobilformbundle_customer_questionnaires_questionnaires_4
;test 1: with value=5 checked=checked should be=1 mobilformbundle_customer_questionnaires_questionnaires_5
;test 2: with value=6 checked=checked should be= mobilformbundle_customer_questionnaires_questionnaires_6
;test 3: with value=10 checked= should be= mobilformbundle_customer_questionnaires_questionnaires_10

测试通过了!

抱歉这个愚蠢的问题。随意关闭投票。