使用连续 () 进行测试时出现的问题

我有一个类 填充用户支付统计服务 与方法:


public function fillStatisticForAllTime(): void

    {

        $firstDate = \DateTime::createFromFormat('Y-m-d H:i:s', $this->userPaymentsRepository->getTheOldestDate());

        $lastDate = (new \DateTime())->setTime(0, 0, 0);

        while ($firstDate < $lastDate) {

            $this->fillStatistic($firstDate);

            $firstDate->add(new \DateInterval('P1D'));

        }




private function fillStatistic(\DateTime $day): void

    {

        $dateFrom = $day->setTime(0, 0, 0);

        $dateTo = clone $day;

        $dateTo->setTime(23, 59, 59);


        $statisticAll = $this->userPaymentsRepository

            ->getSummaryStatistic(['dateFrom' => $dateFrom, 'dateTo' => $dateTo, 'onlyPaid' => false]);

        $this->userPaymentsStatisticRepository->insertDailyStatistic($statisticAll, $day, false);


        $statisticOnlyPaid = $this->userPaymentsRepository

            ->getSummaryStatistic(['dateFrom' => $dateFrom, 'dateTo' => $dateTo, 'onlyPaid' => true]);

        $this->userPaymentsStatisticRepository->insertDailyStatistic($statisticOnlyPaid, $day, true);

    }

}

我试着写测试:


public function testFillStatisticForAllTime(): void

{

    $dateFromFirst = (new \DateTime())->sub(new \DateInterval('P2D'))->setTime(0, 0, 0);

    $dateToFirst = (new \DateTime())->sub(new \DateInterval('P2D'))->setTime(23, 59, 59);


    $dateFromSecond = (new \DateTime())->sub(new \DateInterval('P1D'))->setTime(0, 0, 0);

    $dateToSecond = (new \DateTime())->sub(new \DateInterval('P1D'))->setTime(23, 59, 59);


    $statistic = ['addPeopleFromPlugin' => 10];

    $statisticOnlyPaid = ['addPeopleFromPlugin' => 7];


    $this->userPaymentsRepositoryMock->expects($this->once())

        ->method('getTheOldestDate')

        ->willReturn($dateFromFirst->format('Y-m-d H:i:s'));



FFIVE
浏览 167回答 1
1回答

RISEBY

这里的主要问题是你的变量是可变对象(通过引用传递)。此外,在测试后会检查模拟断言,并且某些变量已更改。修复:在使用任何日期之前克隆它(“分离”与原始变量)。public function fillStatisticForAllTime(): void{&nbsp; &nbsp; $firstDate = \DateTime::createFromFormat('Y-m-d H:i:s', $this->userPaymentsRepository->getTheOldestDate());&nbsp; &nbsp; $lastDate&nbsp; = (new \DateTime())->setTime(0, 0, 0);&nbsp; &nbsp; while ($firstDate < $lastDate) {&nbsp; &nbsp; &nbsp; &nbsp; $this->fillStatistic(clone $firstDate); // <--------------- clone date&nbsp; &nbsp; &nbsp; &nbsp; $firstDate->add(new \DateInterval('P1D'));&nbsp; &nbsp; }}private function fillStatistic(\DateTime $day): void{&nbsp; &nbsp; $dateFrom = clone $day; // <----------------------------------- clone date&nbsp; &nbsp; $dateFrom->setTime(0, 0, 0);&nbsp; &nbsp; $dateTo = clone $day;&nbsp; &nbsp; $dateTo->setTime(23, 59, 59);&nbsp; &nbsp; $statisticAll = $this->userPaymentsRepository&nbsp; &nbsp; &nbsp; &nbsp; ->getSummaryStatistic(['dateFrom' => $dateFrom, 'dateTo' => $dateTo, 'onlyPaid' => false]);&nbsp; &nbsp; $this->userPaymentsStatisticRepository->insertDailyStatistic($statisticAll, $day, false);&nbsp; &nbsp; $statisticOnlyPaid = $this->userPaymentsRepository&nbsp; &nbsp; &nbsp; &nbsp; ->getSummaryStatistic(['dateFrom' => $dateFrom, 'dateTo' => $dateTo, 'onlyPaid' => true]);&nbsp; &nbsp; $this->userPaymentsStatisticRepository->insertDailyStatistic($statisticOnlyPaid, $day, true);}顺便说一句,你的第二种方法之所以有效,是因为你创建了一个新对象(从原始变量“detact”)为了避免将来出现类似的错误,您可能需要使用不可变的日期对象。
打开App,查看更多内容
随时随地看视频慕课网APP