Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/Storage/SimpleInMemoryStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Aternos\Lock\Storage;

/**
* A simple in-memory storage implementation for testing purposes.
* This is not thread-safe and should not be used in production, but can be very useful for unit testing.
*/
class SimpleInMemoryStorage implements StorageInterface
{
protected array $storage = [];

public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
{
$realValue = $this->get($key);
if ($realValue === $previousValue) {
$this->storage[$key] = $value;
return true;
} elseif ($returnNewValueOnFail) {
return $realValue;
} else {
return false;
}
}

public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
{
$realValue = $this->get($key);
if ($realValue === $previousValue) {
unset($this->storage[$key]);
return true;
} elseif ($returnNewValueOnFail) {
return $realValue;
} else {
return false;
}
}

public function get(string $key): ?string
{
return $this->storage[$key] ?? null;
}
}
104 changes: 104 additions & 0 deletions test/Storage/SimpleInMemoryStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Aternos\Lock\Test\Storage;

use Aternos\Lock\Storage\SimpleInMemoryStorage;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class SimpleInMemoryStorageTest extends TestCase
{
protected PublicSimpleInMemoryStorage $storage;

protected function setUp(): void
{
$this->storage = new PublicSimpleInMemoryStorage();
}

public function testGetNull(): void
{
$this->assertNull($this->storage->get("non-existing-key"));
}

public function testGet(): void
{
$this->storage->getStorage()["key"] = "value";
$this->assertEquals("value", $this->storage->get("key"));
}

#[TestWith(["old"])]
#[TestWith([null])]
public function testPutIfSuccess(?string $previousValue): void
{
if ($previousValue !== null) {
$this->storage->getStorage()["key"] = $previousValue;
}
$this->assertTrue($this->storage->putIf("key", "new", $previousValue));
$this->assertEquals("new", $this->storage->get("key"));
}

#[TestWith(["old", null])]
#[TestWith([null, "old"])]
#[TestWith(["old", "real"])]
public function testPutIfFail(?string $previousValue, ?string $realValue): void
{
if ($realValue !== null) {
$this->storage->getStorage()["key"] = $realValue;
}
$this->assertFalse($this->storage->putIf("key", "new", $previousValue));
$this->assertEquals($realValue, $this->storage->get("key"));
}

#[TestWith(["old", null])]
#[TestWith([null, "old"])]
public function testPutIfFailValue(?string $previousValue, ?string $realValue): void
{
if ($realValue !== null) {
$this->storage->getStorage()["key"] = $realValue;
}
$this->assertEquals($realValue, $this->storage->putIf("key", "new", $previousValue, true));
$this->assertEquals($realValue, $this->storage->get("key"));
}

#[TestWith(["old"])]
#[TestWith([null])]
public function testDeleteIfSuccess(?string $previousValue): void
{
if ($previousValue !== null) {
$this->storage->getStorage()["key"] = $previousValue;
}
$this->assertTrue($this->storage->deleteIf("key", $previousValue));
$this->assertEquals(null, $this->storage->get("key"));
}

#[TestWith(["old", null])]
#[TestWith([null, "old"])]
#[TestWith(["old", "real"])]
public function testDeleteIfFail(?string $previousValue, ?string $realValue): void
{
if ($realValue !== null) {
$this->storage->getStorage()["key"] = $realValue;
}
$this->assertFalse($this->storage->deleteIf("key", $previousValue));
$this->assertEquals($realValue, $this->storage->get("key"));
}

#[TestWith(["old", null])]
#[TestWith([null, "old"])]
public function testDeleteIfFailValue(?string $previousValue, ?string $realValue): void
{
if ($realValue !== null) {
$this->storage->getStorage()["key"] = $realValue;
}
$this->assertEquals($realValue, $this->storage->deleteIf("key", $previousValue, true));
$this->assertEquals($realValue, $this->storage->get("key"));
}
}

class PublicSimpleInMemoryStorage extends SimpleInMemoryStorage
{
public function &getStorage(): array
{
return $this->storage;
}
}
Loading