无法在启用会话的情况下对PHP CodeIgniter应用程序进行单元测试


Unable to unit test PHP CodeIgniter application with session enabled

我在CodeIgniter3中开发了我的PHP应用程序,其中包含使用PHPUnit测试框架编写的测试用例(而不是使用内置框架)。我的测试用例运行良好,直到我在autoload.php:[$autoload['libraries'] = array('session')]中加载会话。当我这样做并运行测试用例时,会抛出以下信息:

Unable to locate the specified class: Session.php

我甚至创建了一个带有Welcome控制器的空白CI项目,该控制器只有一个添加功能。虽然我没有使用session,但当我将其添加到autoload.php中时,会出现错误。我尽可能多地在谷歌上搜索,但我找到的解决方案都不能解决我的问题。有没有遇到过这个问题的人能快速解决问题?

CITest.php

class CITest extends PHPUnit_Framework_TestCase {
  private $CI;
  public function setUp() {
    $this->CI = &get_instance();
  }
  public function testAdd() {
    $this->wel = new Welcome;
    $this->assertEquals(15, $this->wel->sum(5, 10));
  }
}

欢迎使用.php

class Welcome extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
    }
    public function index() {
        echo "index...";
    }
    public function sum($x, $y) {
        return $x + $y;
    }
}

我将Session.php文件从System/library/Session复制到System/library。测试运行时没有出现任何问题。

对我有用的是添加空构造函数,如下

public function __construct() { parent::__construct(); }

完整的类看起来像这个

class Welcome_test extends TestCase
{
    public function __construct()
    {
        parent::__construct();
    }
    public function test_index()
    {
       //your tests here
    }
}