Skip to content

Commit fa2247e

Browse files
tangrufusCopilot
andauthored
Cache: Fix cache content schema Windows compatibility (#12)
Co-authored-by: Copilot <[email protected]>
1 parent b915513 commit fa2247e

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/WpOrg/Api/Cache.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@ public function read(string $slug): ?bool
2525
return null;
2626
}
2727

28-
$content = $this->cache->read($key);
28+
$content = (string) $this->cache->read($key);
2929

30-
// Should never happen because of the age check.
31-
if ($content === false) {
32-
return null;
33-
}
34-
35-
$lines = explode("\n", $content);
36-
37-
return match ($lines[0]) {
38-
'closed' => true,
39-
'open' => false,
40-
default => null, // Unexpected content. Treat as a cache miss.
30+
return match (true) {
31+
str_starts_with($content, 'closed'.PHP_EOL) => true,
32+
str_starts_with($content, 'open'.PHP_EOL) => false,
33+
default => null, // Unexpected content. Treat as a miss.
4134
};
4235
}
4336

@@ -48,10 +41,13 @@ public function write(string $slug, bool $isClosed): void
4841
}
4942

5043
$content = sprintf(
51-
"%s\n%s\n%s\n",
44+
"%s%s%s%s%s%s",
5245
$isClosed ? 'closed' : 'open',
46+
PHP_EOL,
5347
$slug,
48+
PHP_EOL,
5449
date(DateTimeInterface::RFC3339),
50+
PHP_EOL
5551
);
5652

5753
$this->cache->write(

tests/Unit/WpOrg/Api/CacheTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function (string $slug, bool $expected, string $firstLine): void {
3131
$composerCache->expects()
3232
->read()
3333
->with("{$slug}.txt")
34-
->andReturn("{$firstLine}\n{$slug}\n2006-01-02T15:04:05+07:00\n");
34+
->andReturn($firstLine.PHP_EOL.$slug.PHP_EOL.'2006-01-02T15:04:05+07:00'.PHP_EOL);
3535

3636
$cache = new Cache($composerCache);
3737

@@ -89,7 +89,7 @@ function (string $slug, bool $isClosed, string $expected): void {
8989

9090
$composerCache->shouldHaveReceived('write', [
9191
"{$slug}.txt",
92-
Mockery::on(static fn (string $actual) => str_starts_with($actual, $expected."\n")),
92+
Mockery::on(static fn (string $actual) => str_starts_with($actual, $expected.PHP_EOL.$slug.PHP_EOL)),
9393
]);
9494
})->with('slugs');
9595

0 commit comments

Comments
 (0)