Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Support/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Cache
/**
* The cache version.
*/
private const string CACHE_VERSION = 'v3';
private const string CACHE_VERSION = 'v4';

/**
* The cache instance.
Expand Down Expand Up @@ -138,7 +138,7 @@ public function persist(string $file, array $values): void

$cache[$fileHash] = $values;

$content = '<?php return '.var_export($cache, true).';';
$content = '<?php return unserialize('.var_export(serialize($cache), true).');';

if (file_put_contents($filePath, $content) !== false) {
chmod($filePath, 0666);
Expand Down
41 changes: 41 additions & 0 deletions tests/Support/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Pest\TypeCoverage\Support\Cache;
use PHPStan\Analyser\Error;

beforeEach(function () {
$this->cache = Cache::instance();
$this->cache->flush();
});

afterEach(function () {
$this->cache->flush();
});

test('it persists and retrieves cached results', function () {
$file = __DIR__.'/../Fixtures/All.php';
$error = new Error('Test error', $file, 10);

$this->cache->persist($file, [$file, [$error], []]);

expect($this->cache->has($file))->toBeTrue()
->and($this->cache->get($file))->toBeArray()
->and($this->cache->get($file)[0])->toBe($file)
->and($this->cache->get($file)[1])->toHaveCount(1)
->and($this->cache->get($file)[1][0])->toBeInstanceOf(Error::class)
->and($this->cache->get($file)[1][0]->getMessage())->toBe('Test error');
});

test('it persists and retrieves errors containing PhpParser node metadata', function () {
$file = __DIR__.'/../Fixtures/All.php';
$nodeName = new PhpParser\Node\Name('Tests\Fixtures\All');
$error = new Error('Type mismatch', $file, 10, metadata: ['type' => $nodeName]);

$this->cache->persist($file, [$file, [$error], []]);

$cached = $this->cache->get($file);

expect($cached[1])->toHaveCount(1)
->and($cached[1][0])->toBeInstanceOf(Error::class)
->and($cached[1][0]->getMessage())->toBe('Type mismatch');
});