为什么在依赖的phpunit测试之间无法正确传递symfony DOMCrawler对象? [英] Why are symfony DOMCrawler objects not properly passed between dependent phpunit tests?

查看:67
本文介绍了为什么在依赖的phpunit测试之间无法正确传递symfony DOMCrawler对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于symfony应用程序的phpunit测试套件.在该测试文件中,我在不同的测试之间有一些依赖关系,并在依赖关系之间传递DOMCrawler对象,这样我就不必每次都导航到该对象.

I have a test suite for phpunit for my symfony application. In that test file, I have some dependencies between different tests, and pass a DOMCrawler object between the dependent so that I don't have to navigate to it each time.

但是,按照我的方法,似乎您无法使用这些传递的对象提交表单,但是可以单击它们上的链接.是否有一个原因?我的设计是否很差,如果是,我应该如何进行更改?欢迎任何反馈.我在下面附加了一些代码.

However, in taking the approach that I did, It seems that you cannot submit forms with these passed objects, but you can click on the links on them. Is there a reason for this? Is my design just poor and if so, how should I change it? Any feedback is welcome. I've attached some code below.

<?php

namespace someBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
 * blah Controller Test
 * 
 */
class BlahControllerTest extends WebTestCase
{

    private $adminUrl;

    /**
     * Constructs basic information for a audit report controller test suite
     *
     */
    public function __construct()
    {
        $this->adminUrl = '/admin/';
    }

    /**
     * Starts a test suite 
     *
     * @return Crawler
     */
    public function testAdd()
    {
        // Create a new client to browse the application
        $client = static::createClient();

        // Go to site specific admin url
        $crawler = $client->request('GET', $this->adminUrl);
        $this->assertTrue(200 === $client->getResponse()->getStatusCode());

        // do stuff here

        // goes to edit page
        $crawler = $client->request('GET', $editPage);

        return $crawler;
    }

    /**
     * Tests the edit functionality
     *
     * @param Crawler $crawler Crawler for the show view
     *
     * @depends testAdd
     */
    public function testEdit($crawler)
    {
        // Create a new client to browse the application
        $client = static::createClient();

        //Line below is included if the crawler points to the show view
        //$crawler = $client->click($crawler->selectLink('Edit')->link());

        // Fill in the form and submit it
        $form = $crawler->selectButton('Edit')->form(array(
            $foo => $bar,
        ));

        // The following line doesn't work properly if testEdit is passed the
        // edit page. However, if it is passed the show page, and the 
        // edit link above is clicked, then the form will submit fine.
        $client->submit($form);
        $crawler = $client->followRedirect();

        // more code here...
    }
}

推荐答案

原因是,正如您在扩展的WebTestCase类中所看到的,实现了拆解:

The reason is that as you can see in the WebTestCase class which you extend, a teardown is implemented:

protected function tearDown()
{
    if (null !== static::$kernel) {
        static::$kernel->shutdown();
    }
}

关闭内核会产生许多影响.一种效果就是您正在经历的.我试图跟踪一下到底发生了什么,但是我什么也没走,只是想起了一个事实,那就是一旦调用了关闭程序,客户端和爬虫就变得毫无用处了.

This shutdown of the kernel has many effects. One effect is what you are experiencing. I tried to track down what's exactly happening once, but I didn't get anywhere and just made a mental note that the client and crawler are pretty useless once the shutdown is called.

我会推荐与路易(Louis)相同的事情:独立进行测试.除了它不能与客户端一起使用之外,请考虑一下您的创建页面上出现问题的时间.实际上,您的编辑页面测试也会中断,尽管页面本身可能还不错.

I would recommend the same thing as Louis: Make your tests independend. Beside that it's not working with the client, think about the time when something breaks on your create page. In effect, your edit page tests also break, although the page itself might be ok.

Depends通常用于进一步验证对象,例如是否要更深入地测试响应.您将使用依赖测试,并从第一个测试返回响应.在这种情况下,两个测试都可以中断,因为如果您的创建页面中断了,当然您的响应内容看起来也不应该如此.

Depends is normally used to further validate objects like if you would like to test the reponse a bit more in-depth. You would use a depending test and return the response from the first one. In this case, it's also ok that both tests break, because if your create page breaks, of course your response content doesn't look like it should.

这篇关于为什么在依赖的phpunit测试之间无法正确传递symfony DOMCrawler对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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