- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 symfony 2.3 中测试一个表单,该表单具有一个选择输入...以及一个文件上传(enctype multipart/form-data)
选择输入如下...
我用 DomCrawler 选择表单
$form = $crawler->selectButton->('更新')->form()
然后尝试设置select的值
$form['select'] = null
要么$form['select'] = ssjksjkajsdfj
如果我这样做,DomCrawler 中有一个内部验证系统会返回以下错误。
InvalidArgumentException:输入“select”不能将“ssjksjkajsdfj”作为值(可能的值:1、2、3)。
在 symfony 2.4 及更高版本中,DomCrawler/Form 中有一个名为 disableValidation()
的神奇方法,它运行良好。不幸的是,由于某些依赖项需要 2.3,我无法升级
我也试过直接使用$client->Request()
方法
$post_data = '------WebKitFormBoundaryi7fAxO2jrDFTmxef
Content-Disposition: form-data; name="select"
ssjksjkajsdfj
------WebKitFormBoundaryi7fAxO2jrDFTmxef--';
$client->request(
'POST',
$form->getUri(),
array(),
array(),
array(
'CONTENT_TYPE' => 'multipart/form-data; boundary=----WebKitFormBoundaryi7fAxO2jrDFTmxef',
),
$post_data
);
但是 symfony 似乎并不知道/不关心表单,只是返回一个没有任何错误消息的常规表单,表单处理程序出于某种原因不验证表单。
这是 Controller 中的updateAction
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CoyoteAdBundle:Ad')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Ad entity.');
}
$old_entity = $entity;
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$this->archiveImage($old_entity);
$entity->upload();
$em->flush();
$this->addSuccessFlash('Ad has been updated successfully!');
return $this->redirect($this->generateUrl('ad_show', array('id' => $id)));
}
return $this->render('CoyoteAdBundle:Ad:edit.html.twig', array(
'entity' => $entity,
'form' => $editForm->createView(),
));
}
在这里我发现了一个确切的问题...... Select impossible value in select inputs with Symfony DomCrawler
但答案不是我要找的。我想测试并确保服务器将返回 200 以及一条消息,让用户知道他们想要做什么是不可能的。
PS:我可以在 Chrome 中使用 Postman 达到预期的效果。
最佳答案
我不知道 SF 2.3 中有任何方法可以用作 disableValidation
。但是,我认为发送虚假请求应该可以解决问题。我还认为您使用请求方法的方式不正确。检查documentation对于 2.3 SF 版本,您有一个关于如何伪造请求的示例:
// Form submission with a file upload
use Symfony\Component\HttpFoundation\File\UploadedFile;
$photo = new UploadedFile(
'/path/to/photo.jpg',
'photo.jpg',
'image/jpeg',
123
);// Fake photo upload if you like, if not, just send the select value.
$client->request(
'POST',
'/submit',// valid route
array('select' => 'ssjksjkajsdfj'),// Substitute here the name of your select and the value you want to send
array('photo' => $photo)
);
它应该发送请求,如果一切正常, Controller 应该用 200 和错误进行应答。
希望对您有所帮助。
更新
好的,这里有几件事:CSRF 很重要,请求中的参数格式也很重要。
由于您的表单使用的是 CSRF,您应该在请求中发送它,否则它将失败并将您重定向到“空”表单。此外,请求必须以正确的格式发送数据。假设生成的表单代码以 my_form[name]
的形式命名输入,您应该相应地在请求中创建它们:
$csrfToken = $client->getContainer()->get('form.csrf_provider')->generateCsrfToken('my_form');// Generate CSRF for my_form
$crawler = $client->request(
'POST',
'/ajax/register', // valid route
array(
'my_form' => array(// form accepts params in form my_form[name]
'_token' => $csrfToken,
'select' => 'asdfasdf',// select I want to send invalid value
'photo' => $photo, // the uploaded photo
'whatever' => 'whatever', // rest of required parameters.
),
));
关于php - 如何使用 symfony 2.3 在 WebTestCase 中提交无效的选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075552/
我尝试在 PhpUnit 的 WebTestCase 中发送原始数据,但它不起作用: $jsonEvent = '{ "type": "invoice.payment_succeeded"
我正在尝试从 WebTestCase 中调用“router:debug”命令来自动验证所有静态路由。 class RoutesTest extends WebTestCase { public f
我想做的是对我的包(可重复使用的包)进行功能测试。更深入: 创建对给定 url /my/url 的请求> 检查是否调用了 MyParamConverter 并将请求转换为 MyObject 的实例>
我目前正在为我的 symfony 应用程序编写功能测试。我将 symfony 3 (3.1.6) 与 phpunit 5.6.1 一起使用。编辑:根据 Alvin Bunk 的要求,我的应用程序不是网
Symfony WebTestCase 是一个只启动内核并测试响应对象的工具,还是生成一个 PHP Web 服务器并针对服务器进行测试的工具? 这是为了了解我是否可以测试 Symfony 框架之外的遗
我尝试为我的应用编写功能测试,但出现以下错误: InvalidArgumentException: The option "test_case" must be set. 我尝试用谷歌搜索它,但几乎没
use Liip\FunctionalTestBundle\Test\WebTestCase; class ControllerTest extends WebTestCase { publi
检查 php bin/console debug:autowiring 时,出现以下错误: In FileLoader.php line 168: Class Symfony\Bundle\Fra
在使用 WebTestCase 类进行功能测试时,@Route 注释似乎不起作用。 我的 Controller 看起来像这样: $this->getCart($this->getCurrentUse
我想测试我的 cookie 是否在 Controller 中设置正确。 class RedirectControllerTest extends WebTestCase { public
我有一个使用 Symfony2 编写的 API,我正在尝试为其编写事后测试。其中一个端点使用电子邮件服务向用户发送密码重置电子邮件。我想模拟此服务,以便我可以检查是否向该服务发送了正确的信息,并防止实
我有一个 WebTestCase,它在我的应用程序中执行一些基本路由。 我想在 PHPUnit 的 setUp 方法上创建一个与我的主数据库相同的测试数据库,并将固定装置加载到其中。 我目前正在做一些
使用 Symfony2 WebTestCase,我有以下测试: $client->request('GET', '/'); $this->assertTrue($client->getResponse
我的应用程序通过 $this->get('request')->getHost() 生成一些绝对链接。问题是:当我尝试运行测试用例时,我收到以下错误消息: [exception] 500 | Inte
我正在尝试访问 Symfony2 WebTestCase 中的容器,但我得到的是 NULL。有任何想法吗?这是我的代码: $this->client = static::createClient();
我用 composer create project 开始了一个新的 symfony/skeleton 项目。 现在我想添加php单元,所以我这样做 composer require --dev te
我正在尝试设置一个简单的 WebTestCase,它使用 Symfony 4(和 "phpunit/phpunit": "^6.5")。但是,测试失败: Failed to start the ses
我正在尝试在 symfony 2.3 中测试一个表单,该表单具有一个选择输入...以及一个文件上传(enctype multipart/form-data) 选择输入如下... 这是必填字段。 有 3
我安装了 phpunit 3.6.12 和 Silex。在我的应用程序的根目录中,我有包含简单测试文件 BlogFunctionTest.php 的测试目录 assertGreaterThan(0,
我的很多测试都有很多相同的 setUp()/tearDown()正在发生的事情。将相同的代码复制并粘贴到我的每个单元测试中似乎很愚蠢。我想我想创建一个扩展 WebTestCase 的新测试类我的其他测
我是一名优秀的程序员,十分优秀!