2

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

这是要测试的功能:

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 也不适用于我的测试用例。

4

0 回答 0