0

我正在使用别人的源代码 ecshop 开发一个网站,这是一个电子商务网站。我想使用 PHPUnit 对我的代码进行单元测试,但遇到了问题。这是错误的样子:

C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin>phpunit --stderr wang_test.php PHPUnit 3.7.27 by Sebastian Bergmann。

时间:1.03 秒,内存:6.75Mb

有 1 个错误:

1) ShopTest::test_get_shop_name 未定义索引:ecs

C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\includes\lib_common.ph p:564 C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\includes\init.ph p:147 C: \Users\maoqiuzi\Documents\Shanglian\XinTiandi\xintiandi\admin\wang.php:10 C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\wang_test.php:10

失败!测试:1,断言:0,错误:1。

wang_test.php的源代码:

<?php
require_once("wang.php");
class ShopTest extends PHPUnit_Framework_TestCase
{
    public function test_get_shop_name()
    {
        $shop = new Wang();
        $first_row_of_shop_list = $shop->get_shop_list();
    }
}

wang.php的源代码:

<?php 
class Wang
{
    private $exchange;
        function get_shop_list()
        {
            define("IN_ECS", 1);
            require(dirname(__FILE__).'/includes/init.php');
            $this->exchange = new exchange($GLOBALS['ecs']->table('shop'), $GLOBALS['db'], 'shop_id', 'shop_name');
            $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('shop');
            $shop_list = $GLOBALS['db']->getAll($sql);
            if($shop_list != array())
                return $shop_list;
            else
                return array();
        }
}

init.php 中的代码

require(ROOT_PATH . 'includes/lib_common.php');
class ECS //line 82
{
    var $db_name = '';
    var $prefix  = 'ecs_';

    function ECS($db_name, $prefix)
    {
        $this->db_name = $db_name;
        $this->prefix  = $prefix;
    }
...
}
...
$ecs = new ECS($db_name, $prefix); // line 114
... // other initialization codes here
$_CFG = load_config(); //line 147

lib_common.php 中的代码

function load_config()
{
    $arr = array();

    $data = read_static_cache('shop_config');
    if ($data === false)
    {
        $sql = 'SELECT code, value FROM ' . $GLOBALS['ecs']->table('shop_config') . ' WHERE parent_id > 0';
        $res = $GLOBALS['db']->getAll($sql);
...
}

我已经为此工作了几天,感到非常沮丧。希望有人帮助我!谢谢!!!

4

2 回答 2

1

正如您在 PHPunit 的“如何测试 PHP 错误”相关的手册部分中看到的,error_reporting的配置方式会影响测试套件;这是你的情况。

你有(至少)三个不同的选择:

  • 修复代码以检查而不使用未定义的数组索引
  • 更改error_reporting以忽略通知(链接中的示例之一)
  • 创建(并使用)一个phpunit.xml配置文件并设置convertNoticesToExceptionsfalse
于 2013-10-14T13:18:58.657 回答
0

In init.php, if you change:

$ecs = new ECS($db_name, $prefix);

to:

$GLOBALS['ecs'] = new ECS($db_name, $prefix);

does it start to work (or at least move on to a different error message)?

What I'm thinking is that init.php is expecting it is running as global code, so isn't being explicit like that, but then PHPUnit is doing something clever such that it is not run as global code. So $ecs just ends up being treated as a local variable, not as a global.

(If it does change the error message, go through all other global code in your libraries and change them to use $GLOBALS[...] explicitly too. This is a GoodThing™ to do anyway, as it makes code clearer when other people look at it, and avoids easy-to-make mistakes when refactoring global code into functions.)

于 2013-10-14T23:59:46.630 回答