“致命错误:无法重新声明函数()”在使用phpunit进行测试时发生

时间:2018-03-15 12:16:54

标签: php testing phpunit

我对我的电子邮件进行了一些测试。我的测试:

class myTests extends PHPunit_Framework_Testcase{
    public function testValidateInventoryNumber(){
        include('includes/functions.php');
        $con = connectToDatabase();

        $resultTest1 = validateInventoryNumber("001234", $con);
        $this->assertEquals("001234", $resultTest1['data']);
    }

    public function testValidateLabel(){
        include('includes/functions.php');

        $resultTest1 = validateLabel("Sample Label.");
        $this->assertEquals("Sample Label.", $resultTest1['data']);
    }
}

函数validateInventoryNumber()validateLabel()includes/functions.php中声明,因此我需要在两个测试中包含此文件,我是对的吗?

如果我想运行testfile,我会收到以下错误:

Fatal error: Cannot redeclare connectToDatabase() (previously declared in C:\xampp\htdocs\prototype\includes\functions.php:4) in C:\xampp\htdocs\prototype\includes\functions.php on line 10

我认为它与测试开始时的包含有关,因为如果我注释掉其中一个测试,它就能正常工作。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

感谢@Karlo Kokkak在评论中提到了解决方案。而不是include('includes/functions.php');,请使用include_once('includes/functions.php');