|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\Hydrator\Tests\Unit\Extension\Cryptography\Store; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey; |
| 9 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyStore; |
| 10 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\Psr16CacheStoreDecorator; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Psr\SimpleCache\CacheInterface; |
| 14 | + |
| 15 | +#[CoversClass(Psr16CacheStoreDecorator::class)] |
| 16 | +final class Psr16CacheStoreDecoratorTest extends TestCase |
| 17 | +{ |
| 18 | + public function testCurrentKeyForWithCacheHit(): void |
| 19 | + { |
| 20 | + $key = $this->createKey(); |
| 21 | + |
| 22 | + $cache = $this->createMock(CacheInterface::class); |
| 23 | + $cache->expects(self::once())->method('get')->with('subjectId:subject-1')->willReturn($key); |
| 24 | + $cache->expects(self::never())->method('set'); |
| 25 | + |
| 26 | + $innerStore = $this->createMock(CipherKeyStore::class); |
| 27 | + $innerStore->expects(self::never())->method('currentKeyFor'); |
| 28 | + |
| 29 | + $store = new Psr16CacheStoreDecorator($innerStore, $cache); |
| 30 | + |
| 31 | + self::assertSame($key, $store->currentKeyFor('subject-1')); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCurrentKeyForWithCacheMiss(): void |
| 35 | + { |
| 36 | + $key = $this->createKey(); |
| 37 | + |
| 38 | + $cache = $this->createMock(CacheInterface::class); |
| 39 | + $cache->expects(self::once())->method('get')->with('subjectId:subject-1')->willReturn(null); |
| 40 | + $cache->expects(self::once())->method('set')->with('subjectId:subject-1', $key, 42); |
| 41 | + |
| 42 | + $innerStore = $this->createMock(CipherKeyStore::class); |
| 43 | + $innerStore->expects(self::once())->method('currentKeyFor')->with('subject-1')->willReturn($key); |
| 44 | + |
| 45 | + $store = new Psr16CacheStoreDecorator($innerStore, $cache, 42); |
| 46 | + |
| 47 | + self::assertSame($key, $store->currentKeyFor('subject-1')); |
| 48 | + } |
| 49 | + |
| 50 | + public function testGetWithCacheMiss(): void |
| 51 | + { |
| 52 | + $key = $this->createKey(); |
| 53 | + |
| 54 | + $cache = $this->createMock(CacheInterface::class); |
| 55 | + $cache->expects(self::once())->method('get')->with('id:key-1')->willReturn(false); |
| 56 | + $cache->expects(self::once())->method('set')->with('id:key-1', $key, null); |
| 57 | + |
| 58 | + $innerStore = $this->createMock(CipherKeyStore::class); |
| 59 | + $innerStore->expects(self::once())->method('get')->with('key-1')->willReturn($key); |
| 60 | + |
| 61 | + $store = new Psr16CacheStoreDecorator($innerStore, $cache); |
| 62 | + |
| 63 | + self::assertSame($key, $store->get('key-1')); |
| 64 | + } |
| 65 | + |
| 66 | + public function testStoreWritesInnerStoreAndBothCacheEntries(): void |
| 67 | + { |
| 68 | + $key = $this->createKey(); |
| 69 | + |
| 70 | + $cache = $this->createMock(CacheInterface::class); |
| 71 | + $cache->expects(self::exactly(2))->method('set')->willReturnMap([ |
| 72 | + ['id:key-1', $key, 17, true], |
| 73 | + ['subjectId:subject-1', $key, 17, true], |
| 74 | + ]); |
| 75 | + |
| 76 | + $innerStore = $this->createMock(CipherKeyStore::class); |
| 77 | + $innerStore->expects(self::once())->method('store')->with($key); |
| 78 | + |
| 79 | + $store = new Psr16CacheStoreDecorator($innerStore, $cache, 17); |
| 80 | + $store->store($key); |
| 81 | + } |
| 82 | + |
| 83 | + public function testRemoveDeletesIdCacheEntry(): void |
| 84 | + { |
| 85 | + $cache = $this->createMock(CacheInterface::class); |
| 86 | + $cache->expects(self::once())->method('delete')->with('id:key-1'); |
| 87 | + |
| 88 | + $innerStore = $this->createMock(CipherKeyStore::class); |
| 89 | + $innerStore->expects(self::once())->method('remove')->with('key-1'); |
| 90 | + |
| 91 | + $store = new Psr16CacheStoreDecorator($innerStore, $cache); |
| 92 | + $store->remove('key-1'); |
| 93 | + } |
| 94 | + |
| 95 | + public function testRemoveWithSubjectIdDeletesSubjectCacheEntry(): void |
| 96 | + { |
| 97 | + $cache = $this->createMock(CacheInterface::class); |
| 98 | + $cache->expects(self::once())->method('delete')->with('subjectId:subject-1'); |
| 99 | + |
| 100 | + $innerStore = $this->createMock(CipherKeyStore::class); |
| 101 | + $innerStore->expects(self::once())->method('removeWithSubjectId')->with('subject-1'); |
| 102 | + |
| 103 | + $store = new Psr16CacheStoreDecorator($innerStore, $cache); |
| 104 | + $store->removeWithSubjectId('subject-1'); |
| 105 | + } |
| 106 | + |
| 107 | + private function createKey(): CipherKey |
| 108 | + { |
| 109 | + return new CipherKey( |
| 110 | + 'key-1', |
| 111 | + 'subject-1', |
| 112 | + 'secret', |
| 113 | + 'aes-256-gcm', |
| 114 | + new DateTimeImmutable(), |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments