1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pagerfanta\Tests\Adapter; |
4
|
|
|
|
5
|
|
|
use Pagerfanta\Adapter\DoctrineDbalSingleTableAdapter; |
6
|
|
|
|
7
|
|
|
class DoctrineDbalSingleTableAdapterTest extends DoctrineDbalTestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var DoctrineDbalSingleTableAdapter |
11
|
|
|
*/ |
12
|
|
|
private $adapter; |
13
|
|
|
|
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->adapter = new DoctrineDbalSingleTableAdapter($this->qb, 'p.id'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testGetNbResults() |
22
|
|
|
{ |
23
|
|
|
$this->doTestGetNbResults(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetNbResultsShouldWorkAfterCallingGetSlice() |
27
|
|
|
{ |
28
|
|
|
$this->adapter->getSlice(1, 10); |
29
|
|
|
|
30
|
|
|
$this->doTestGetNbResults(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function doTestGetNbResults() |
34
|
|
|
{ |
35
|
|
|
$this->assertSame(50, $this->adapter->getNbResults()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetNbResultWithNoData() |
39
|
|
|
{ |
40
|
|
|
$q = clone $this->qb; |
41
|
|
|
$q->delete('posts')->execute(); |
42
|
|
|
|
43
|
|
|
$this->assertSame(0, $this->adapter->getNbResults()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetSlice() |
47
|
|
|
{ |
48
|
|
|
$this->doTestGetSlice(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetSliceShouldWorkAfterCallingGetNbResults() |
52
|
|
|
{ |
53
|
|
|
$this->adapter->getNbResults(); |
54
|
|
|
|
55
|
|
|
$this->doTestGetSlice(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function doTestGetSlice() |
59
|
|
|
{ |
60
|
|
|
$offset = 30; |
61
|
|
|
$length = 10; |
62
|
|
|
|
63
|
|
|
$q = clone $this->qb; |
64
|
|
|
$q->setFirstResult($offset)->setMaxResults($length); |
65
|
|
|
$expectedResults = $q->execute()->fetchAll(); |
66
|
|
|
|
67
|
|
|
$results = $this->adapter->getSlice($offset, $length); |
68
|
|
|
$this->assertSame($expectedResults, $results); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @expectedException \Pagerfanta\Exception\InvalidArgumentException |
73
|
|
|
*/ |
74
|
|
|
public function testItShouldThrowAnInvalidArgumentExceptionIfTheCountFieldDoesNotHaveAlias() |
75
|
|
|
{ |
76
|
|
|
new DoctrineDbalSingleTableAdapter($this->qb, 'id'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @expectedException \Pagerfanta\Exception\InvalidArgumentException |
81
|
|
|
*/ |
82
|
|
|
public function testItShouldThrowAnInvalidArgumentExceptionIfTheQueryHasJoins() |
83
|
|
|
{ |
84
|
|
|
$this->qb->innerJoin('p', 'comments', 'c', 'c.post_id = p.id'); |
85
|
|
|
|
86
|
|
|
new DoctrineDbalSingleTableAdapter($this->qb, 'p.id'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|