Skip to content

Commit

Permalink
Improve array of conditions support in DynamoDbQueryBuilder::where() (b…
Browse files Browse the repository at this point in the history
  • Loading branch information
mlcollins10 authored and baopham committed Feb 12, 2019
1 parent 7e316c8 commit 71aa7b5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/DynamoDbQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
// received when the method was called and pass it into the nested where.
if (is_array($column)) {
foreach ($column as $key => $value) {
return $this->where($key, '=', $value);
$this->where($key, '=', $value, $boolean);
}

return $this;
}

// Here we will make some assumptions about the operator. If only 2 values are
Expand Down
4 changes: 2 additions & 2 deletions tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,13 @@ public function testAfterKeyForQueryOperation()
$query = $this->testModel->where('id', 'id')->where('id2', '>', '-1');

$this->assertEquals('Query', $query->toDynamoDbQuery()->op);

do {
$items = $query->afterKey($afterKey)->limit(2)->all();
$paginationResult = $paginationResult->merge($items->pluck('id2'));
$afterKey = $getKey($items);
} while ($afterKey);

$this->assertCount(10, $paginationResult);
$paginationResult->each(function ($id) {
$this->assertGreaterThan('-1', $id);
Expand Down
30 changes: 26 additions & 4 deletions tests/DynamoDbNonCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ public function testAfterKeyForQueryOperation()
$this->assertEquals(10, $count);
});
}

public function testAfterKeyForScanOperation()
{
foreach (range(0, 9) as $i) {
Expand All @@ -919,18 +919,18 @@ public function testAfterKeyForScanOperation()
$assert = function (callable $getKey) {
$paginationResult = collect();
$afterKey = null;

do {
$items = $this->testModel
->afterKey($afterKey)
->limit(2)->all();
$afterKey = $getKey($items);
$paginationResult = $paginationResult->merge($items->pluck('count'));
} while ($afterKey);

$this->assertEquals(range(0, 9), $paginationResult->sort()->values()->toArray());
};

$assert(function ($items) {
return $items->lastKey();
});
Expand Down Expand Up @@ -1156,6 +1156,28 @@ public function testQueryNestedAttributes()
$this->assertEquals($item['id']['S'], $results->first()->id);
}

public function testBuilderContainsAllWhereClausesWhenGivenArrayOfConditions()
{
/** @var array $conditions */
$conditions = [
"foo" => "bar",
"bin" => "baz"
];

$builder = $this->getTestModel()->where($conditions);

/** @var array $conditionsFromBuilder */
$conditionsFromBuilder = [];

/** @var array $builderConditions */
foreach ($builder->wheres as $builderConditions) {
$conditionsFromBuilder[$builderConditions['column']] = $builderConditions['value'];
}

// Assert that the builder has the where-conditions we expect to see
$this->assertEquals($conditions, $conditionsFromBuilder);
}

protected function assertRemoveAttributes($item)
{
$this->assertNull($item->name);
Expand Down

0 comments on commit 71aa7b5

Please sign in to comment.