Skip to content

Commit

Permalink
Merge pull request #34 from shopware/update-dynamodb-items
Browse files Browse the repository at this point in the history
fix: update dynamodb items instead of replaceing them
  • Loading branch information
shyim authored Jan 7, 2025
2 parents 93cd344 + 90628b2 commit a1008d7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/Adapter/DynamoDB/DynamoDBRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion tests/Adapter/DynamoDB/DynamoDBRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit a1008d7

Please sign in to comment.