Can#39;t Unit Test: $_SESSION empties before each test is ran(无法进行单元测试:在运行每个测试之前 $_SESSION 清空)
问题描述
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
(
)
*/
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.
这篇关于无法进行单元测试:在运行每个测试之前 $_SESSION 清空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法进行单元测试:在运行每个测试之前 $_SESSION 清空
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01