Skip to content

Commit

Permalink
fix: update dynamodb items instead of replaceing them
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jan 7, 2025
1 parent 93cd344 commit 06e0928
Show file tree
Hide file tree
Showing 2 changed files with 52 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' => [

Check warning on line 88 in src/Adapter/DynamoDB/DynamoDBRepository.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ } public function updateShop(ShopInterface $shop): void { - $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()]]])); + $this->client->updateItem(new UpdateItemInput(['TableName' => $this->tableName, 'Key' => [], '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 {
'id' => ['S' => $shop->getShopId()],

Check warning on line 89 in src/Adapter/DynamoDB/DynamoDBRepository.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "ArrayItem": @@ @@ } public function updateShop(ShopInterface $shop): void { - $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()]]])); + $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 {

Check warning on line 89 in src/Adapter/DynamoDB/DynamoDBRepository.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ } public function updateShop(ShopInterface $shop): void { - $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()]]])); + $this->client->updateItem(new UpdateItemInput(['TableName' => $this->tableName, 'Key' => ['id' => []], '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 {
],
'UpdateExpression' => 'SET active = :active, #u = :url, secret = :secret, clientId = :clientId, clientSecret = :clientSecret',
'ExpressionAttributeNames' => [

Check warning on line 92 in src/Adapter/DynamoDB/DynamoDBRepository.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "ArrayItemRemoval": @@ @@ } public function updateShop(ShopInterface $shop): void { - $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()]]])); + $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' => [], '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 {
'#u' => 'url',
],
'ExpressionAttributeValues' => [
':active' => ['BOOL' => $shop->isShopActive() ? '1' : '0'],
':url' => ['S' => $shop->getShopUrl()],
':secret' => ['S' => $shop->getShopSecret()],
':clientId' => ['S' => (string) $shop->getShopClientId()],

Check warning on line 99 in src/Adapter/DynamoDB/DynamoDBRepository.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "CastString": @@ @@ } public function updateShop(ShopInterface $shop): void { - $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()]]])); + $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' => $shop->getShopClientId()], ':clientSecret' => ['S' => (string) $shop->getShopClientSecret()]]])); } public function deleteShop(string $shopId): void {
':clientSecret' => ['S' => (string) $shop->getShopClientSecret()],

Check warning on line 100 in src/Adapter/DynamoDB/DynamoDBRepository.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "CastString": @@ @@ } public function updateShop(ShopInterface $shop): void { - $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()]]])); + $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' => $shop->getShopClientSecret()]]])); } public function deleteShop(string $shopId): void {
],
]));
}

public function deleteShop(string $shopId): void
Expand Down
35 changes: 34 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,38 @@ 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());

$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('shopClientId', $attributes[':clientId']->getS());
static::assertSame('shopClientSecret', $attributes[':clientSecret']->getS());
static::assertSame(true, $attributes[':active']->getBool());

return true;
}));

$repository = new DynamoDBRepository($client, 'tableName');

$shop = new DynamoDBShop('shopId', 'shopUrl', 'shopSecret', 'shopClientId', 'shopClientSecret', true);
$repository->updateShop($shop);
}

Expand Down

0 comments on commit 06e0928

Please sign in to comment.