From 7ee79f252b3b036acfd58b111b306bdb6f18b4df Mon Sep 17 00:00:00 2001 From: "v.stepanenko" Date: Thu, 23 Jun 2016 15:26:05 +0300 Subject: [PATCH] Fix #3 --- src/Processor/PaginateProcessor.php | 8 ++--- .../Processor/PaginateProcessorTest.php | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/Processor/PaginateProcessorTest.php diff --git a/src/Processor/PaginateProcessor.php b/src/Processor/PaginateProcessor.php index 79d6642..0130fbe 100644 --- a/src/Processor/PaginateProcessor.php +++ b/src/Processor/PaginateProcessor.php @@ -2,7 +2,8 @@ namespace ViewComponents\Eloquent\Processor; -use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Query\Builder; use ViewComponents\ViewComponents\Data\Operation\OperationInterface; use ViewComponents\ViewComponents\Data\Operation\PaginateOperation; use ViewComponents\ViewComponents\Data\Processor\AbstractPaginateProcessor; @@ -10,15 +11,14 @@ class PaginateProcessor extends AbstractPaginateProcessor { /** - * @param Builder $src + * @param Builder|EloquentBuilder $src * @param OperationInterface|PaginateOperation $operation * @return mixed */ public function process($src, OperationInterface $operation) { - $src->getQuery() - ->limit($operation->getPageSize()) + $src->limit($operation->getPageSize()) ->offset($this->getOffset($operation)); return $src; } diff --git a/tests/phpunit/Processor/PaginateProcessorTest.php b/tests/phpunit/Processor/PaginateProcessorTest.php new file mode 100644 index 0000000..c681ea7 --- /dev/null +++ b/tests/phpunit/Processor/PaginateProcessorTest.php @@ -0,0 +1,32 @@ +count(); + if ($recordsQty <= 5) { + throw new \Exception("Can't test PaginateProcessor: not enough fixture data"); + } + $provider = new EloquentDataProvider(TestUser::class); + $provider->operations()->add(new PaginateOperation(1,3)); + $data = iterator_to_array($provider); + $id1 = array_first($data)->id; + self::assertEquals(3, count($data)); + + $provider = new EloquentDataProvider(DB::table((new TestUser)->getTable())); + $provider->operations()->add(new PaginateOperation(2,2)); + $data = iterator_to_array($provider); + self::assertEquals(2, count($data)); + + $id2 = array_first($data)->id; + self::assertTrue($id2 > 0 && $id2 > 0 && $id1 !== $id2); + } +}