Skip to content

Commit 0be47f4

Browse files
committed
Cache: Fix cache content schema Windows compatibility
1 parent b915513 commit 0be47f4

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/WpOrg/Api/Cache.php

Lines changed: 11 additions & 18 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

@@ -47,12 +40,12 @@ public function write(string $slug, bool $isClosed): void
4740
return;
4841
}
4942

50-
$content = sprintf(
51-
"%s\n%s\n%s\n",
52-
$isClosed ? 'closed' : 'open',
53-
$slug,
54-
date(DateTimeInterface::RFC3339),
55-
);
43+
$content = $isClosed ? 'closed' : 'open';
44+
$content .= PHP_EOL;
45+
$content .= $slug;
46+
$content .= PHP_EOL;
47+
$content .= date(DateTimeInterface::RFC3339);
48+
$content .= PHP_EOL;
5649

5750
$this->cache->write(
5851
$this->key($slug),

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)