|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Aternos\Lock\Test\Storage; |
| 4 | + |
| 5 | +use Aternos\Lock\Storage\SimpleInMemoryStorage; |
| 6 | +use PHPUnit\Framework\Attributes\TestWith; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +class SimpleInMemoryStorageTest extends TestCase |
| 10 | +{ |
| 11 | + protected PublicSimpleInMemoryStorage $storage; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + $this->storage = new PublicSimpleInMemoryStorage(); |
| 16 | + } |
| 17 | + |
| 18 | + public function testGetNull(): void |
| 19 | + { |
| 20 | + $this->assertNull($this->storage->get("non-existing-key")); |
| 21 | + } |
| 22 | + |
| 23 | + public function testGet(): void |
| 24 | + { |
| 25 | + $this->storage->getStorage()["key"] = "value"; |
| 26 | + $this->assertEquals("value", $this->storage->get("key")); |
| 27 | + } |
| 28 | + |
| 29 | + #[TestWith(["old"])] |
| 30 | + #[TestWith([null])] |
| 31 | + public function testPutIfSuccess(?string $previousValue): void |
| 32 | + { |
| 33 | + if ($previousValue !== null) { |
| 34 | + $this->storage->getStorage()["key"] = $previousValue; |
| 35 | + } |
| 36 | + $this->assertTrue($this->storage->putIf("key", "new", $previousValue)); |
| 37 | + $this->assertEquals("new", $this->storage->get("key")); |
| 38 | + } |
| 39 | + |
| 40 | + #[TestWith(["old", null])] |
| 41 | + #[TestWith([null, "old"])] |
| 42 | + #[TestWith(["old", "real"])] |
| 43 | + public function testPutIfFail(?string $previousValue, ?string $realValue): void |
| 44 | + { |
| 45 | + if ($realValue !== null) { |
| 46 | + $this->storage->getStorage()["key"] = $realValue; |
| 47 | + } |
| 48 | + $this->assertFalse($this->storage->putIf("key", "new", $previousValue)); |
| 49 | + $this->assertEquals($realValue, $this->storage->get("key")); |
| 50 | + } |
| 51 | + |
| 52 | + #[TestWith(["old", null])] |
| 53 | + #[TestWith([null, "old"])] |
| 54 | + public function testPutIfFailValue(?string $previousValue, ?string $realValue): void |
| 55 | + { |
| 56 | + if ($realValue !== null) { |
| 57 | + $this->storage->getStorage()["key"] = $realValue; |
| 58 | + } |
| 59 | + $this->assertEquals($realValue, $this->storage->putIf("key", "new", $previousValue, true)); |
| 60 | + $this->assertEquals($realValue, $this->storage->get("key")); |
| 61 | + } |
| 62 | + |
| 63 | + #[TestWith(["old"])] |
| 64 | + #[TestWith([null])] |
| 65 | + public function testDeleteIfSuccess(?string $previousValue): void |
| 66 | + { |
| 67 | + if ($previousValue !== null) { |
| 68 | + $this->storage->getStorage()["key"] = $previousValue; |
| 69 | + } |
| 70 | + $this->assertTrue($this->storage->deleteIf("key", $previousValue)); |
| 71 | + $this->assertEquals(null, $this->storage->get("key")); |
| 72 | + } |
| 73 | + |
| 74 | + #[TestWith(["old", null])] |
| 75 | + #[TestWith([null, "old"])] |
| 76 | + #[TestWith(["old", "real"])] |
| 77 | + public function testDeleteIfFail(?string $previousValue, ?string $realValue): void |
| 78 | + { |
| 79 | + if ($realValue !== null) { |
| 80 | + $this->storage->getStorage()["key"] = $realValue; |
| 81 | + } |
| 82 | + $this->assertFalse($this->storage->deleteIf("key", $previousValue)); |
| 83 | + $this->assertEquals($realValue, $this->storage->get("key")); |
| 84 | + } |
| 85 | + |
| 86 | + #[TestWith(["old", null])] |
| 87 | + #[TestWith([null, "old"])] |
| 88 | + public function testDeleteIfFailValue(?string $previousValue, ?string $realValue): void |
| 89 | + { |
| 90 | + if ($realValue !== null) { |
| 91 | + $this->storage->getStorage()["key"] = $realValue; |
| 92 | + } |
| 93 | + $this->assertEquals($realValue, $this->storage->deleteIf("key", $previousValue, true)); |
| 94 | + $this->assertEquals($realValue, $this->storage->get("key")); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +class PublicSimpleInMemoryStorage extends SimpleInMemoryStorage |
| 99 | +{ |
| 100 | + public function &getStorage(): array |
| 101 | + { |
| 102 | + return $this->storage; |
| 103 | + } |
| 104 | +} |
0 commit comments