class DefaultResultCache
@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@internal This class is not covered by the backward compatibility promise for PHPUnit
Hierarchy
- class \PHPUnit\Runner\ResultCache\DefaultResultCache implements \PHPUnit\Runner\ResultCache\ResultCache
Expanded class hierarchy of DefaultResultCache
1 file declares its use of DefaultResultCache
- Application.php in vendor/
phpunit/ phpunit/ src/ TextUI/ Application.php
File
-
vendor/
phpunit/ phpunit/ src/ Runner/ ResultCache/ DefaultResultCache.php, line 34
Namespace
PHPUnit\Runner\ResultCacheView source
final class DefaultResultCache implements ResultCache {
/**
* @var int
*/
private const VERSION = 1;
/**
* @var string
*/
private const DEFAULT_RESULT_CACHE_FILENAME = '.phpunit.result.cache';
private readonly string $cacheFilename;
/**
* @psalm-var array<string, TestStatus>
*/
private array $defects = [];
/**
* @psalm-var array<string, float>
*/
private array $times = [];
public function __construct(?string $filepath = null) {
if ($filepath !== null && is_dir($filepath)) {
$filepath .= DIRECTORY_SEPARATOR . self::DEFAULT_RESULT_CACHE_FILENAME;
}
$this->cacheFilename = $filepath ?? $_ENV['PHPUNIT_RESULT_CACHE'] ?? self::DEFAULT_RESULT_CACHE_FILENAME;
}
public function setStatus(string $id, TestStatus $status) : void {
if ($status->isSuccess()) {
return;
}
$this->defects[$id] = $status;
}
public function status(string $id) : TestStatus {
return $this->defects[$id] ?? TestStatus::unknown();
}
public function setTime(string $id, float $time) : void {
$this->times[$id] = $time;
}
public function time(string $id) : float {
return $this->times[$id] ?? 0.0;
}
public function load() : void {
if (!is_file($this->cacheFilename)) {
return;
}
$contents = file_get_contents($this->cacheFilename);
if ($contents === false) {
return;
}
$data = json_decode($contents, true);
if ($data === null) {
return;
}
if (!isset($data['version'])) {
return;
}
if ($data['version'] !== self::VERSION) {
return;
}
assert(isset($data['defects']) && is_array($data['defects']));
assert(isset($data['times']) && is_array($data['times']));
foreach (array_keys($data['defects']) as $test) {
$data['defects'][$test] = TestStatus::from($data['defects'][$test]);
}
$this->defects = $data['defects'];
$this->times = $data['times'];
}
/**
* @throws Exception
*/
public function persist() : void {
if (!Filesystem::createDirectory(dirname($this->cacheFilename))) {
throw new DirectoryDoesNotExistException(dirname($this->cacheFilename));
}
$data = [
'version' => self::VERSION,
'defects' => [],
'times' => $this->times,
];
foreach ($this->defects as $test => $status) {
$data['defects'][$test] = $status->asInt();
}
file_put_contents($this->cacheFilename, json_encode($data), LOCK_EX);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
DefaultResultCache::$cacheFilename | private | property | ||
DefaultResultCache::$defects | private | property | @psalm-var array<string, TestStatus> | |
DefaultResultCache::$times | private | property | @psalm-var array<string, float> | |
DefaultResultCache::DEFAULT_RESULT_CACHE_FILENAME | private | constant | ||
DefaultResultCache::load | public | function | Overrides ResultCache::load | |
DefaultResultCache::persist | public | function | Overrides ResultCache::persist | |
DefaultResultCache::setStatus | public | function | Overrides ResultCache::setStatus | |
DefaultResultCache::setTime | public | function | Overrides ResultCache::setTime | |
DefaultResultCache::status | public | function | Overrides ResultCache::status | |
DefaultResultCache::time | public | function | Overrides ResultCache::time | |
DefaultResultCache::VERSION | private | constant | ||
DefaultResultCache::__construct | public | function |