|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Tools\Pagination; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\ORM\Decorator\EntityManagerDecorator; |
|
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
10
|
|
|
use Doctrine\ORM\Internal\Hydration\AbstractHydrator; |
|
11
|
|
|
use Doctrine\ORM\Query; |
|
12
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
13
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock; |
|
14
|
|
|
use Doctrine\Tests\Mocks\DriverMock; |
|
15
|
|
|
use Doctrine\Tests\OrmTestCase; |
|
16
|
|
|
|
|
17
|
|
|
class PaginatorTest extends OrmTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Connection */ |
|
20
|
|
|
private $connection; |
|
21
|
|
|
/** @var EntityManagerInterface */ |
|
22
|
|
|
private $em; |
|
23
|
|
|
/** @var AbstractHydrator */ |
|
24
|
|
|
private $hydrator; |
|
25
|
|
|
|
|
26
|
|
|
protected function setUp() : void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->connection = $this->getMockBuilder(ConnectionMock::class) |
|
|
|
|
|
|
29
|
|
|
->setConstructorArgs([[], new DriverMock()]) |
|
30
|
|
|
->setMethods(['executeQuery']) |
|
31
|
|
|
->getMock() |
|
32
|
|
|
; |
|
33
|
|
|
|
|
34
|
|
|
$this->em = $this->getMockBuilder(EntityManagerDecorator::class) |
|
|
|
|
|
|
35
|
|
|
->setConstructorArgs([$this->getTestEntityManager($this->connection)]) |
|
36
|
|
|
->setMethods(['newHydrator']) |
|
37
|
|
|
->getMock() |
|
38
|
|
|
; |
|
39
|
|
|
|
|
40
|
|
|
$this->hydrator = $this->createMock(AbstractHydrator::class); |
|
|
|
|
|
|
41
|
|
|
$this->em->method('newHydrator')->willReturn($this->hydrator); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testExtraParametersAreStrippedWhenWalkerRemovingOriginalSelectElementsIsUsed() : void |
|
45
|
|
|
{ |
|
46
|
|
|
$paramInWhere = 1; |
|
47
|
|
|
$paramInSubSelect = 2; |
|
48
|
|
|
$returnedIds = [10]; |
|
49
|
|
|
|
|
50
|
|
|
$this->hydrator->method('hydrateAll')->willReturn([$returnedIds]); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$query = new Query($this->em); |
|
53
|
|
|
$query->setDQL( |
|
54
|
|
|
'SELECT u, |
|
55
|
|
|
( |
|
56
|
|
|
SELECT MAX(a.version) |
|
57
|
|
|
FROM Doctrine\\Tests\\Models\\CMS\\CmsArticle a |
|
58
|
|
|
WHERE a.user = u AND 1 = :paramInSubSelect |
|
59
|
|
|
) AS HIDDEN max_version |
|
60
|
|
|
FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u |
|
61
|
|
|
WHERE u.id = :paramInWhere' |
|
62
|
|
|
); |
|
63
|
|
|
$query->setParameters(['paramInWhere' => $paramInWhere, 'paramInSubSelect' => $paramInSubSelect]); |
|
64
|
|
|
$paginator = (new Paginator($query, true))->setUseOutputWalkers(false); |
|
65
|
|
|
|
|
66
|
|
|
$this->connection->expects($this->exactly(3))->method('executeQuery'); |
|
67
|
|
|
|
|
68
|
|
|
$this->connection->expects($this->at(0)) |
|
69
|
|
|
->method('executeQuery') |
|
70
|
|
|
->with($this->anything(), [$paramInWhere]) |
|
71
|
|
|
; |
|
72
|
|
|
|
|
73
|
|
|
$this->connection->expects($this->at(1)) |
|
74
|
|
|
->method('executeQuery') |
|
75
|
|
|
->with($this->anything(), [$paramInWhere]) |
|
76
|
|
|
; |
|
77
|
|
|
|
|
78
|
|
|
$this->connection->expects($this->at(2)) |
|
79
|
|
|
->method('executeQuery') |
|
80
|
|
|
->with($this->anything(), [$paramInSubSelect, $paramInWhere, $returnedIds]) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
$paginator->count(); |
|
84
|
|
|
$paginator->getIterator(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testPaginatorNotCaringAboutExtraParametersWithoutOutputWalkers() : void |
|
88
|
|
|
{ |
|
89
|
|
|
$this->connection->expects($this->exactly(3))->method('executeQuery'); |
|
90
|
|
|
|
|
91
|
|
|
$this->createPaginatorWithExtraParametersWithoutOutputWalkers([])->count(); |
|
92
|
|
|
$this->createPaginatorWithExtraParametersWithoutOutputWalkers([[10]])->count(); |
|
93
|
|
|
$this->createPaginatorWithExtraParametersWithoutOutputWalkers([])->getIterator(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testgetIteratorDoesCareAboutExtraParametersWithoutOutputWalkersWhenResultIsNotEmpty() : void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->connection->expects($this->exactly(1))->method('executeQuery'); |
|
99
|
|
|
$this->expectException(Query\QueryException::class); |
|
100
|
|
|
$this->expectExceptionMessage('Too many parameters: the query defines 1 parameters and you bound 2'); |
|
101
|
|
|
|
|
102
|
|
|
$this->createPaginatorWithExtraParametersWithoutOutputWalkers([[10]])->getIterator(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param int[][] $willReturnRows |
|
107
|
|
|
*/ |
|
108
|
|
|
private function createPaginatorWithExtraParametersWithoutOutputWalkers(array $willReturnRows) : Paginator |
|
109
|
|
|
{ |
|
110
|
|
|
$this->hydrator->method('hydrateAll')->willReturn($willReturnRows); |
|
111
|
|
|
$this->connection->method('executeQuery')->with($this->anything(), []); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
$query = new Query($this->em); |
|
114
|
|
|
$query->setDQL('SELECT u FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u'); |
|
115
|
|
|
$query->setParameters(['paramInWhere' => 1]); |
|
116
|
|
|
|
|
117
|
|
|
return (new Paginator($query, true))->setUseOutputWalkers(false); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..