duanqilupinf67040 2019-04-11 21:03
浏览 19

简单的map类不会像部署时那样失败

I am working on some old php code. in dev envioronment and error_reporting turned on to E_ALL | E_STRICT i get Notice: Undefined offset: 4 in /home/pgnetdev/www/PGNet/php-includes/base_classes.inc on line 545

the offending php class is

<?php
class CMap
{
    var $m_aData;
    var $m_aKeys;
    var $m_iNumItems;
    var $m_iCurrentDatumIndex;

    function CMap()
    {
        $this->m_aData = array();
        $this->m_aKeys = array();
        $this->m_iNumItems = 0;
        $this->m_iCurrentDatumIndex = 0;
    }

    function Add($strKey, & $clDatum)
    {
        if ($this->Contains($strKey))
            return false;
        $this->m_aKeys[$strKey] = $strKey;
        $this->m_iNumItems++;
        $this->m_aData[$strKey] = & $clDatum;
        return true;
    }
    function &First()
    {
        if ($this->m_iNumItems <= 0)
            return null;
        sort($this->m_aKeys);
        $this->m_iCurrentDatumIndex = 0;
        return $this->m_aData[$this->m_aKeys[0]];
    }
    //line 545 in this method
    function &Next()
    {
        fwrite(STDERR, "+[NEXT]: {$this->m_iCurrentDatumIndex} >= {$this->m_iNumItems}
"); //not deployed
        if ($this->m_iCurrentDatumIndex >= $this->m_iNumItems)
        {
            fwrite(STDERR, "-[NEXT]: returning null
");// not deployed
            return null;
        }
        fwrite(STDERR, "-[NEXT]: returning value
"); //not deployed
        return $this->m_aData[$this->m_aKeys[++$this->m_iCurrentDatumIndex]];// line 545
    }
    function &Find($strKey)
    {
        if (array_key_exists($strKey, $this->m_aData))
            return $this->m_aData[$strKey];
        return null;
    }
    function Contains($strKey)
    {
        return array_key_exists($strKey, $this->m_aData);
    }

    function Items()
    {
        return $this->m_iNumItems;
    }
}

my tests for it are

<?php
error_reporting(E_ALL | E_STRICT);

require_once '../PGNet/php-includes/base_classes.inc';

class CMapTest extends PHPUnit_Framework_TestCase
{
    public function testItemsReturnsNumberOfItemsAdded()
    {
        $map = new CMap();
        $var = 1;
        $this->assertEquals(0, $map->Items(), 'No Items, Zero Count expected');
        $map->Add("KEY", $var);

        $this->assertEquals(1, $map->Items(), 'Item just added, should have count 1');
    }

    public function testContainsReturnsTrueIfMapContainsKey()
    {
        $map = new CMap();
        $var = 1;
        $this->assertFalse($map->Contains("KEY"), 'No Items, No KEYS');
        $map->Add("KEY", $var);

        $this->assertTrue($map->Contains("KEY"), 'Item just added, should a key');
    }

    public function testFindReturnsNullIfMapDoesntContainsKey()
    {
        $map = new CMap();
        $var = 1;
        $map->Add("KEY", $var);

        $actual = $map->Find("NO_KEY");

        $this->assertEquals(null, $actual, 'no key no value... NULL is expected here');
    }

    public function testFindReturnsValueIfMapContainsKey()
    {
        $map = new CMap();
        $var = 1;
        $map->Add("KEY", $var);

        $actual = $map->Find("KEY");

        $this->assertEquals($var, $actual, 'has key finds value... var is expected here');
    }

    public function testAddReturnsFalseIfKeyIsAlreadyAdded()
    {
        $map = new CMap();
        $var1 = 1;
        $var2 = 2;

        $willBeTrue = $map->Add("KEY", $var1);
        $willBeFalse = $map->Add("KEY", $var2);

        $this->assertTrue($willBeTrue, "first key added should always be true");
        $this->assertFalse($willBeFalse, "key already exists so you cant replace it");
    }

    public function testFirstReturnsNullWhenNoItemsAdded()
    {
        $map = new CMap();

        $actual = $map->First();

        $this->assertEquals(null, $actual, 'no items added, returns false');
    }

    public function testFirstReturnsFirstSortedValue()
    {
        $map = new CMap();
        $var1 = 1;
        $var2 = 2;
        $map->Add("B", $var2);
        $map->Add("A", $var1);

        $actual = $map->First();

        $this->assertEquals($var1, $actual, 'keys are sorted then first one is grabbed');
    }

    public function testNextReturnsNullIfOnlySingleItemAdded()
    {
        $map = new CMap();
        $var1 = 1;
        $map->Add("A", $var1);

        $actual = $map->First();

        $this->assertEquals($var1, $actual, 'keys are sorted then first one is grabbed');
        $this->assertEquals(null, $map->Next());
    }

    public function testNextReturnsNullAfterAllItemsAreIteratedThrough()
    {
        fwrite(STDERR, "+[testNextReturnsNullAfterAllItemsAreIteratedThrough]
");

        $map = new CMap();
        $var1 = 1;
        $var2 = 2;
        $map->Add("B", $var2);
        $map->Add("A", $var1);

        $actual = $map->First();
        $loopCount = 0;
        try
        {
            for($actual = $map->First(); $actual != null; $actual = $map->Next())
            {
                $this->assertNotNull($actual);
                $loopCount++;
            }
        }
        catch (Throwable $th)
        {
            fwrite(STDERR, "-[testNextReturnsNullAfterAllItemsAreIteratedThrough]:WTH
");

            $this->assertFalse(true, $th->getMessage());
        }
        $this->assertEquals(2, $loopCount, "the for loop should work");
        fwrite(STDERR, "-[testNextReturnsNullAfterAllItemsAreIteratedThrough]
");
    }
}

The last test in here is what I was really puzzled why it isn't breaking or throwing or anything. the console output for that is

$ phpunit ../PGNetTests/tests/unitTests/
PHPUnit @package_version@ by Sebastian Bergmann.

.......................................................+[NEXT]: 0 >= 1
-[NEXT]: returning value
.+[testNextReturnsNullAfterAllItemsAreIteratedThrough]
+[NEXT]: 0 >= 2
-[NEXT]: returning value
+[NEXT]: 1 >= 2
-[NEXT]: returning value
-[testNextReturnsNullAfterAllItemsAreIteratedThrough]
.... 60 / 88
..........................+[NEXT]: 0 >= 1
-[NEXT]: returning value
..

Time: 0 seconds, Memory: 4.25Mb

OK (88 tests, 326 assertions)

like I would expect to either get the same message in the output as I do in production or to at least a thrown exception or even a -[NEXT] return null but i get nothing but passing tests and I don't know why. Why???

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀