Skip to content

Commit 71aa7b5

Browse files
mlcollins10baopham
authored andcommitted
Improve array of conditions support in DynamoDbQueryBuilder::where() (baopham#180)
1 parent 7e316c8 commit 71aa7b5

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/DynamoDbQueryBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
211211
// received when the method was called and pass it into the nested where.
212212
if (is_array($column)) {
213213
foreach ($column as $key => $value) {
214-
return $this->where($key, '=', $value);
214+
$this->where($key, '=', $value, $boolean);
215215
}
216+
217+
return $this;
216218
}
217219

218220
// Here we will make some assumptions about the operator. If only 2 values are

tests/DynamoDbCompositeModelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,13 @@ public function testAfterKeyForQueryOperation()
573573
$query = $this->testModel->where('id', 'id')->where('id2', '>', '-1');
574574

575575
$this->assertEquals('Query', $query->toDynamoDbQuery()->op);
576-
576+
577577
do {
578578
$items = $query->afterKey($afterKey)->limit(2)->all();
579579
$paginationResult = $paginationResult->merge($items->pluck('id2'));
580580
$afterKey = $getKey($items);
581581
} while ($afterKey);
582-
582+
583583
$this->assertCount(10, $paginationResult);
584584
$paginationResult->each(function ($id) {
585585
$this->assertGreaterThan('-1', $id);

tests/DynamoDbNonCompositeModelTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ public function testAfterKeyForQueryOperation()
909909
$this->assertEquals(10, $count);
910910
});
911911
}
912-
912+
913913
public function testAfterKeyForScanOperation()
914914
{
915915
foreach (range(0, 9) as $i) {
@@ -919,18 +919,18 @@ public function testAfterKeyForScanOperation()
919919
$assert = function (callable $getKey) {
920920
$paginationResult = collect();
921921
$afterKey = null;
922-
922+
923923
do {
924924
$items = $this->testModel
925925
->afterKey($afterKey)
926926
->limit(2)->all();
927927
$afterKey = $getKey($items);
928928
$paginationResult = $paginationResult->merge($items->pluck('count'));
929929
} while ($afterKey);
930-
930+
931931
$this->assertEquals(range(0, 9), $paginationResult->sort()->values()->toArray());
932932
};
933-
933+
934934
$assert(function ($items) {
935935
return $items->lastKey();
936936
});
@@ -1156,6 +1156,28 @@ public function testQueryNestedAttributes()
11561156
$this->assertEquals($item['id']['S'], $results->first()->id);
11571157
}
11581158

1159+
public function testBuilderContainsAllWhereClausesWhenGivenArrayOfConditions()
1160+
{
1161+
/** @var array $conditions */
1162+
$conditions = [
1163+
"foo" => "bar",
1164+
"bin" => "baz"
1165+
];
1166+
1167+
$builder = $this->getTestModel()->where($conditions);
1168+
1169+
/** @var array $conditionsFromBuilder */
1170+
$conditionsFromBuilder = [];
1171+
1172+
/** @var array $builderConditions */
1173+
foreach ($builder->wheres as $builderConditions) {
1174+
$conditionsFromBuilder[$builderConditions['column']] = $builderConditions['value'];
1175+
}
1176+
1177+
// Assert that the builder has the where-conditions we expect to see
1178+
$this->assertEquals($conditions, $conditionsFromBuilder);
1179+
}
1180+
11591181
protected function assertRemoveAttributes($item)
11601182
{
11611183
$this->assertNull($item->name);

0 commit comments

Comments
 (0)