twistor /
lru-cache
| 1 | <?php declare(strict_types=1); |
||||
| 2 | |||||
| 3 | namespace Tests; |
||||
| 4 | |||||
| 5 | use InvalidArgumentException; |
||||
| 6 | use PHPUnit\Framework\TestCase; |
||||
| 7 | use Twistor\LruCache; |
||||
| 8 | |||||
| 9 | class LruCacheTest extends TestCase |
||||
| 10 | { |
||||
| 11 | /** |
||||
| 12 | * @var \Twistor\LruCache |
||||
| 13 | * |
||||
| 14 | * @psalm-var \Twistor\LruCache<int, string> |
||||
| 15 | */ |
||||
| 16 | private $cache; |
||||
| 17 | |||||
| 18 | public function setUp(): void |
||||
| 19 | { |
||||
| 20 | /** @var \Twistor\LruCache<int, string> cache */ |
||||
| 21 | $this->cache = new LruCache(2); |
||||
| 22 | |||||
| 23 | $this->cache->put(1, 'first'); |
||||
| 24 | $this->cache->put(2, 'second'); |
||||
| 25 | } |
||||
| 26 | |||||
| 27 | public function testConstructorValidation(): void |
||||
| 28 | { |
||||
| 29 | $this->assertInstanceOf(LruCache::class, new LruCache(1)); |
||||
| 30 | |||||
| 31 | $this->expectException(InvalidArgumentException::class); |
||||
| 32 | |||||
| 33 | new LruCache(0); |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function testHas(): void |
||||
| 37 | { |
||||
| 38 | $this->assertTrue($this->cache->has(1)); |
||||
| 39 | $this->assertTrue($this->cache->has(2)); |
||||
| 40 | $this->assertFalse($this->cache->has(3)); |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | public function testGet(): void |
||||
| 44 | { |
||||
| 45 | $this->assertSame('first', $this->cache->get(1)); |
||||
| 46 | $this->assertSame('second', $this->cache->get(2)); |
||||
| 47 | $this->assertNull($this->cache->get(3)); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | public function testOldKeysRemoved(): void |
||||
| 51 | { |
||||
| 52 | $this->cache->put(3, 'third'); |
||||
| 53 | |||||
| 54 | $this->assertFalse($this->cache->has(1)); |
||||
| 55 | $this->assertTrue($this->cache->has(2)); |
||||
| 56 | $this->assertTrue($this->cache->has(3)); |
||||
| 57 | |||||
| 58 | $this->assertNull($this->cache->get(1)); |
||||
| 59 | $this->assertSame('second', $this->cache->get(2)); |
||||
| 60 | $this->assertSame('third', $this->cache->get(3)); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | public function testGettingExistingTouchesKey(): void |
||||
| 64 | { |
||||
| 65 | // This makes 1 a more recently used key. |
||||
| 66 | $this->assertSame('first', $this->cache->get(1)); |
||||
| 67 | |||||
| 68 | $this->cache->put(3, 'third'); |
||||
| 69 | |||||
| 70 | $this->assertSame('first', $this->cache->get(1)); |
||||
| 71 | $this->assertNull($this->cache->get(2)); |
||||
| 72 | $this->assertSame('third', $this->cache->get(3)); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | public function testSettingExistingTouchesKey(): void |
||||
| 76 | { |
||||
| 77 | // This makes 1 a more recently used key. |
||||
| 78 | $this->cache->put(1, 'first updated'); |
||||
| 79 | |||||
| 80 | // When we set third, second should be removed. |
||||
| 81 | $this->cache->put(3, 'third'); |
||||
| 82 | |||||
| 83 | $this->assertSame('first updated', $this->cache->get(1)); |
||||
| 84 | $this->assertNull($this->cache->get(2)); |
||||
| 85 | $this->assertSame('third', $this->cache->get(3)); |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | public function testGetWith(): void |
||||
| 89 | { |
||||
| 90 | $throws = function(int $key): string { |
||||
|
0 ignored issues
–
show
|
|||||
| 91 | throw new InvalidArgumentException(); |
||||
| 92 | }; |
||||
| 93 | |||||
| 94 | $this->assertSame('first', $this->cache->getWith(1, $throws)); |
||||
| 95 | $this->assertSame('second', $this->cache->getWith(2, $throws)); |
||||
| 96 | |||||
| 97 | $returnsThree = function(int $key): string { |
||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 98 | return 'third'; |
||||
| 99 | }; |
||||
| 100 | |||||
| 101 | $this->assertSame('third', $this->cache->getWith(3, $returnsThree)); |
||||
| 102 | $this->assertTrue($this->cache->has(3)); |
||||
| 103 | $this->assertSame('third', $this->cache->getWith(3, $throws)); |
||||
| 104 | } |
||||
| 105 | } |
||||
| 106 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.