1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pagerfanta\Tests\Adapter; |
4
|
|
|
|
5
|
|
|
use Pagerfanta\Adapter\DoctrineSelectableAdapter; |
6
|
|
|
use Doctrine\Common\Collections\Selectable; |
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Doctrine\Common\Collections\Criteria; |
9
|
|
|
use Doctrine\Common\Version; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class DoctrineSelectableAdapterTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private $selectable; |
15
|
|
|
private $criteria; |
16
|
|
|
/** |
17
|
|
|
* @var DoctrineSelectableAdapter |
18
|
|
|
*/ |
19
|
|
|
private $adapter; |
20
|
|
|
|
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
if ($this->isDoctrine23OrGreaterNotAvailable()) { |
24
|
|
|
$this->markTestSkipped('This test can only be run using Doctrine >= 2.3'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->selectable = $this->createSelectableMock(); |
28
|
|
|
$this->criteria = $this->createCriteria(); |
29
|
|
|
|
30
|
|
|
$this->adapter = new DoctrineSelectableAdapter($this->selectable, $this->criteria); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function isDoctrine23OrGreaterNotAvailable() |
34
|
|
|
{ |
35
|
|
|
return version_compare(Version::VERSION, '2.3', '<'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function createSelectableMock() |
39
|
|
|
{ |
40
|
|
|
return $this->getMockBuilder('Doctrine\Common\Collections\Selectable')->getMock(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function createCriteria() |
44
|
|
|
{ |
45
|
|
|
$criteria = new Criteria(); |
46
|
|
|
$criteria->orderBy(array('username' => 'ASC')); |
47
|
|
|
$criteria->setFirstResult(2); |
48
|
|
|
$criteria->setMaxResults(3); |
49
|
|
|
|
50
|
|
|
return $criteria; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetNbResults() |
54
|
|
|
{ |
55
|
|
|
$this->criteria->setFirstResult(null); |
56
|
|
|
$this->criteria->setMaxResults(null); |
57
|
|
|
|
58
|
|
|
$collection = $this->createCollectionMock(); |
59
|
|
|
$collection |
60
|
|
|
->expects($this->any()) |
61
|
|
|
->method('count') |
62
|
|
|
->will($this->returnValue(10)); |
63
|
|
|
|
64
|
|
|
$this->selectable |
65
|
|
|
->expects($this->once()) |
66
|
|
|
->method('matching') |
67
|
|
|
->with($this->equalTo($this->criteria)) |
68
|
|
|
->will($this->returnValue($collection)); |
69
|
|
|
|
70
|
|
|
$this->assertSame(10, $this->adapter->getNbResults()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function createCollectionMock() |
74
|
|
|
{ |
75
|
|
|
return $this->getMockBuilder('Doctrine\Common\Collections\Collection')->getMock(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetSlice() |
79
|
|
|
{ |
80
|
|
|
$this->criteria->setFirstResult(10); |
81
|
|
|
$this->criteria->setMaxResults(20); |
82
|
|
|
|
83
|
|
|
$slice = new \stdClass(); |
84
|
|
|
|
85
|
|
|
$this->selectable |
86
|
|
|
->expects($this->once()) |
87
|
|
|
->method('matching') |
88
|
|
|
->with($this->equalTo($this->criteria)) |
89
|
|
|
->will($this->returnValue($slice)); |
90
|
|
|
|
91
|
|
|
$this->assertSame($slice, $this->adapter->getSlice(10, 20)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: