Skip to content

Commit

Permalink
Add DefaultResultCache::mergeWith API to merge multiple result cach…
Browse files Browse the repository at this point in the history
…e instances
  • Loading branch information
Slamdunk authored and sebastianbergmann committed Dec 11, 2024
1 parent 2ad0441 commit e8b3f1b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Runner/ResultCache/DefaultResultCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Runner/ResultCache/DefaultResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit e8b3f1b

Please sign in to comment.