Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't Unit Test: $_SESSION empties before each test is ran
    text
    copied!<p>I can't unit test my code.</p> <p>$_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.</p> <p>Looks like $_SESSION becomes local variable when unit testing.</p> <p>I don't know what else to do. Please check the output bellow:</p> <pre><code>// 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-&gt;getWsClient()-&gt;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-&gt;getObject(); $object-&gt;startSession($developerId, $productCode, $productVersion, $platform, $deviceType, $locale); $this-&gt;assertTrue($object-&gt;sessionStarted()); } catch (Exception $e) { $this-&gt;fail('Fail to start session: ' . $object-&gt;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-&gt;getObject(); // fails $this-&gt;assertTrue($lastObject-&gt;sessionStarted()); $this-&gt;assertTrue($object-&gt;sessionStarted()); $this-&gt;assertEquals($lastObject-&gt;getSessionId(), $object-&gt;getSessionId()); return $object; } } /* ###### Output $_SESSION in testStartProductSession(): Array ( [__MC] =&gt; Array ( [MC_Session_Product] =&gt; Array ( [keyOne] =&gt; 'valueOne' [sessionId] =&gt; 'someId' ) ) ) $_SESSION in testSessionIdIsKept(): Array ( ) */ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload