Skip to content

Commit

Permalink
Tested Pageable::distinctFields
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Mar 18, 2016
1 parent f7c9325 commit d8b5969
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Domain/Model/Repository/Contracts/Pageable.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public function filters();
* @return Fields
*/
public function fields();

/**
* @return Fields
*/
public function distinctFields();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function findAll(Pageable $pageable = null)

$results = $this->findBy($pageable->filters(), $pageable->sortings());

if (0 !== count($pageable->distinctFields()->get())) {
$results = $this->resultsWithDistinctFieldsOnly($pageable->distinctFields(), $results);
}

return new ResultPage(
array_slice($results, $pageable->offset() - $pageable->pageSize(), $pageable->pageSize()),
count($results),
Expand Down Expand Up @@ -229,6 +233,19 @@ public function findByDistinct(
) {
$results = $this->findBy($filter, $sort, $filter);

return $this->resultsWithDistinctFieldsOnly($distinctFields, $results);
}

/**
* @param Fields $distinctFields
* @param $results
*
* @return array
*
* @throws \Exception
*/
protected function resultsWithDistinctFieldsOnly(Fields $distinctFields, $results)
{
$newResults = [];
$valueHash = [];
foreach ($results as $result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,36 @@ public function testFindByDistinct()
$this->assertEquals(4, count($results));
}

public function testFindAllWithDistinct()
{
$clients = new Clients(
5,
'John Doe',
new DateTime('2014-12-11'),
3,
[
new DateTime('2014-12-16'),
new DateTime('2014-12-31'),
new DateTime('2015-03-11'),
],
25.125
);

$this->repository->add($clients);

$pageable = new Pageable(
1,
10,
null,
null,
null,
new Fields(['name'])
);

$result = $this->repository->findAll($pageable);
$this->assertEquals(4, count($result->content()));
}

public function testTransactional()
{
$clients = new Clients(
Expand Down

0 comments on commit d8b5969

Please sign in to comment.