PHPunit全局变量返回NULL

时间:2018-11-12 19:33:52

标签: php phpunit

我是php单元测试的新手,但是我可以安装它并使它工作(在Mac / MAMP系统上为PHP 7.1.2 / phpUnit 6.1.1)。但是我现在不能让它测试我的应用程序。可能是由于类的体系结构层次奇怪。

在我的autoload.php中,我像这样加载类:

/* get timing functions */
$th = new TimingHelper();

/* Get basic functions */
$basic = new basic();

/* Get App data (param: fake localhost true / false) */
$App = new App(); 

/* Start DB Connection */
$db = new db(0);

/* Get usr data  (param: fake user mode true / false, user id) */
$usr = new User();

还有更多的课程...;)

现在在用户类中,我得到了以前的类:

class User {
     /* get DB connection */
    private $db, $App, $tr, $th, $basic;

    public function __construct() {

        /* get DB connection */
        Global $db, $App, $tr, $th, $basic;
        $this->db = $db;
        $this->App = $App;
        $this->tr = $tr;
        $this->th = $th;
        $this->basic = $basic;

        /* Start timing */
        $this->th->start();
    }
}

那可能不是人们应该做的,但是它有效;)

但是现在,如果我想使用phpUnit,我听说它会忽略全局对象,所以

 var_dump($this->th);

为NULL。

是否没有办法可以在类系统中使用phpUnit?如果没有,我需要更改什么?

我试图扩展每个类,但是后来我的应用程序运行起来超级慢...

0 个答案:

没有答案
相关问题