无法进行单元测试:$ _SESSION在每次测试运行前都为空 [英] Can't Unit Test: $_SESSION empties before each test is ran

查看:228
本文介绍了无法进行单元测试:$ _SESSION在每次测试运行前都为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法对我的代码进行单元测试.

I can't unit test my code.

$ _ SESSION每次运行下一个测试时清除. 当我运行testStartProductSession()时,我的对象将一些数据添加到$ _SESSION变量中.但是,当我运行下一个测试方法(testSessionIdIsKept())时,$ _SESSION再次为空.

$_SESSION clears every time the next test is run. When I run testStartProductSession() my object adds some data to the $_SESSION variable. But when I run the next test method ( testSessionIdIsKept() ) the $_SESSION is empty again.

看起来像$ _SESSION成为单元测试时的局部变量.

Looks like $_SESSION becomes local variable when unit testing.

我不知道该怎么办.请检查下面的输出:

I don't know what else to do. Please check the output bellow:

// session_start() on bootrap.php;

class MC_Session_ProductTest extends PHPUnit_Framework_TestCase
{

    /**
     * @return MC_Session_Product
     */
    public function getObject()
    {
        // make getInstance() return new instance instead of singleton instance
        MC_Session_Product::$isUnityTest = true;
        $object = MC_Session_Product::getInstance();
        $object->getWsClient()->setServerListUrl(SERVER_LIST_URL);
        return $object;
    }

    /**
     * All tests pass
     * @depends testSetParam
     */
    public function testStartProductSession()
    {
        $developerId = PARAM_DEVELOPER_ID;
        $productCode = PARAM_PRODUCT_CODE;
        $productVersion = PARAM_PRODUCT_VERSION;
        $platform = PARAM_PLATAFORM;
        $deviceType = PARAM_DEVICE_TYPE;
        $locale = PARAM_LOCALE;

        try {
            $object = $this->getObject();
            $object->startSession($developerId, $productCode, $productVersion,
                                  $platform, $deviceType, $locale);
            $this->assertTrue($object->sessionStarted());
        } catch (Exception $e) {
            $this->fail('Fail to start session: ' . $object->getLastUrl());
        }

        echo "\$_SESSION in testStartProductSession(): ", print_r($_SESSION, 1);
        return $object;
    }

    /**
     * Test fails because $_SESSION is empty again
     * @depends testStartProductSession
     */
    public function testSessionIdIsKept(MC_Session_Product $lastObject)
    {
        echo "\$_SESSION in testSessionIdIsKept(): ", print_r($_SESSION, 1);
        $object = $this->getObject();
        // fails
        $this->assertTrue($lastObject->sessionStarted());
        $this->assertTrue($object->sessionStarted());
        $this->assertEquals($lastObject->getSessionId(), $object->getSessionId());
        return $object;
    }
}

/* ###### Output


$_SESSION in testStartProductSession():
Array
(
    [__MC] => Array
        (
            [MC_Session_Product] => Array
                (
                    [keyOne] => 'valueOne'
                    [sessionId] => 'someId'
                )

        )

)
$_SESSION in testSessionIdIsKept():
Array
(
)

*/

推荐答案

PHPUnit在所有测试方法之前将所有全局变量(包括$_SESSION)重置为其初始值.您可以通过将$backupGlobals实例属性覆盖为false来在测试案例中禁用此功能.在setUp()方法中无法使用.

PHPUnit resets all global variables--including $_SESSION--to their starting values before each test method. You can disable this for a test case by overriding the $backupGlobals instance property to false. This does not work from the setUp() method.

class MyTest extends PHPUnit_Framework_TestCase
{
    protected $backupGlobals = FALSE;

    // ...
}

有关更多详细信息,请参见全局变量和PHPUnit .

See Global Variables and PHPUnit for further details.

这篇关于无法进行单元测试:$ _SESSION在每次测试运行前都为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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