Skip to content

Commit

Permalink
chore: add setRemoveElement and tests (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
poppoerika authored Nov 17, 2022
1 parent f9bc2dc commit 60d0f03
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.phpunit.cache
.phpunit.result.cache
.idea/
.vscode/
28 changes: 28 additions & 0 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,3 +1222,31 @@ class CacheSetFetchResponseError extends CacheSetFetchResponse
{
use ErrorBody;
}

abstract class CacheSetRemoveElementResponse extends ResponseBase
{
public function asSuccess(): CacheSetRemoveElementResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError(): CacheSetRemoveElementResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheSetRemoveElementResponseSuccess extends CacheSetRemoveElementResponse
{
}

class CacheSetRemoveElementResponseError extends CacheSetRemoveElementResponse
{
use ErrorBody;
}
6 changes: 6 additions & 0 deletions src/Cache/SimpleCacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Momento\Cache\CacheOperationTypes\CacheListRemoveValueResponse;
use Momento\Cache\CacheOperationTypes\CacheSetAddElementResponse;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheSetRemoveElementResponse;
use Momento\Cache\CacheOperationTypes\CacheSetResponse;
use Momento\Cache\CacheOperationTypes\CreateCacheResponse;
use Momento\Cache\CacheOperationTypes\DeleteCacheResponse;
Expand Down Expand Up @@ -197,4 +198,9 @@ public function setFetch(string $cacheName, string $setName): CacheSetFetchRespo
{
return $this->dataClient->setFetch($cacheName, $setName);
}

public function setRemoveElement(string $cacheName, string $setName, string $element): CacheSetRemoveElementResponse
{
return $this->dataClient->setRemoveElement($cacheName, $setName, $element);
}
}
28 changes: 28 additions & 0 deletions src/Cache/_ScsDataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Cache_client\_ListPushFrontRequest;
use Cache_client\_ListRange;
use Cache_client\_ListRemoveRequest;
use Cache_client\_SetDifferenceRequest;
use Cache_client\_SetDifferenceRequest\_Subtrahend;
use Cache_client\_SetDifferenceRequest\_Subtrahend\_Set;
use Cache_client\_SetFetchRequest;
use Cache_client\_SetRequest;
use Cache_client\_SetUnionRequest;
Expand Down Expand Up @@ -97,6 +100,9 @@
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponseHit;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponseMiss;
use Momento\Cache\CacheOperationTypes\CacheSetRemoveElementResponse;
use Momento\Cache\CacheOperationTypes\CacheSetRemoveElementResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetRemoveElementResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheSetResponse;
use Momento\Cache\CacheOperationTypes\CacheSetResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetResponseSuccess;
Expand Down Expand Up @@ -698,4 +704,26 @@ public function setFetch(string $cacheName, string $setName): CacheSetFetchRespo
}
return new CacheSetFetchResponseMiss();
}

public function setRemoveElement(string $cacheName, string $setName, string $element): CacheSetRemoveElementResponse {
try {
validateCacheName($cacheName);
validateSetName($setName);
validateElement($element);
$setRemoveElementRequest = new _SetDifferenceRequest();
$setRemoveElementRequest->setSetName($setName);
$subtrahend = new _Subtrahend();
$set = new _Set();
$set->setElements([$element]);
$subtrahend->setSet($set);
$setRemoveElementRequest->setSubtrahend($subtrahend);
$call = $this->grpcManager->client->SetDifference($setRemoveElementRequest, ["cache" => [$cacheName]], ["timeout" => $this->timeout]);
$this->processCall($call);
} catch (SdkError $e) {
return new CacheSetRemoveElementResponseError($e);
} catch (Exception $e) {
return new CacheSetRemoveElementResponseError(new UnknownError($e->getMessage()));
}
return new CacheSetRemoveElementResponseSuccess();
}
}
86 changes: 86 additions & 0 deletions tests/Cache/CacheClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1933,4 +1933,90 @@ public function testSetAddElementSetFetch_RefreshTtl()
$this->assertNotNull($response->asHit(), "Expected a hit but got: $response");
$this->assertEquals($element, $response->asHit()->valueArray()[0]);
}

public function testSetRemoveElementWithNullCacheName_ThrowsException()
{
$this->expectException(TypeError::class);
$setName = uniqid();
$element = uniqid();
$this->client->setRemoveElement(null, $setName, $element);
}

public function testSetRemoveElementWithEmptyCacheName_ThrowsException()
{
$setName = uniqid();
$element = uniqid();
$response = $this->client->setRemoveElement("", $setName, $element);
$this->assertNotNull($response->asError(), "Expected error but got: $response");
$this->assertEquals(MomentoErrorCode::INVALID_ARGUMENT_ERROR, $response->asError()->errorCode());
}

public function testSetRemoveElementWithNullSetName_ThrowsException()
{
$this->expectException(TypeError::class);
$element = uniqid();
$this->client->setRemoveElement($this->TEST_CACHE_NAME, null, $element);
}

public function testSetRemoveElementWithEmptySetName_ThrowsException()
{
$element = uniqid();
$response = $this->client->setRemoveElement($this->TEST_CACHE_NAME, "", $element);
$this->assertNotNull($response->asError(), "Expected error but got: $response");
$this->assertEquals(MomentoErrorCode::INVALID_ARGUMENT_ERROR, $response->asError()->errorCode());
}

public function testSetRemoveElementWithNullElement_ThrowsException()
{
$this->expectException(TypeError::class);
$setName = uniqid();
$this->client->setRemoveElement($this->TEST_CACHE_NAME, $setName, null);
}

public function testSetRemoveElementWithEmptyElement_ThrowsException()
{
$setName = uniqid();
$response = $this->client->setRemoveElement($this->TEST_CACHE_NAME, $setName, "");
$this->assertNotNull($response->asError(), "Expected error but got: $response");
$this->assertEquals(MomentoErrorCode::INVALID_ARGUMENT_ERROR, $response->asError()->errorCode());
}

public function testSetRemoveElement_HappyPath()
{
$setName = uniqid();
$element = uniqid();
$response = $this->client->setAddElement($this->TEST_CACHE_NAME, $setName, $element, false);
$this->assertNull($response->asError());
$this->assertNotNull($response->asSuccess(), "Expected a success but got: $response");

$response = $this->client->setRemoveElement($this->TEST_CACHE_NAME, $setName, uniqid());
$this->assertNull($response->asError());
$this->assertNotNull($response->asSuccess(), "Expected a success but got: $response");

$response = $this->client->setFetch($this->TEST_CACHE_NAME, $setName);
$this->assertNotNull($response->asHit(), "Expected a hit but got: $response");
$this->assertEquals($element, $response->asHit()->valueArray()[0]);

$response = $this->client->setRemoveElement($this->TEST_CACHE_NAME, $setName, $element);
$this->assertNull($response->asError());
$this->assertNotNull($response->asSuccess(), "Expected a success but got: $response");

$response = $this->client->setFetch($this->TEST_CACHE_NAME, $setName);
$this->assertNotNull($response->asMiss(), "Expected a miss but got: $response");
}

public function testSetRemoveElement_SetIsMissingElement_Noop()
{
$setName = uniqid();

$response = $this->client->setFetch($this->TEST_CACHE_NAME, $setName);
$this->assertNotNull($response->asMiss(), "Expected a miss but got: $response");

$response = $this->client->setRemoveElement($this->TEST_CACHE_NAME, $setName, uniqid());
$this->assertNull($response->asError());
$this->assertNotNull($response->asSuccess(), "Expected a success but got: $response");

$response = $this->client->setFetch($this->TEST_CACHE_NAME, $setName);
$this->assertNotNull($response->asMiss(), "Expected a miss but got: $response");
}
}

0 comments on commit 60d0f03

Please sign in to comment.