Skip to content

Commit 06fa665

Browse files
committed
Merge remote-tracking branch 'kitar/make-update-item-returns-new-values' into feature/increment-decrement
2 parents 49a365e + 7a8edb5 commit 06fa665

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function deleteItem($key)
274274
* Update item.
275275
*
276276
* @param array $item
277-
* @return \Aws\Result
277+
* @return array|null
278278
*/
279279
public function updateItem($item)
280280
{
@@ -292,7 +292,7 @@ public function updateItem($item)
292292
}
293293
}
294294

295-
return $this->process('updateItem', null);
295+
return $this->process('updateItem', 'processSingleItem');
296296
}
297297

298298
/**

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ public function compileUpdates($updates)
130130
}
131131

132132
return [
133-
'UpdateExpression' => implode(' ', $expressions)
133+
'UpdateExpression' => implode(' ', $expressions),
134+
'ReturnValues' => 'UPDATED_NEW',
134135
];
135136
}
136137

src/Kitar/Dynamodb/Query/Processor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ protected function unmarshal(Result $res)
2929
}
3030
}
3131

32+
if (! empty($responseArray['Attributes'])) {
33+
$responseArray['Attributes'] = $this->marshaler->unmarshalItem($responseArray['Attributes']);
34+
}
35+
3236
return $responseArray;
3337
}
3438

@@ -46,6 +50,10 @@ public function processSingleItem(Result $awsResponse, $modelClass = null)
4650
$item->setMeta($response ?? null);
4751
return $item;
4852
}
53+
54+
if (! empty($response['Attributes'])) {
55+
return $response;
56+
}
4957
}
5058

5159
public function processMultipleItems(Result $awsResponse, $modelClass = null)

tests/Model/AuthUserProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ public function it_can_update_remember_token()
255255
]
256256
],
257257
'UpdateExpression' => 'set #1 = :1',
258+
'ReturnValues' => 'UPDATED_NEW',
258259
'ExpressionAttributeNames' => [
259260
'#1' => 'remember_token'
260261
],
@@ -263,7 +264,7 @@ public function it_can_update_remember_token()
263264
'S' => 'new_token'
264265
]
265266
]
266-
]);
267+
])->andReturn($this->sampleAwsResultEmpty());
267268
$this->setConnectionResolver($connection);
268269

269270
$provider = new AuthUserProvider($this->hasher, UserA::class);

tests/Model/ModelTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ protected function newConnectionMock()
3636
return $connection;
3737
}
3838

39+
protected function sampleAwsResultEmpty()
40+
{
41+
return new Result([
42+
'@metadata' => [
43+
'statuscode' => 200
44+
]
45+
]);
46+
}
47+
3948
/** @test */
4049
public function it_can_create_new_instance()
4150
{
@@ -446,6 +455,7 @@ public function it_can_save_existing_instance()
446455
]
447456
],
448457
'UpdateExpression' => 'set #1 = :1',
458+
'ReturnValues' => 'UPDATED_NEW',
449459
'ExpressionAttributeNames' => [
450460
'#1' => 'name'
451461
],
@@ -457,7 +467,7 @@ public function it_can_save_existing_instance()
457467
];
458468

459469
$connection = $this->newConnectionMock();
460-
$connection->shouldReceive('updateItem')->with($params);
470+
$connection->shouldReceive('updateItem')->with($params)->andReturn($this->sampleAwsResultEmpty());
461471
$this->setConnectionResolver($connection);
462472

463473
$user = (new UserA)->newFromBuilder(['partition' => 'p']);

tests/Query/BuilderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ public function it_can_process_update_item()
668668
]
669669
],
670670
'UpdateExpression' => 'set #1 = :1, #2 = :2 remove #3, #4',
671+
'ReturnValues' => 'UPDATED_NEW',
671672
'ExpressionAttributeNames' => [
672673
'#1' => 'LastPostedBy',
673674
'#2' => 'Replies',
@@ -683,6 +684,7 @@ public function it_can_process_update_item()
683684
]
684685
]
685686
];
687+
$processor = 'processSingleItem';
686688

687689
$query = $this->newQuery('Thread')
688690
->key([
@@ -697,7 +699,7 @@ public function it_can_process_update_item()
697699

698700
$this->assertEquals($method, $query['method']);
699701
$this->assertEquals($params, $query['params']);
700-
$this->assertNull($query['processor']);
702+
$this->assertEquals($processor, $query['processor']);
701703
}
702704

703705
/** @test */

0 commit comments

Comments
 (0)