-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Luís Cobucci <[email protected]>
- Loading branch information
Showing
4 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace FastRoute\Cache; | ||
|
||
use FastRoute\Cache; | ||
use FastRoute\DataGenerator; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
use function is_array; | ||
|
||
/** @phpstan-import-type RouteData from DataGenerator */ | ||
final class Psr16Cache implements Cache | ||
{ | ||
public function __construct(private readonly CacheInterface $cache) | ||
{ | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function get(string $key, callable $loader): array | ||
{ | ||
$result = $this->cache->get($key); | ||
|
||
if (is_array($result)) { | ||
// @phpstan-ignore-next-line because we won´t be able to validate the array shape in a performant way | ||
return $result; | ||
} | ||
|
||
$data = $loader(); | ||
$this->cache->set($key, $data); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace FastRoute\Test\Cache; | ||
|
||
use FastRoute\Cache\Psr16Cache; | ||
use PHPUnit\Framework\Attributes as PHPUnit; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
final class Psr16CacheTest extends TestCase | ||
{ | ||
#[PHPUnit\Test] | ||
public function cacheShouldOnlyBeSetOnMiss(): void | ||
{ | ||
$data = []; | ||
|
||
$generatedData = [['GET' => ['/' => ['test', []]]], []]; | ||
|
||
$adapter = new Psr16Cache($this->createDummyCache($data)); | ||
$result = $adapter->get('test', static fn () => $generatedData); | ||
|
||
self::assertSame($generatedData, $result); | ||
self::assertSame($generatedData, $data['test']); | ||
|
||
// Try again, now with a different callback | ||
$result = $adapter->get('test', static fn () => [['POST' => ['/' => ['test', []]]], []]); | ||
|
||
self::assertSame($generatedData, $result); | ||
} | ||
|
||
/** @param array<string, mixed> $data */ | ||
private function createDummyCache(array &$data): CacheInterface | ||
{ | ||
$cache = $this->createMock(CacheInterface::class); | ||
|
||
$cache->method('get') | ||
->willReturnCallback( | ||
static function (string $key, mixed $default) use (&$data): mixed { | ||
return $data[$key] ?? $default; | ||
}, | ||
); | ||
|
||
$cache->method('set') | ||
->willReturnCallback( | ||
static function (string $key, mixed $value) use (&$data): bool { | ||
$data[$key] = $value; | ||
|
||
return true; | ||
}, | ||
); | ||
|
||
return $cache; | ||
} | ||
} |