Skip to content

Commit 7ee79f2

Browse files
committed
Fix #3
1 parent 1ae71a2 commit 7ee79f2

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Processor/PaginateProcessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
namespace ViewComponents\Eloquent\Processor;
44

5-
use Illuminate\Database\Eloquent\Builder;
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6+
use Illuminate\Database\Query\Builder;
67
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
78
use ViewComponents\ViewComponents\Data\Operation\PaginateOperation;
89
use ViewComponents\ViewComponents\Data\Processor\AbstractPaginateProcessor;
910

1011
class PaginateProcessor extends AbstractPaginateProcessor
1112
{
1213
/**
13-
* @param Builder $src
14+
* @param Builder|EloquentBuilder $src
1415
* @param OperationInterface|PaginateOperation $operation
1516
* @return mixed
1617
*/
1718
public function process($src, OperationInterface $operation)
1819
{
1920

20-
$src->getQuery()
21-
->limit($operation->getPageSize())
21+
$src->limit($operation->getPageSize())
2222
->offset($this->getOffset($operation));
2323
return $src;
2424
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent\Test\Processor;
4+
5+
use ViewComponents\Eloquent\EloquentDataProvider;
6+
use ViewComponents\Eloquent\Test\Mock\TestUser;
7+
use ViewComponents\ViewComponents\Data\Operation\PaginateOperation;
8+
use Illuminate\Database\Capsule\Manager as DB;
9+
10+
class PaginateProcessorTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function test()
13+
{
14+
$recordsQty = TestUser::all()->count();
15+
if ($recordsQty <= 5) {
16+
throw new \Exception("Can't test PaginateProcessor: not enough fixture data");
17+
}
18+
$provider = new EloquentDataProvider(TestUser::class);
19+
$provider->operations()->add(new PaginateOperation(1,3));
20+
$data = iterator_to_array($provider);
21+
$id1 = array_first($data)->id;
22+
self::assertEquals(3, count($data));
23+
24+
$provider = new EloquentDataProvider(DB::table((new TestUser)->getTable()));
25+
$provider->operations()->add(new PaginateOperation(2,2));
26+
$data = iterator_to_array($provider);
27+
self::assertEquals(2, count($data));
28+
29+
$id2 = array_first($data)->id;
30+
self::assertTrue($id2 > 0 && $id2 > 0 && $id1 !== $id2);
31+
}
32+
}

0 commit comments

Comments
 (0)