|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DoctrineModuleTest\Paginator\Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
9
|
|
|
use DoctrineModule\Paginator\Adapter\Selectable as SelectableAdapter; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use function range; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tests for the Selectable pagination adapter |
|
15
|
|
|
* |
|
16
|
|
|
* @link http://www.doctrine-project.org/ |
|
17
|
|
|
*/ |
|
18
|
|
|
class SelectableAdapterTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @covers \DoctrineModule\Paginator\Adapter\Selectable::getItems |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testGetItemsAtOffsetZeroWithEmptyCriteria() : void |
|
24
|
|
|
{ |
|
25
|
|
|
$selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); |
|
26
|
|
|
$adapter = new SelectableAdapter($selectable); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$me = $this; |
|
29
|
|
|
|
|
30
|
|
|
$selectable |
|
31
|
|
|
->expects($this->once()) |
|
32
|
|
|
->method('matching') |
|
33
|
|
|
->with( |
|
34
|
|
|
$this->callback( |
|
35
|
|
|
static function (Criteria $criteria) use ($me) { |
|
36
|
|
|
$me->assertEquals(0, $criteria->getFirstResult()); |
|
37
|
|
|
$me->assertEquals(10, $criteria->getMaxResults()); |
|
38
|
|
|
|
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
) |
|
42
|
|
|
) |
|
43
|
|
|
->will($this->returnValue(new ArrayCollection(range(1, 10)))); |
|
44
|
|
|
|
|
45
|
|
|
$expected = range(1, 10); |
|
46
|
|
|
$actual = $adapter->getItems(0, 10); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals($expected, $actual); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @covers \DoctrineModule\Paginator\Adapter\Selectable::getItems |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testGetItemsAtOffsetZeroWithNonEmptyCriteria() : void |
|
55
|
|
|
{ |
|
56
|
|
|
$selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); |
|
57
|
|
|
$criteria = new Criteria(Criteria::expr()->eq('foo', 'bar')); |
|
58
|
|
|
$adapter = new SelectableAdapter($selectable, $criteria); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$me = $this; |
|
61
|
|
|
|
|
62
|
|
|
$selectable->expects($this->once()) |
|
63
|
|
|
->method('matching') |
|
64
|
|
|
->with( |
|
65
|
|
|
$this->callback( |
|
66
|
|
|
static function (Criteria $innerCriteria) use ($criteria, $me) { |
|
67
|
|
|
// Criteria are cloned internally |
|
68
|
|
|
$me->assertNotEquals($innerCriteria, $criteria); |
|
69
|
|
|
$me->assertEquals(0, $innerCriteria->getFirstResult()); |
|
70
|
|
|
$me->assertEquals(10, $innerCriteria->getMaxResults()); |
|
71
|
|
|
$me->assertEquals($innerCriteria->getWhereExpression(), $criteria->getWhereExpression()); |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
) |
|
76
|
|
|
) |
|
77
|
|
|
->will($this->returnValue(new ArrayCollection(range(1, 10)))); |
|
78
|
|
|
|
|
79
|
|
|
$expected = range(1, 10); |
|
80
|
|
|
$actual = $adapter->getItems(0, 10); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals($expected, $actual); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @covers \DoctrineModule\Paginator\Adapter\Selectable::getItems |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testGetItemsAtOffsetTenWithEmptyCriteria() : void |
|
89
|
|
|
{ |
|
90
|
|
|
$selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); |
|
91
|
|
|
$adapter = new SelectableAdapter($selectable); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$me = $this; |
|
94
|
|
|
|
|
95
|
|
|
$selectable->expects($this->once()) |
|
96
|
|
|
->method('matching') |
|
97
|
|
|
->with( |
|
98
|
|
|
$this->callback( |
|
99
|
|
|
static function (Criteria $criteria) use ($me) { |
|
100
|
|
|
$me->assertEquals(10, $criteria->getFirstResult()); |
|
101
|
|
|
$me->assertEquals(10, $criteria->getMaxResults()); |
|
102
|
|
|
|
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
) |
|
106
|
|
|
) |
|
107
|
|
|
->will($this->returnValue(new ArrayCollection(range(11, 20)))); |
|
108
|
|
|
|
|
109
|
|
|
$expected = range(11, 20); |
|
110
|
|
|
$actual = $adapter->getItems(10, 10); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals($expected, $actual); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @covers \DoctrineModule\Paginator\Adapter\Selectable::getItems |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testGetItemsAtOffsetTenWithNonEmptyCriteria() : void |
|
119
|
|
|
{ |
|
120
|
|
|
$selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); |
|
121
|
|
|
$criteria = new Criteria(Criteria::expr()->eq('foo', 'bar')); |
|
122
|
|
|
$adapter = new SelectableAdapter($selectable, $criteria); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$me = $this; |
|
125
|
|
|
|
|
126
|
|
|
$selectable->expects($this->once()) |
|
127
|
|
|
->method('matching') |
|
128
|
|
|
->with( |
|
129
|
|
|
$this->callback( |
|
130
|
|
|
static function (Criteria $innerCriteria) use ($criteria, $me) { |
|
131
|
|
|
// Criteria are cloned internally |
|
132
|
|
|
$me->assertNotEquals($innerCriteria, $criteria); |
|
133
|
|
|
$me->assertEquals(10, $innerCriteria->getFirstResult()); |
|
134
|
|
|
$me->assertEquals(10, $innerCriteria->getMaxResults()); |
|
135
|
|
|
$me->assertEquals($innerCriteria->getWhereExpression(), $criteria->getWhereExpression()); |
|
136
|
|
|
|
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
) |
|
140
|
|
|
) |
|
141
|
|
|
->will($this->returnValue(new ArrayCollection(range(11, 20)))); |
|
142
|
|
|
|
|
143
|
|
|
$expected = range(11, 20); |
|
144
|
|
|
$actual = $adapter->getItems(10, 10); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertEquals($expected, $actual); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @covers \DoctrineModule\Paginator\Adapter\Selectable::count |
|
151
|
|
|
*/ |
|
152
|
|
|
public function testReturnsCorrectCount() : void |
|
153
|
|
|
{ |
|
154
|
|
|
$selectable = $this->createMock('Doctrine\Common\Collections\Selectable'); |
|
155
|
|
|
$expression = Criteria::expr()->eq('foo', 'bar'); |
|
156
|
|
|
$criteria = new Criteria($expression, ['baz' => Criteria::DESC], 10, 20); |
|
157
|
|
|
$adapter = new SelectableAdapter($selectable, $criteria); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
$selectable->expects($this->once()) |
|
160
|
|
|
->method('matching') |
|
161
|
|
|
->with( |
|
162
|
|
|
$this->callback( |
|
163
|
|
|
static function (Criteria $criteria) use ($expression) { |
|
164
|
|
|
return $criteria->getWhereExpression() === $expression |
|
165
|
|
|
&& ($criteria->getOrderings() === ['baz' => Criteria::DESC]) |
|
166
|
|
|
&& $criteria->getFirstResult() === null |
|
167
|
|
|
&& $criteria->getMaxResults() === null; |
|
168
|
|
|
} |
|
169
|
|
|
) |
|
170
|
|
|
) |
|
171
|
|
|
->will($this->returnValue(new ArrayCollection(range(1, 101)))); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertEquals(101, $adapter->count()); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertSame(10, $criteria->getFirstResult(), 'Original criteria was not modified'); |
|
176
|
|
|
$this->assertSame(20, $criteria->getMaxResults(), 'Original criteria was not modified'); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
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: