Php 我能';我的代码不工作,它一直在说未定义的索引:ecs

Php 我能';我的代码不工作,它一直在说未定义的索引:ecs,php,phpunit,undefined,global,Php,Phpunit,Undefined,Global,我正在使用其他人的源代码开发一个网站,名为ecshop,一个电子商务网站。我想使用PHPUnit对代码进行单元测试,但遇到了一个问题。 这就是错误的样子: C:\Users\maoqiuzi\Documents\Shanglian\xintandi\xintandi\admin>phpunit --Sebastian Bergmann编写的stderr wang_test.php PHPUnit 3.7.27 E 时间:1.03秒,内存:6.75Mb 有1个错误: 1) ShopTest::te

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

C:\Users\maoqiuzi\Documents\Shanglian\xintandi\xintandi\admin>phpunit --Sebastian Bergmann编写的stderr wang_test.php PHPUnit 3.7.27

E

时间:1.03秒,内存:6.75Mb

有1个错误:

1) ShopTest::test\u get\u shop\u name未定义索引:ecs

C:\Users\maoqiuzi\Documents\Shanglian\xintandi\xintandi\includes\lib\u common.ph p:564 C:\Users\maoqiuzi\Documents\Shanglian\xintandi\xintandi\admin\includes\init.ph p:147 C:\Users\maoqiuzi\Documents\Shanglian\xintandi\xintandi\admin\wang.php:10 C:\Users\maoqiuzi\Documents\Shanglian\xintandi\xintandi\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();
    }
}
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);
...
}
我已经为此工作了好几天,感到非常沮丧。希望有人能帮助我!谢谢

如您所见,配置方式会影响测试套件;这是你的情况

您(至少)有三种不同的选择:

  • 修复代码以检查而不使用未定义的数组索引
  • 将更改为忽略通知(链接中的示例之一)
  • 创建(并使用)配置文件,并将
    convertNoticesToExceptions
    设置为
    false

在init.php中,如果您更改:

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

它开始工作了吗(或者至少转到另一个错误消息)

我的想法是init.php期望它以全局代码的形式运行,所以不是那样显式的,但是PHPUnit做了一些聪明的事情,使它不以全局代码的形式运行。因此,$ecs最终被视为局部变量,而不是全局变量


(如果它确实更改了错误消息,请检查库中的所有其他全局代码,并将它们更改为显式使用
$GLOBALS[…]
。这是一件好事™ 无论如何,当其他人看到它时,它会使代码更清晰,并避免在将全局代码重构为函数时容易出错。)

错误告诉您,$GLOBALS数组没有“ecs”键,可能是因为它在未运行的部分代码中被实例化。i、 e.init.php和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);
...
}
$ecs = new ECS($db_name, $prefix);
$GLOBALS['ecs'] = new ECS($db_name, $prefix);