Skip to content

Commit 0e3264f

Browse files
authored
Merge pull request #18 from aternosorg/in-memory-storage
Add simple in memory storage for unit tests
2 parents f3082aa + b4ad191 commit 0e3264f

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Aternos\Lock\Storage;
4+
5+
/**
6+
* A simple in-memory storage implementation for testing purposes.
7+
* This is not thread-safe and should not be used in production, but can be very useful for unit testing.
8+
*/
9+
class SimpleInMemoryStorage implements StorageInterface
10+
{
11+
protected array $storage = [];
12+
13+
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
14+
{
15+
$realValue = $this->get($key);
16+
if ($realValue === $previousValue) {
17+
$this->storage[$key] = $value;
18+
return true;
19+
} elseif ($returnNewValueOnFail) {
20+
return $realValue;
21+
} else {
22+
return false;
23+
}
24+
}
25+
26+
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
27+
{
28+
$realValue = $this->get($key);
29+
if ($realValue === $previousValue) {
30+
unset($this->storage[$key]);
31+
return true;
32+
} elseif ($returnNewValueOnFail) {
33+
return $realValue;
34+
} else {
35+
return false;
36+
}
37+
}
38+
39+
public function get(string $key): ?string
40+
{
41+
return $this->storage[$key] ?? null;
42+
}
43+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)