Skip to content

Commit 7b9e68d

Browse files
Merge pull request sebastianbergmann#1627 from lyft/feature-cachedcount
Add option to cache the count of tests since the strategy for the count() function is not efficient.
2 parents 2c40e43 + d05fc90 commit 7b9e68d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Framework/TestSuite.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
*/
4747
class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing, IteratorAggregate
4848
{
49+
/**
50+
* Last count of tests in this suite.
51+
*
52+
* @var integer|null
53+
*/
54+
private $cachedNumTests;
55+
4956
/**
5057
* Enable or disable the backup and restoration of the $GLOBALS array.
5158
*
@@ -404,14 +411,19 @@ public function addTestFiles($filenames)
404411
/**
405412
* Counts the number of test cases that will be run by this test.
406413
*
414+
* @Param boolean $preferCache Indicates if cache is preferred.
407415
* @return integer
408416
*/
409-
public function count()
417+
public function count($preferCache = false)
410418
{
411-
$numTests = 0;
412-
413-
foreach ($this as $test) {
414-
$numTests += count($test);
419+
if ($preferCache && $this->cachedNumTests != null) {
420+
$numTests = $this->cachedNumTests;
421+
} else {
422+
$numTests = 0;
423+
foreach ($this as $test) {
424+
$numTests += count($test);
425+
}
426+
$this->cachedNumTests = $numTests;
415427
}
416428

417429
return $numTests;

0 commit comments

Comments
 (0)