Symfony2:对 ArrayCollection 的测试给出“无法访问的字段"; [英] Symfony2: Test on ArrayCollection gives "Unreachable field"

查看:18
本文介绍了Symfony2:对 ArrayCollection 的测试给出“无法访问的字段";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在添加 ArrayCollection 成员的表单功能测试中,有以下语句:

<块引用>

$form['client[members][1][fname]'] = 'Benny';

字段名称已通过 DOM 检查器验证.

这一行的控制台输出是:

InvalidArgumentException:无法访问的字段成员"G:DocumentsworkspacesymvendorsymfonysymfonysrcSymfonyComponentDomCrawlerForm.php:459G:DocumentsworkspacesymvendorsymfonysymfonysrcSymfonyComponentDomCrawlerForm.php:496G:DocumentsworkspacesymvendorsymfonysymfonysrcSymfonyComponentDomCrawlerForm.php:319G:DocumentsworkspacesymsrcManaClientBundleTestsControllerClientControllerTest.php:57

应该使用什么方法来测试添加 ArrayCollection 成员?

按要求编辑(n.b.,跟随重定向已开启):

<块引用>

//触发添加家庭成员表单的链接$link = $crawler->selectLink('添加家庭成员')->link();$crawler = $client->click($link);$form = $crawler->selectButton('添加客户端')->form();$form['client[members][1][fname]'] = 'Benny';$form['client[members][1][dob]'] = "3/12/1999";$crawler = $client->submit($form);$this->assertTrue($crawler->filter('html:contains("Client View Form")')->count()>0);

解决方案

我刚遇到同样的问题,经过一番研究,我找到了一个对我有帮助的解决方案.从 Collection 表单类型中删除字段的解决方案这篇文章并不是你要找的,这篇文章是为了删除一个元素而不是添加新元素.但原理是一样的.我所做的而不是 $form->setValues() ... $form->getPhpValues() 我创建了一个数组并发布了它

在下面的示例中,表单的 configurations 字段是一个 Collection

 $submitButton = $crawler->selectButton(self::BUTTON_ADD_APPLICATION);$form = $submitButton->form();$值 = 数组('设置' =>大批('_token' =>$form['Setup[_token]']->getValue(),'名称' =>'我的新设置','配置' =>大批(0 =>大批('国家' =>'匈奴','价值' =>'3',),1 =>大批('国家' =>'GBR','价值' =>'0',),),),);$client->request($form->getMethod(), $form->getUri(), $values);

希望对您有所帮助!并感谢 sstok 的原始解决方案!

In a functional test of a form to add members of an ArrayCollection there is this statement:

$form['client[members][1][fname]'] = 'Benny';

The field name was verified with a DOM inspector.

The console output at this line is:

InvalidArgumentException: Unreachable field "members"

G:DocumentsworkspacesymvendorsymfonysymfonysrcSymfonyComponentDomCrawlerForm.php:459
G:DocumentsworkspacesymvendorsymfonysymfonysrcSymfonyComponentDomCrawlerForm.php:496
G:DocumentsworkspacesymvendorsymfonysymfonysrcSymfonyComponentDomCrawlerForm.php:319
G:DocumentsworkspacesymsrcManaClientBundleTestsControllerClientControllerTest.php:57

What method should be used to test the addition of an ArrayCollection member?

Edit as requested (n.b., follow redirects is on):

    //link to trigger adding household member form
    $link = $crawler->selectLink('Add household member')->link();
    $crawler = $client->click($link);
    $form = $crawler->selectButton('Add client')->form();
    $form['client[members][1][fname]'] = 'Benny';
    $form['client[members][1][dob]'] = "3/12/1999";
    $crawler = $client->submit($form);
    $this->assertTrue($crawler->filter('html:contains("Client View Form")')->count() > 0);

解决方案

I just had the same issue and after a bit of research I found a solution which helped me. A solution to remove a field from a Collection form type This post is not exactly what you were looking for, this one is for removing an element and not adding new ones. But the principle is the same. What I did instead of $form->setValues() ... $form->getPhpValues() that I've created an array, and POSTed that

In the example bellow, the configurations field of the form is a Collection

    $submitButton = $crawler->selectButton(self::BUTTON_ADD_APPLICATION);
    $form = $submitButton->form();
    $values = array(
        'Setup' => array(
            '_token' => $form['Setup[_token]']->getValue(),
            'name'   => 'My New Setup',
            'configurations' => array(
                0 => array(
                    'country' => 'HUN',
                    'value'   => '3',
                ),
                1 => array(
                    'country' => 'GBR',
                    'value'   => '0',
                ),
            ),
        ),
    );

    $client->request($form->getMethod(), $form->getUri(), $values);

Hope it helps! And thanks for sstok for the original solution!

这篇关于Symfony2:对 ArrayCollection 的测试给出“无法访问的字段";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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