Can't Unit Test: $_SESSION empties before each test is ran

微笑、不失礼 提交于 2020-01-13 11:47:09

问题


I can't unit test my code.

$_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.

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
(
)

*/

回答1:


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;

    // ...
}

See Global Variables and PHPUnit for further details.




回答2:


Instead of making $_SESSION a dependency for your unit tests, you should parameterise the parts of the session that each function needs. Then you can mock the parameters in your unit test easily. Doing this the current way you're trying is too difficult and makes your code less testable. This will require changing your current code, but it will improve your code and make things more testable.




回答3:


I would assume there is a method which is called before every test. You can set up the cookie there. In java you have the annotation @Before.



来源:https://stackoverflow.com/questions/7639627/cant-unit-test-session-empties-before-each-test-is-ran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!