symfony2 phpunit会话无法启动

时间:2013-06-28 10:32:14

标签: unit-testing symfony phpunit symfony-2.2

我的应用程序中有$ _SESSION的奇怪问题

由于我在此不解释的不同原因,我有必要在会话中设置AppKernel.php中的环境

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{

  protected $session;

  public function initializeContainer()
  {
    parent::initializeContainer();

    $this->session = $this->container->get('session');
    $this->session->set('isTestEnv', $this->getEnvironment() == 'test');
  }

在另一个用于管理某些API请求的类中,我现在需要获取该参数

namespace Bioversity\ServerConnectionBundle\Repository;

use Symfony\Component\HttpFoundation\Session\Session;
use Bioversity\ServerConnectionBundle\Repository\ServerResponseManager;
use Bioversity\ServerConnectionBundle\Repository\ServerResponseRequestQueryManager;

class ServerRequestManager
{

  protected $wrapper= "http://url/to/the/api.php";   

  public function __construct()
  {
    if($_SESSION['_sf2_attributes']['isTestEnv'] == 'test')
    {
        $this->wrapper= "http://url/to/the/api.test.php";
    }
  }

在浏览器中一切正常,但是当我尝试运行测试时,我得到一个奇怪的错误

48) ServerConnectionBundle\Tests\Repository\TraitConnectionRepositoryTest::testGetTags
ErrorException: Notice: Undefined variable: _SESSION in ServerConnectionBundle/Repository/ServerRequestManager.php line 41

--------------- EDIT ------------

我更新了我的代码, 现在我有这样的服务     参数:         环境:%kernel.environment%

services:
    bioversity_server_connection:
        class: Bioversity\ServerConnectionBundle\Repository\ServerRequestManager
        arguments: [%env%]

在我班上我添加了

class ServerRequestManager
{    
  public function __construct($env)
  {
    print_r($env);

    //if(array_key_exists('isTestEnv', $_SESSION['_sf2_attributes']))
    //{
        //if($_SESSION['_sf2_attributes']['isTestEnv'] == 'test'){
        if($env == 'test')
        {
            $this->wrapper= "http://temp.wrapper.grinfo.net/TIP/Wrapper.test.php";
            $this->setDatabaseOntology('TEST-'.$this->getDatabaseOntology());
            $this->setDatabasePGRSecure('TEST-'.$this->getDatabasePGRSecure());
            $this->setDatabaseUsers('TEST-'.$this->getDatabaseUsers());
        }
    //}
  }

我在config.yml中导入了services.yml 但我只有这个错误

  

警告:缺少Bioversity \ ServerConnectionBundle \ Repository \ ServerRequestManager :: __ construct()的参数1,在/home/aczepod/Sites/Bioversity/src/Bioversity/SecurityBundle/Repository/ServerConnection.php中调用

现在有什么问题!?

1 个答案:

答案 0 :(得分:0)

哇哈哈,你到底在做什么。 Symfony有参数kernel.environment,它指的是当前环境!如此简单地将%kernel.environment%作为参数注入您的服务。

顺便说一下。使用PHP-CLI(运行测试)根本没有$_SESSION