Skip to content

Commit

Permalink
Process where boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
tswestendorp committed Sep 19, 2018
1 parent 48ecf68 commit 29964f0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,15 @@ protected function applyConditions(array $where)
{
$this->initiateModel();

parent::applyConditions($where);
foreach ($where as $field => $value) {
if (\is_array($value)) {
$value[3] = $value[3] ?? 'and';
list($field, $condition, $val, $boolean) = $value;
$this->model = $this->model->where($field, $condition, $val, $boolean);
} else {
$this->model = $this->model->where($field, '=', $value);
}
}
}

}
39 changes: 39 additions & 0 deletions tests/BaseRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,36 @@ public function testResetModelCreatesNewModel()
$this->assertNotSame($model, $repository->modelProperty());
}

/**
* @dataProvider providerTestApplyConditionsProcessesWhereBoolean
*/
public function testApplyConditionsProcessesWhereBoolean($argumentBool, string $expectationBool)
{
$repository = new RepositoryStub(
$this->application,
new \Illuminate\Support\Collection(),
$model = new ModelStub()
);

$model::setConnectionResolver($connectionResolver = m::mock(\Illuminate\Database\ConnectionResolverInterface::class));
$connectionResolver->shouldReceive('connection')->andReturn(
$connection = m::mock(\Illuminate\Database\ConnectionInterface::class)
);
$connection->shouldReceive('getQueryGrammar', 'getPostProcessor');

$reflectionMethod = new \ReflectionMethod($repository, 'applyConditions');
$reflectionMethod->setAccessible(true);
$reflectionMethod->invokeArgs($repository, [
[array_filter([$column = 'foo', $operator = 'LIKE', $value = '%bar%', $argumentBool]),],
]);

$where = $repository->modelProperty()->getQuery()->wheres[0];
$this->assertSame($column, $where['column']);
$this->assertSame($operator, $where['operator']);
$this->assertSame($value, $where['value']);
$this->assertSame($expectationBool, $where['boolean']);
}

public function providerTestConstructor(): array
{
return [
Expand All @@ -125,6 +155,15 @@ public function providerTestConstructor(): array
];
}

public function providerTestApplyConditionsProcessesWhereBoolean(): array
{
return [
[null, 'and',],
['and', 'and',],
['or', 'or',],
];
}

public function getModelMock(): m\MockInterface
{
return m::mock(\Illuminate\Database\Eloquent\Model::class);
Expand Down

0 comments on commit 29964f0

Please sign in to comment.