PHPUnit和Symfony-简单测试用例的CPU被100%咀嚼

PHPUnit和Symfony-简单测试用例的CPU被100%咀嚼,symfony,phpunit,symfony-3.4,simple-phpunit,Symfony,Phpunit,Symfony 3.4,Simple Phpunit,我刚刚加入了一个项目,我正试图让PHPUnit测试井然有序。我想可能是出了什么问题,但我不确定,所以我发布了这个问题 下面的测试需要几分钟才能失败,根据我所阅读的内容,测试应该在10秒或更短的时间内执行。此外,它是咀嚼100%的CPU。在生产环境中,测试所测试的内容以毫秒为单位执行,所以我不明白为什么需要几分钟。。。旁注,测试用例的第一行需要几分钟的时间,我在里面放了一些回音,甚至连第二行都没有 下面是测试用例 public function it_should_get_the_weight_l

我刚刚加入了一个项目,我正试图让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?

结果表明,我使用的是一个由生产数据填充的数据库,并且有查询获取数百万行,因此CPU和内存消耗

结果表明,我使用的是一个由生产数据填充的数据库,还有一些查询获取数百万行,因此cpu和内存消耗

您是否尝试在测试数据库上执行原始查询?这是否也占用了您的CPU和RAM?querybuilder是否被模拟?您是否尝试在测试数据库上执行原始查询?这是否也占用了您的CPU和RAM?querybuilder被嘲笑了吗?