|
1
|
|
|
<?php |
|
2
|
|
|
namespace League\Fractal\Test\Pagination; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\ORM\Query; |
|
5
|
|
|
use League\Fractal\Pagination\DoctrinePaginatorAdapter; |
|
6
|
|
|
use Mockery; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class DoctrinePaginatorAdapterTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testPaginationAdapter() |
|
12
|
|
|
{ |
|
13
|
|
|
$total = 50; |
|
14
|
|
|
$count = 5; |
|
15
|
|
|
$perPage = 5; |
|
16
|
|
|
$currentPage = 2; |
|
17
|
|
|
$lastPage = 10; |
|
18
|
|
|
|
|
19
|
|
|
//Mock the doctrine paginator |
|
20
|
|
|
$paginator = Mockery::mock('Doctrine\ORM\Tools\Pagination\Paginator')->makePartial(); |
|
21
|
|
|
$paginator->shouldReceive('count')->andReturn($total); |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
//Mock the query that the paginator is acting on |
|
25
|
|
|
$query = Mockery::mock('Doctrine\ORM\AbstractQuery'); |
|
26
|
|
|
$query->shouldReceive('getFirstResult')->andReturn(($currentPage - 1) * $perPage); |
|
27
|
|
|
$query->shouldReceive('getMaxResults')->andReturn($perPage); |
|
28
|
|
|
$paginator->shouldReceive('getQuery')->andReturn($query); |
|
29
|
|
|
|
|
30
|
|
|
//Mock the iterator of the paginator |
|
31
|
|
|
$iterator = Mockery::mock('IteratorAggregate'); |
|
32
|
|
|
$iterator->shouldReceive('count')->andReturn($count); |
|
33
|
|
|
$paginator->shouldReceive('getIterator')->andReturn($iterator); |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$adapter = new DoctrinePaginatorAdapter($paginator, function ($page) { |
|
37
|
|
|
return 'http://example.com/foo?page='.$page; |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertInstanceOf( |
|
41
|
|
|
'League\Fractal\Pagination\PaginatorInterface', |
|
42
|
|
|
$adapter |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertSame($currentPage, $adapter->getCurrentPage()); |
|
46
|
|
|
$this->assertSame($lastPage, $adapter->getLastPage()); |
|
47
|
|
|
$this->assertSame($count, $adapter->getCount()); |
|
48
|
|
|
$this->assertSame($total, $adapter->getTotal()); |
|
49
|
|
|
$this->assertSame($perPage, $adapter->getPerPage()); |
|
50
|
|
|
$this->assertSame( |
|
51
|
|
|
'http://example.com/foo?page=1', |
|
52
|
|
|
$adapter->getUrl(1) |
|
53
|
|
|
); |
|
54
|
|
|
$this->assertSame( |
|
55
|
|
|
'http://example.com/foo?page=3', |
|
56
|
|
|
$adapter->getUrl(3) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function tearDown() |
|
61
|
|
|
{ |
|
62
|
|
|
Mockery::close(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|