session_start(): Failed to read session data: user (path: ) -in phpUnit and zend-test

720 views Asked by At

We are very new with setting up test automation for out zf2 application.

We use the test classes of zend-test (AbstractHttpControllerTestCase) since it's a zend application. We run it through phpUnit inside vagrant.

Everything was going well so far until we came across the following error:

[ERR] session_start(): Failed to read session data: user (path: )
#0 [internal function]: PHPUnit_Util_ErrorHandler::handleError(2, 'session_start()...', 'app...', 140, Array)
#1 app/vendor/zendframework/zend-session/src/SessionManager.php(140): session_start()
#2 app/vendor/zendframework/zend-session/src/AbstractContainer.php(83): Zend\Session\SessionManager->start()
#3 app/module/APP/Controller/BaseController.php(175): Zend\Session\AbstractContainer->__construct('sessionForlogin...')
#4 [internal function]: APP\Controller\BaseController->APP\Controller\{closure}(Object(Zend\Mvc\MvcEvent))
#5 app/vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Object(Closure), Object(Zend\Mvc\MvcEvent))
#6 app/vendor/zendframework/zend-eventmanager/src/EventManager.php(260): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 app/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(116): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#8 app/vendor/zendframework/zend-mvc/src/DispatchListener.php(118): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#9 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#10 app/vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#11 app/vendor/zendframework/zend-eventmanager/src/EventManager.php(260): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 app/vendor/zendframework/zend-mvc/src/Application.php(340): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#13 app/vendor/zendframework/zend-test/src/PHPUnit/Controller/AbstractControllerTestCase.php(282): Zend\Mvc\Application->run()
#14 app/test2/Test2.php(38): Zend\Test\PHPUnit\Controller\AbstractControllerTestCase->dispatch('/')
#15 [internal function]: Test2->testIndexCanBeAccessed()
#16 app/vendor/phpunit/phpunit/src/Framework/TestCase.php(1062): ReflectionMethod->invokeArgs(Object(Test2), Array)
#17 app/vendor/phpunit/phpunit/src/Framework/TestCase.php(913): PHPUnit_Framework_TestCase->runTest()
#18 app/vendor/phpunit/phpunit/src/Framework/TestResult.php(686): PHPUnit_Framework_TestCase->runBare()
#19 app/vendor/phpunit/phpunit/src/Framework/TestCase.php(868): PHPUnit_Framework_TestResult->run(Object(Test2))
#20 app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(733): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#21 app/vendor/phpunit/phpunit/src/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#22 app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(517): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#23 app/vendor/phpunit/phpunit/src/TextUI/Command.php(186): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
#24 app/vendor/phpunit/phpunit/src/TextUI/Command.php(116): PHPUnit_TextUI_Command->run(Array, true)
#25 app/vendor/phpunit/phpunit/phpunit(52): PHPUnit_TextUI_Command::main()

We are using our own custom session handler, here is its read() method:

function read($session_id) {
        $memcache = new \Memcached("session");
        if (count($memcache->getServerList()) === 0) {
            $memcache->addServer(SERVER_ADDRESS, SERVER_PORT);
            $memcache->setOption(\Memcached::OPT_COMPRESSION, true);
            $memcache->setOption(\Memcached::OPT_SERVER_FAILURE_LIMIT, 3);
        }

        $session_data = $memcache->get('memc.sess.' . $session_id);
        
        if (is_null($session_data)) {
            return ''; 
        } 
        return $session_data; 
    }

The test we were trying to execute was just a simple access on the home page:

/** @test */
    public function testIndexCanBeAccessed()
    {   
           
        $this->dispatch('/');  
        $this->assertResponseStatusCode(200);
        $this->assertControllerClass('IndexController:class');
    }

However, no assert is executed and stops at dispatch due to the error above.

P.S. As far as permission goes, we only use 'sudo' when we run phpunit. A different user is set as the owner for the application folder/files.

I hope you can help us proceed with the test executions.

0

There are 0 answers