1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\DoctrineQueryFilter; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\QueryBuilder; |
8
|
|
|
use Arp\DoctrineQueryFilter\QueryBuilderInterface; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\Query\Expr; |
11
|
|
|
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder; |
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Alex Patterson <[email protected]> |
17
|
|
|
* @package ArpTest\DoctrineQueryFilter |
18
|
|
|
*/ |
19
|
|
|
final class QueryBuilderTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DoctrineQueryBuilder |
23
|
|
|
*/ |
24
|
|
|
private DoctrineQueryBuilder $doctrineQueryBuilder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepare the test case dependencies |
28
|
|
|
*/ |
29
|
|
|
public function setUp(): void |
30
|
|
|
{ |
31
|
|
|
$this->doctrineQueryBuilder = $this->createMock(DoctrineQueryBuilder::class); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Assert the class implement QueryBuilderInterface |
36
|
|
|
*/ |
37
|
|
|
public function testInstanceOfQueryBuilderInterface(): void |
38
|
|
|
{ |
39
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
40
|
|
|
|
41
|
|
|
$this->assertInstanceOf(QueryBuilderInterface::class, $queryBuilder); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Assert calls to createQueryBuilder will return a new instance of itself |
46
|
|
|
*/ |
47
|
|
|
public function testCreateQueryBuilderWillReturnAnewInstanceOfItself(): void |
48
|
|
|
{ |
49
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
50
|
|
|
|
51
|
|
|
/** @var EntityManager|MockObject $entityManager */ |
52
|
|
|
$entityManager = $this->createMock(EntityManager::class); |
53
|
|
|
|
54
|
|
|
/** @var DoctrineQueryBuilder|MockObject $newDoctrineQueryBuilder */ |
55
|
|
|
$newDoctrineQueryBuilder = $this->createMock(DoctrineQueryBuilder::class); |
56
|
|
|
|
57
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
|
|
|
58
|
|
|
->method('getEntityManager') |
59
|
|
|
->willReturn($entityManager); |
60
|
|
|
|
61
|
|
|
$entityManager->expects($this->once()) |
62
|
|
|
->method('createQueryBuilder') |
63
|
|
|
->willReturn($newDoctrineQueryBuilder); |
64
|
|
|
|
65
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $queryBuilder->createQueryBuilder()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Assert that calls to getEntityManager will return the internal query builder's entity manager instance |
70
|
|
|
*/ |
71
|
|
|
public function testGetEntityManagerWillReturnTheConfiguredEntityManagerInstance(): void |
72
|
|
|
{ |
73
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
74
|
|
|
|
75
|
|
|
/** @var EntityManager|MockObject $entityManager */ |
76
|
|
|
$entityManager = $this->createMock(EntityManager::class); |
77
|
|
|
|
78
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
79
|
|
|
->method('getEntityManager') |
80
|
|
|
->willReturn($entityManager); |
81
|
|
|
|
82
|
|
|
$queryBuilder->getEntityManager(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Assert that calls to expr() will return the internal query builder's Expr instance |
87
|
|
|
*/ |
88
|
|
|
public function testGetQueryBuilderWillReturnTheConfiguredExprInstance(): void |
89
|
|
|
{ |
90
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
91
|
|
|
|
92
|
|
|
/** @var Expr|MockObject $expr */ |
93
|
|
|
$expr = $this->createMock(Expr::class); |
94
|
|
|
|
95
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
96
|
|
|
->method('expr') |
97
|
|
|
->willReturn($expr); |
98
|
|
|
|
99
|
|
|
$queryBuilder->expr(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Assert that calls to expr() will return the internal query builder's DQL parts |
104
|
|
|
*/ |
105
|
|
|
public function testGetEntityManagerWillReturnTheConfiguredExprInstance(): void |
106
|
|
|
{ |
107
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
108
|
|
|
|
109
|
|
|
$parts = [ |
110
|
|
|
'foo' => 'bar', |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
114
|
|
|
->method('getDQLParts') |
115
|
|
|
->willReturn($parts); |
116
|
|
|
|
117
|
|
|
$this->assertSame($parts, $queryBuilder->getQueryParts()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Assert that calls to innerJoin() will proxy to the internal query builder |
122
|
|
|
*/ |
123
|
|
|
public function testInnerJoinWillProxyToInternalQueryBuilder(): void |
124
|
|
|
{ |
125
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
126
|
|
|
|
127
|
|
|
$name = 'foo'; |
128
|
|
|
$alias = 'a'; |
129
|
|
|
$type = Expr\Join::ON; |
130
|
|
|
$condition = '1 = 1'; |
131
|
|
|
$indexBy = null; |
132
|
|
|
|
133
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
134
|
|
|
->method('innerJoin') |
135
|
|
|
->with($name, $alias, $type, $condition, $indexBy); |
136
|
|
|
|
137
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->innerJoin($name, $alias, $type, $condition, $indexBy)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Assert that calls to leftJoin() will proxy to the internal query builder |
142
|
|
|
*/ |
143
|
|
|
public function testLeftJoinWillProxyToInternalQueryBuilder(): void |
144
|
|
|
{ |
145
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
146
|
|
|
|
147
|
|
|
$name = 'bar'; |
148
|
|
|
$alias = 'b'; |
149
|
|
|
$type = Expr\Join::ON; |
150
|
|
|
$condition = 'a.test = b.hello'; |
151
|
|
|
$indexBy = 'a.name'; |
152
|
|
|
|
153
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
154
|
|
|
->method('leftJoin') |
155
|
|
|
->with($name, $alias, $type, $condition, $indexBy); |
156
|
|
|
|
157
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->leftJoin($name, $alias, $type, $condition, $indexBy)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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..