uasort():使用Mock Objects的用户比较函数修改了数组

时间:2013-10-28 10:30:27

标签: php phpunit mockery

我正在尝试测试一个正常工作的排序函数,但似乎我无法使用模拟对象测试该函数:

这是要测试的功能:

public function getUsersSorted($users = null)
{
    if ($users === null) {
        if ($this->sortedUsers != null) {
            return $this->sortedUsers;
        }
        $this->sortedUsers = $this->getUsersSorted($this->getActiveUsers());

        return $this->sortedUsers;
    }

    $iterator = $users->getIterator();
    $that = $this;
    $iterator->uasort(
        function ($a, $b) use ($that) {
            return ($that->getPointsFor($a) > $that->getPointsFor($b)) ? -1 : 1;
        }
    );
    $users = new ArrayCollection(iterator_to_array($iterator, false));

    return $users;
}

getPointsFor函数如下所示:

    public function getPointsFor(User $user)
{
    $points = 0;
    // GameBets
    foreach ($this->getBets($user) as $bet) {
        $points += $bet->getPoints();
    }
    // Questions
    foreach ($this->getQuestions() as $q) {
        $bet = $this->getQuestionBet($q, $user);
        $points += $bet->getPoints();
    }

    return $points;
}

我的代码中没有看到任何会导致此错误消息的修改。

更新 这里是所有其他被称为函数,我没有看到任何副作用,但也许我错了:

public function getBets(User $user = null)
{
    if ($user === null) {
        return $this->bets;
    }
    $c = Criteria::create()
        ->where(Criteria::expr()->eq('user', $user));

    return $this->bets->matching($c);
}

这是我的赌注中的getPoints:

public function getPoints(){
    if(!isset($this->points)){
       // This part is never used, since the points are already set in the test
       $this->points = $this->reloadPoints();
    }
     return $this->points;
}

getQuestion和getQuestionbet也不适用于我的测试用例。

0 个答案:

没有答案