symfony单元测试:添加/修改表单操作 [英] symfony unit tests: add/modify form action

查看:97
本文介绍了symfony单元测试:添加/修改表单操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有动作的表单(使用javascript提交),并且我正在尝试为此编写单元测试,但由于缺少"action"属性而失败:

I have a form, without action (is submitted with javascript) and I'm trying to write a unit test for it, but it fails because "action" attribute is missing:

InvalidArgumentException:当前URI必须是绝对URL(").

InvalidArgumentException : Current URI must be an absolute URL ("").

有没有一种方法可以在单元测试中添加它或使用搜寻器修改html内容?

There is a way to do add it in unit tests or modify the html content using the crawler?

<form id="form_search_page">
    <input type="text" name="keyword" value="" />
    <button type="submit" name="searchBtn" id="searchBtn">Search</button>
</form>


$client = $this->makeClient(true);
$url = $this->createRoute("page_index"));
$crawler = $client->request('GET', $url);
$response = $client->getResponse();

$form = $crawler->filter('#form_search_page')->form();
$params = array(
    "form[text]" => "dummy title"
);
$form->setValues($params);
$crawler = $client->submit($form);
$response = $client->getResponse();
$this->assertGreaterThan(0, $crawler->filter('.pages li')->count());

推荐答案

我找到了解决方法:

$crawler
    ->filter('form#form_search_page')
    ->reduce(function (Crawler $form) use ($router) {
        $url = $router->generate('search_page', array(), true);

        $node = $form->getNode(0);
        if (!$node->hasAttribute('action')){
            $node->setAttribute('action', $url);
            $node->setAttribute('method', 'POST');
            return true;
        }
        return false;
    })
    ->first();

这篇关于symfony单元测试:添加/修改表单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆