diff --git a/src/Runner/ResultCache/DefaultResultCache.php b/src/Runner/ResultCache/DefaultResultCache.php index a9e1eabb372..e7704d81013 100644 --- a/src/Runner/ResultCache/DefaultResultCache.php +++ b/src/Runner/ResultCache/DefaultResultCache.php @@ -87,6 +87,17 @@ public function time(string $id): float return $this->times[$id] ?? 0.0; } + public function mergeWith(self $other): void + { + foreach ($other->defects as $id => $defect) { + $this->defects[$id] = $defect; + } + + foreach ($other->times as $id => $time) { + $this->times[$id] = $time; + } + } + public function load(): void { if (!is_file($this->cacheFilename)) { diff --git a/tests/unit/Runner/ResultCache/DefaultResultCacheTest.php b/tests/unit/Runner/ResultCache/DefaultResultCacheTest.php index 61af997f59c..c2c4224b1d6 100644 --- a/tests/unit/Runner/ResultCache/DefaultResultCacheTest.php +++ b/tests/unit/Runner/ResultCache/DefaultResultCacheTest.php @@ -68,4 +68,32 @@ public function testCanPersistCacheToFile(): void unlink($cacheFile); } + + public function testCanBeMerged(): void + { + $cacheSourceOne = new DefaultResultCache; + $cacheSourceOne->setStatus('status.a', TestStatus::skipped()); + $cacheSourceOne->setStatus('status.b', TestStatus::incomplete()); + $cacheSourceOne->setTime('time.a', 1); + $cacheSourceOne->setTime('time.b', 2); + $cacheSourceTwo = new DefaultResultCache; + $cacheSourceTwo->setStatus('status.c', TestStatus::failure()); + $cacheSourceTwo->setTime('time.c', 4); + + $sum = new DefaultResultCache; + $sum->mergeWith($cacheSourceOne); + + $this->assertSame(TestStatus::skipped()->asString(), $sum->status('status.a')->asString()); + $this->assertSame(TestStatus::incomplete()->asString(), $sum->status('status.b')->asString()); + $this->assertNotSame(TestStatus::failure()->asString(), $sum->status('status.c')->asString()); + + $this->assertSame(1.0, $sum->time('time.a')); + $this->assertSame(2.0, $sum->time('time.b')); + $this->assertNotSame(4.0, $sum->time('time.c')); + + $sum->mergeWith($cacheSourceTwo); + + $this->assertSame(TestStatus::failure()->asString(), $sum->status('status.c')->asString()); + $this->assertSame(4.0, $sum->time('time.c')); + } }