Skip to content

Commit

Permalink
Updated to 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Mar 19, 2016
1 parent 9d21e4d commit bdb38c7
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"nilportugues/repository": "^2.0",
"nilportugues/repository": "^2.1",
"nilportugues/assert": "^1.0",
"illuminate/database": "^5.2",
"illuminate/pagination": "^5.2",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnFailure="true"
stopOnIncomplete="true"
stopOnSkipped="true"
syntaxCheck="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected static function apply(Builder $where, array $filters, $boolean)
}
}

$value = (array) $value;
$value = array_shift($value);
switch ($filterName) {
case BaseFilter::GREATER_THAN_OR_EQUAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,18 @@ public function findAll(Pageable $pageable = null)
EloquentSorter::sort($query, $sort);
}

$model = $model
->take($pageable->pageSize())
->offset($pageable->pageSize() * ($pageable->pageNumber() - 1));

if (count($distinctFields = $pageable->distinctFields()->get()) > 0) {
$model = $model->distinct();
$columns = $distinctFields;
}

return new ResultPage(
$query->paginate($pageable->pageSize(), $columns, 'page', $pageable->pageNumber())->items(),
$query->paginate()->total(),
$model->get($columns)->toArray(),
$model->count(),
$pageable->pageNumber(),
ceil($query->paginate()->total() / $pageable->pageSize())
);
Expand All @@ -258,4 +267,48 @@ public function findAll(Pageable $pageable = null)
1
);
}

/**
* Returns all instances of the type meeting $distinctFields values.
*
* @param Fields $distinctFields
* @param Filter|null $filter
* @param Sort|null $sort
*
* @return array
*/
public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null)
{
$model = $this->getModelInstance();
$query = $model->query();

$columns = (count($fields = $distinctFields->get()) > 0) ? $fields : ['*'];

if ($filter) {
EloquentFilter::filter($query, $filter);
}

if ($sort) {
EloquentSorter::sort($query, $sort);
}

return $query->getQuery()->distinct()->get($columns);
}

/**
* Repository data is added or removed as a whole block.
* Must work or fail and rollback any persisted/erased data.
*
* @param callable $transaction
*
* @throws \Exception
*/
public function transactional(callable $transaction)
{
try {
$transaction();
} catch (\Exception $e) {
throw $e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,96 @@ public function tearDown()
Database::dropAll();
}

public function testItPageableFiltersAndDistinct()
{
$client1 = $this->repository->find(new ClientId(1));
$client1->name = 'Homer Simpson';

$client2 = $this->repository->find(new ClientId(2));
$client2->name = 'Homer Simpson';

$client3 = $this->repository->find(new ClientId(3));
$client3->name = 'Homer Simpson';

$client4 = $this->repository->find(new ClientId(4));
$client4->name = 'Homer Simpson';

$this->repository->addAll([$client1, $client2, $client3, $client4]);

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

$result = $this->repository->findAll($pageable);

$this->assertEquals(1, count($result->content()));
}

public function testItCanRunSuccessfulTransaction()
{
$transaction = function () {
$client1 = $this->repository->find(new ClientId(1));
$client1->name = 'Homer Simpson';

$client2 = $this->repository->find(new ClientId(2));
$client2->name = 'Homer Simpson';

$this->repository->addAll([$client1, $client2]);
};

$this->repository->transactional($transaction);

for ($i = 1; $i <= 2; ++$i) {
$client = $this->repository->find(new ClientId($i));
$this->assertEquals('Homer Simpson', $client->name);
}
}

public function testItCanFailTransactionAndThrowException()
{
$transaction = function () {
$client1 = $this->repository->find(new ClientId(1));
$client1->name = 'Homer Simpson';

$client2 = $this->repository->find(new ClientId(2));
$client2->name = 'Homer Simpson';

$this->repository->addAll([$client1, $client2]);
throw new \Exception('Just because');
};

$this->setExpectedException(Exception::class);
$this->repository->transactional($transaction);
}

public function testItFindByDistinct()
{
$client1 = $this->repository->find(new ClientId(1));
$client1->name = 'Homer Simpson';

$client2 = $this->repository->find(new ClientId(2));
$client2->name = 'Homer Simpson';

$client3 = $this->repository->find(new ClientId(3));
$client3->name = 'Homer Simpson';

$client4 = $this->repository->find(new ClientId(4));
$client4->name = 'Homer Simpson';

$this->repository->addAll([$client1, $client2, $client3, $client4]);

$distinctFields = new Fields(['name']);
$filter = new Filter();
$filter->must()->notEmpty('name');

$results = $this->repository->findByDistinct(
$distinctFields,
$filter,
new Sort(['name'], new Order('DESC'))
);

$this->assertEquals(1, count($results));
}

public function testItCanUpdateAnExistingClient()
{
$expected = $this->repository->find(new ClientId(4));
Expand Down

0 comments on commit bdb38c7

Please sign in to comment.