From 90628b232ac8f2bca43dd007dfcf5292ad4ca910 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Tue, 7 Jan 2025 06:47:29 +0100 Subject: [PATCH] fix: update dynamodb items instead of replaceing them --- src/Adapter/DynamoDB/DynamoDBRepository.php | 19 ++++++++- .../DynamoDB/DynamoDBRepositoryTest.php | 42 ++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Adapter/DynamoDB/DynamoDBRepository.php b/src/Adapter/DynamoDB/DynamoDBRepository.php index 6157ba7..9c7069b 100644 --- a/src/Adapter/DynamoDB/DynamoDBRepository.php +++ b/src/Adapter/DynamoDB/DynamoDBRepository.php @@ -8,6 +8,7 @@ use AsyncAws\DynamoDb\Input\DeleteItemInput; use AsyncAws\DynamoDb\Input\GetItemInput; use AsyncAws\DynamoDb\Input\PutItemInput; +use AsyncAws\DynamoDb\Input\UpdateItemInput; use Shopware\App\SDK\Shop\ShopInterface; use Shopware\App\SDK\Shop\ShopRepositoryInterface; @@ -82,7 +83,23 @@ public function getShopFromId(string $shopId): ShopInterface|null public function updateShop(ShopInterface $shop): void { - $this->createShop($shop); + $this->client->updateItem(new UpdateItemInput([ + 'TableName' => $this->tableName, + 'Key' => [ + 'id' => ['S' => $shop->getShopId()], + ], + 'UpdateExpression' => 'SET active = :active, #u = :url, secret = :secret, clientId = :clientId, clientSecret = :clientSecret', + 'ExpressionAttributeNames' => [ + '#u' => 'url', + ], + 'ExpressionAttributeValues' => [ + ':active' => ['BOOL' => $shop->isShopActive() ? '1' : '0'], + ':url' => ['S' => $shop->getShopUrl()], + ':secret' => ['S' => $shop->getShopSecret()], + ':clientId' => ['S' => (string) $shop->getShopClientId()], + ':clientSecret' => ['S' => (string) $shop->getShopClientSecret()], + ], + ])); } public function deleteShop(string $shopId): void diff --git a/tests/Adapter/DynamoDB/DynamoDBRepositoryTest.php b/tests/Adapter/DynamoDB/DynamoDBRepositoryTest.php index dd2cf21..7ebdddb 100644 --- a/tests/Adapter/DynamoDB/DynamoDBRepositoryTest.php +++ b/tests/Adapter/DynamoDB/DynamoDBRepositoryTest.php @@ -8,6 +8,7 @@ use AsyncAws\DynamoDb\Input\DeleteItemInput; use AsyncAws\DynamoDb\Input\GetItemInput; use AsyncAws\DynamoDb\Input\PutItemInput; +use AsyncAws\DynamoDb\Input\UpdateItemInput; use AsyncAws\DynamoDb\Result\GetItemOutput; use AsyncAws\DynamoDb\ValueObject\AttributeValue; use PHPUnit\Framework\Attributes\CoversClass; @@ -37,7 +38,7 @@ public function testCreateShop(): void $client = $this->createMock(DynamoDbClient::class); $client - ->expects(static::exactly(2)) + ->expects(static::exactly(1)) ->method('putItem') ->with(static::callback(function (PutItemInput $input) { static::assertSame('tableName', $input->getTableName()); @@ -65,6 +66,45 @@ public function testCreateShop(): void $shop = new DynamoDBShop('shopId', 'shopUrl', 'shopSecret', 'shopClientId', 'shopClientSecret', true); $repository->createShop($shop); + } + + public function testUpdateShop(): void + { + $client = $this->createMock(DynamoDbClient::class); + + $client + ->expects(static::exactly(1)) + ->method('updateItem') + ->with(static::callback(function (UpdateItemInput $input) { + static::assertSame('tableName', $input->getTableName()); + + $key = $input->getKey(); + static::assertSame('shopId', $key['id']->getS()); + + static::assertSame([ + '#u' => 'url', + ], $input->getExpressionAttributeNames()); + + $attributes = $input->getExpressionAttributeValues(); + + static::assertArrayHasKey(':url', $attributes); + static::assertArrayHasKey(':secret', $attributes); + static::assertArrayHasKey(':clientId', $attributes); + static::assertArrayHasKey(':clientSecret', $attributes); + static::assertArrayHasKey(':active', $attributes); + + static::assertSame('shopUrl', $attributes[':url']->getS()); + static::assertSame('shopSecret', $attributes[':secret']->getS()); + static::assertSame('', $attributes[':clientId']->getS()); + static::assertSame('', $attributes[':clientSecret']->getS()); + static::assertSame(true, $attributes[':active']->getBool()); + + return true; + })); + + $repository = new DynamoDBRepository($client, 'tableName'); + + $shop = new DynamoDBShop('shopId', 'shopUrl', 'shopSecret', null, null, true); $repository->updateShop($shop); }