Skip to content

Commit

Permalink
Fix toDynamoDbQuery() together with decorate()
Browse files Browse the repository at this point in the history
  • Loading branch information
baopham committed Jun 25, 2018
1 parent 01ae642 commit 04d2078
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DynamoDbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function getKeys()
}

/**
* Get the primary for the model.
* Get the primary key for the model.
*
* @return string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ public function toDynamoDbQuery(
}

$raw->query = $this->cleanUpQuery($query);

if ($this->decorator) {
call_user_func($this->decorator, $raw);
}

return $raw;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,37 @@ public function testDecorateRawQuery()
$this->assertEquals(range(1, 9), $items->pluck('count')->sort()->values()->toArray());
}

public function testToDynamoDbQueryWithDecorate()
{
$queryWithoutDecorate = $this->testModel->where('foo', 'bar');

$this->assertEquals(
[
'FilterExpression' => '#foo = :a1',
'ExpressionAttributeNames' => ['#foo' => 'foo'],
'ExpressionAttributeValues' => [':a1' => ['S' => 'bar']],
'TableName' => $this->testModel->getTable(),
],
$queryWithoutDecorate->toDynamoDbQuery()->query
);

$queryWithDecorate = $queryWithoutDecorate->clone()
->decorate(function (RawDynamoDbQuery $raw) {
$raw->query['FilterExpression'] .= ' AND extra_col = :extra_col_val';
$raw->query['ExpressionAttributeValues'][':extra_col_val'] = ['N' => 0];
});

$this->assertEquals(
[
'FilterExpression' => '#foo = :a1 AND extra_col = :extra_col_val',
'ExpressionAttributeNames' => ['#foo' => 'foo'],
'ExpressionAttributeValues' => [':a1' => ['S' => 'bar'], ':extra_col_val' => ['N' => 0]],
'TableName' => $this->testModel->getTable(),
],
$queryWithDecorate->toDynamoDbQuery()->query
);
}

private function assertUsingKeyAndFilterConditions($model)
{
foreach (range(0, 9) as $i) {
Expand Down

0 comments on commit 04d2078

Please sign in to comment.