Skip to content

Commit 2c433da

Browse files
committed
wip
1 parent d41da58 commit 2c433da

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/WpOrg/Api/Cache.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ public function read(string $slug): ?bool
2727

2828
$content = $this->cache->read($key);
2929

30-
// Unexpected.
30+
// Should never happen because of the age check.
3131
if ($content === false) {
3232
return null;
3333
}
3434

3535
$lines = explode("\n", $content);
3636

37-
// TODO: Ensure expected!
38-
return $lines[0] === 'closed';
37+
return match ($lines[0]) {
38+
'closed' => true,
39+
'open' => false,
40+
default => null, // Unexpected content. Treat as a cache miss.
41+
};
3942
}
4043

4144
public function write(string $slug, bool $isClosed): void

tests/Unit/WpOrg/Api/CacheTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,26 @@ function (string $slug, bool $expected, string $firstLine): void {
5757
'expired' => 999_999,
5858
]);
5959

60-
test('when unexpected content', function (): void {});
60+
test('when unexpected content', function (false|string $content): void {
61+
$composerCache = Mockery::mock(ComposerCache::class);
62+
$composerCache->expects()
63+
->getAge()
64+
->with("foo.txt")
65+
->andReturn(123);
66+
$composerCache->expects()
67+
->read()
68+
->with("foo.txt")
69+
->andReturn($content);
70+
71+
$cache = new Cache($composerCache);
72+
73+
$actual = $cache->read('foo');
74+
75+
expect($actual)->toBeNull();
76+
})->with([
77+
'missed' => false,
78+
'unexpected' => 'not-closed-nor-open',
79+
]);
6180
});
6281

6382
describe('::write()', static function (): void {

0 commit comments

Comments
 (0)