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
21 changes: 16 additions & 5 deletions src/Storage/EtcdStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@ public function __construct(?ClientInterface $client = null)
$this->client = $client ?? new Client();
}

public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
{
try {
return $this->client->putIf($key, $value, $previousValue ?? false, $returnNewValueOnFail);
$result = $this->client->putIf($key, $value, $previousValue ?? false, $returnNewValueOnFail);
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
throw new StorageException($e->getMessage(), $e->getCode(), $e);
}

if ($returnNewValueOnFail && $result === false) {
return null;
}

return $result;
}

public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
{
try {
return $this->client->deleteIf($key, $previousValue ?? false, $returnNewValueOnFail);
$result = $this->client->deleteIf($key, $previousValue ?? false, $returnNewValueOnFail);
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
throw new StorageException($e->getMessage(), $e->getCode(), $e);
}
}

if ($returnNewValueOnFail && $result === false) {
return null;
}

return $result;
}

public function get(string $key): ?string
{
Expand Down
12 changes: 6 additions & 6 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ interface StorageInterface
* @param string|null $previousValue The previous value to compare against. If null is provided, the comparison
* should check that the key does not exist yet.
* @param bool $returnNewValueOnFail if true the new value of the key should be returned if the operation fails
* @return bool|string true if the operation succeeded, false if it failed and `$returnNewValueOnFail` is false,
* otherwise the new value of the key
* @return bool|string|null true if the operation succeeded, false if it failed and `$returnNewValueOnFail` is
* false, otherwise the new value of the key
* @throws StorageException a known, retryable error occurred
* @throws Exception an unknown error occurred
*/
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail): bool|string;
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail): bool|string|null;

/**
* Delete if $key value matches $previous value otherwise $returnNewValueOnFail
Expand All @@ -31,12 +31,12 @@ public function putIf(string $key, string $value, ?string $previousValue, bool $
* @param string|null $previousValue The previous value to compare against. If null is provided, the comparison
* should check that the key does not exist yet.
* @param bool $returnNewValueOnFail
* @return bool|string true if the operation succeeded, false if it failed and `$returnNewValueOnFail` is false,
* otherwise the new value of the key
* @return bool|string|null true if the operation succeeded, false if it failed and `$returnNewValueOnFail` is
* false, otherwise the new value of the key
* @throws StorageException a known, retryable error occurred
* @throws Exception an unknown error occurred
*/
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string;
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null;

/**
* Get the value of a key
Expand Down
52 changes: 46 additions & 6 deletions test/Storage/EtcdStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ protected function equalToEtcdValue(?string $x): IsEqual

#[TestWith(['previous_value', false])]
#[TestWith(['previous_value', true])]
#[TestWith(['previous_value', "real-previous-value"])]
#[TestWith([null, false])]
public function testPutIf(?string $previousValue, bool|string $result): void
#[TestWith([null, true])]
public function testPutIf(?string $previousValue, bool $result): void
{
$key = 'test_key';
$value = 'test_value';
Expand All @@ -43,7 +43,28 @@ public function testPutIf(?string $previousValue, bool|string $result): void
->with($this->equalTo($key), $this->equalTo($value), $this->equalToEtcdValue($previousValue))
->willReturn($result);

$this->assertEquals($result, $this->storage->putIf($key, $value, $previousValue));
$this->assertSame($result, $this->storage->putIf($key, $value, $previousValue));
}

#[TestWith(['previous_value', false, null])]
#[TestWith(['previous_value', true, true])]
#[TestWith([null, 'real_value', 'real_value'])]
#[TestWith([null, true, true])]
public function testPutIfReturnValue(
?string $previousValue,
bool|string $etcdResponse,
bool|string|null $expected,
): void
{
$key = 'test_key';
$value = 'test_value';


$this->client->method('putIf')
->with($this->equalTo($key), $this->equalTo($value), $this->equalToEtcdValue($previousValue))
->willReturn($etcdResponse);

$this->assertSame($expected, $this->storage->putIf($key, $value, $previousValue, true));
}

public function testPutIfStorageException(): void
Expand Down Expand Up @@ -78,8 +99,8 @@ public function testPutIfOtherException(): void

#[TestWith(['previous_value', false])]
#[TestWith(['previous_value', true])]
#[TestWith(['previous_value', "real-previous-value"])]
#[TestWith([null, false])]
#[TestWith([null, true])]
public function testDeleteIf(?string $previousValue, bool|string $result): void
{
$key = 'test_key';
Expand All @@ -88,7 +109,26 @@ public function testDeleteIf(?string $previousValue, bool|string $result): void
->with($this->equalTo($key), $this->equalToEtcdValue($previousValue))
->willReturn($result);

$this->assertEquals($result, $this->storage->deleteIf($key, $previousValue));
$this->assertSame($result, $this->storage->deleteIf($key, $previousValue));
}

#[TestWith(['previous_value', false, null])]
#[TestWith(['previous_value', true, true])]
#[TestWith([null, 'real_value', 'real_value'])]
#[TestWith([null, true, true])]
public function testDeleteIfReturnValue(
?string $previousValue,
bool|string $etcdResponse,
bool|string|null $expected,
): void
{
$key = 'test_key';

$this->client->method('deleteIf')
->with($this->equalTo($key), $this->equalToEtcdValue($previousValue))
->willReturn($etcdResponse);

$this->assertSame($expected, $this->storage->deleteIf($key, $previousValue, true));
}

public function testDeleteIfStorageException(): void
Expand Down Expand Up @@ -129,7 +169,7 @@ public function testGet(?string $value)
->with($this->equalTo($key))
->willReturn($value ?? false);

$this->assertEquals($value, $this->storage->get($key));
$this->assertSame($value, $this->storage->get($key));
}

public function testGStorageException(): void
Expand Down
Loading