我刚刚加入了一个项目,我正在尝试按顺序进行 PHPUnit 测试。我认为可能有问题,但我不确定,所以我发布了这个问题。
以下测试需要几分钟才能失败,根据我一直在阅读的内容,测试应该在 10 秒或更短的时间内执行。此外,它正在咀嚼 100% 的 CPU。测试在生产中测试的内容以毫秒为单位执行,所以我不明白为什么需要几分钟...旁注,仅在测试用例的第一行就需要几分钟,我在那里放了一些回声,它甚至没有到达第二行。
这是测试用例
public function it_should_get_the_weight_logs()
{
$this->assertEmpty($this->tracker->getWeightLog());
$this->assertEquals(0, $this->tracker->getWeightLog(0, null, '', '', '', true));
$exercise = new ExerciseTracking;
$exercise->setDate(new \DateTime);
$exercise->setMeasure('oz');
$this->entityManager->persist($exercise);
$this->entityManager->flush();
$this->assertEmpty($this->tracker->getWeightLog());
$this->assertEquals(0, $this->tracker->getWeightLog(0, null, '', '', '', true));
$exercise->setMeasure('kg');
$this->entityManager->flush();
$this->assertNotEmpty($this->tracker->getWeightLog());
$this->assertEquals(1, $this->tracker->getWeightLog(0, null, '', '', '', true));
$exercise->setMeasure('lbs');
$this->entityManager->flush();
$this->assertNotEmpty($this->tracker->getWeightLog());
$this->assertEquals(1, $this->tracker->getWeightLog(0, null, '', '', '', true));
}
这是第一行的代码
public function getWeightLog(int $limit = 0, ?int $offset = null, string $orderColumn = '', string $orderDirection = '', string $searchValue = '', bool $getCount = false)
{
$qb = $this->createQueryBuilder('et');
if ($getCount) {
$qb->select('COUNT(et.id)');
} else {
$qb->select('et');
}
$qb->where('et.measure = \'kg\' OR et.measure = \'lbs\'')
->leftJoin('et.user', 'u')
->leftJoin('et.workout', 'w')
->leftJoin('et.exercise', 'ex');
if ($limit) {
$qb->setMaxResults($limit);
}
if (!is_null($offset)) {
$qb->setFirstResult($offset);
}
if ($orderColumn) {
$qb->orderBy($orderColumn, $orderDirection);
}
if ($searchValue !== '') {
$parts = explode(' ', $searchValue);
foreach ($parts as $part) {
if (is_numeric($part)) {
$qb->andWhere('et.repsQuantity = :part OR et.value = :part OR ex.id = :part OR ex.name = :part');
$qb->setParameter('part', $part);
} else {
$qb->andWhere("et.date LIKE '%$part%' OR u.firstName LIKE '%$part%'
OR u.lastName LIKE '%$part%' OR w.name LIKE '%$part%' OR ex.name LIKE '%$part%'
OR ex.displayName LIKE '%$part%' OR et.measure LIKE '%$part%'");
}
}
}
if ($getCount) {
return $qb->getQuery()->getSingleScalarResult();
} else {
return $qb->getQuery()->getResult();
}
}
我的问题是,这个测试是否应该占用 100% 的 CPU 并消耗超过 2GB 的 RAM?