1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Tests\DataLoader; |
12
|
|
|
|
13
|
|
|
use Addiks\RDMBundle\DataLoader\SimpleSelectDataLoader; |
14
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
15
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
16
|
|
|
use Addiks\RDMBundle\Mapping\ChoiceMapping; |
17
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
|
|
|
|
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
20
|
|
|
use Doctrine\DBAL\Connection; |
21
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
22
|
|
|
use Doctrine\ORM\Query\Expr; |
23
|
|
|
use Doctrine\DBAL\Driver\Statement; |
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
use Doctrine\ORM\UnitOfWork; |
26
|
|
|
use Addiks\RDMBundle\Tests\Hydration\ServiceExample; |
27
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
28
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
29
|
|
|
use Doctrine\DBAL\Schema\Column; |
30
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
31
|
|
|
use Doctrine\DBAL\Types\Type; |
32
|
|
|
|
33
|
|
|
final class SimpleSelectLoaderTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var SimpleSelectDataLoader |
38
|
|
|
*/ |
39
|
|
|
private $dataLoader; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var MappingDriverInterface |
43
|
|
|
*/ |
44
|
|
|
private $mappingDriver; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var ValueResolverInterface |
48
|
|
|
*/ |
49
|
|
|
private $valueResolver; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ClassMetadata |
53
|
|
|
*/ |
54
|
|
|
private $classMetaData; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Connection |
58
|
|
|
*/ |
59
|
|
|
private $connection; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var QueryBuilder |
63
|
|
|
*/ |
64
|
|
|
private $queryBuilder; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Expr |
68
|
|
|
*/ |
69
|
|
|
private $expr; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var Statement |
73
|
|
|
*/ |
74
|
|
|
private $statement; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var EntityManagerInterface |
78
|
|
|
*/ |
79
|
|
|
private $entityManager; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var UnitOfWork |
83
|
|
|
*/ |
84
|
|
|
private $unitOfWork; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var EntityMappingInterface |
88
|
|
|
*/ |
89
|
|
|
private $entityMapping; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var ChoiceMapping |
93
|
|
|
*/ |
94
|
|
|
private $mappings = array(); |
95
|
|
|
|
96
|
|
|
public function setUp(): void |
97
|
|
|
{ |
98
|
|
|
$this->mappingDriver = $this->createMock(MappingDriverInterface::class); |
|
|
|
|
99
|
|
|
$this->connection = $this->createMock(Connection::class); |
|
|
|
|
100
|
|
|
$this->queryBuilder = $this->createMock(QueryBuilder::class); |
|
|
|
|
101
|
|
|
$this->expr = $this->createMock(Expr::class); |
|
|
|
|
102
|
|
|
$this->statement = $this->createMock(Statement::class); |
|
|
|
|
103
|
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class); |
|
|
|
|
104
|
|
|
$this->unitOfWork = $this->createMock(UnitOfWork::class); |
|
|
|
|
105
|
|
|
$this->entityMapping = $this->createMock(EntityMappingInterface::class); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->dataLoader = new SimpleSelectDataLoader( |
108
|
|
|
$this->mappingDriver, |
109
|
|
|
1 |
|
|
|
|
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->classMetaData = new ClassMetadata(EntityExample::class); |
113
|
|
|
$this->classMetaData->table = ['name' => 'some_table']; |
114
|
|
|
$this->classMetaData->identifier = ["id"]; |
115
|
|
|
$this->classMetaData->fieldMappings = [ |
116
|
|
|
"id" => [ |
117
|
|
|
'columnName' => 'id' |
118
|
|
|
] |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
$this->mappings['foo'] = $this->createMock(MappingInterface::class); |
122
|
|
|
$this->mappings['bar'] = $this->createMock(MappingInterface::class); |
123
|
|
|
|
124
|
|
|
$this->mappingDriver->method("loadRDMMetadataForClass")->willReturn($this->entityMapping); |
125
|
|
|
$this->entityManager->method("getClassMetadata")->willReturn($this->classMetaData); |
126
|
|
|
$this->entityManager->method("getConnection")->willReturn($this->connection); |
127
|
|
|
$this->entityManager->method("getUnitOfWork")->willReturn($this->unitOfWork); |
128
|
|
|
$this->entityManager->method('getMetadataFactory')->willReturn($this->createMock(ClassMetadataFactory::class)); |
129
|
|
|
$this->connection->method('createQueryBuilder')->willReturn($this->queryBuilder); |
130
|
|
|
$this->queryBuilder->method('expr')->willReturn($this->expr); |
131
|
|
|
$this->queryBuilder->method('execute')->willReturn($this->statement); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @test |
136
|
|
|
*/ |
137
|
|
|
public function loadsDataFromDatabase() |
138
|
|
|
{ |
139
|
|
|
/** @var array $expectedData */ |
140
|
|
|
$expectedData = array( |
141
|
|
|
'foo_column' => 'Lorem ipsum', |
142
|
|
|
'bar_column' => 'dolor sit amet' |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$this->classMetaData->identifier = ["id", "secondId"]; |
146
|
|
|
$this->classMetaData->fieldMappings = [ |
147
|
|
|
"id" => ['columnName' => 'id'], |
148
|
|
|
"secondId" => ['columnName' => 'secondId'], |
149
|
|
|
"faz" => ['columnName' => 'faz'] |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$this->queryBuilder->expects($this->once())->method('from')->with($this->equalTo('some_table')); |
|
|
|
|
153
|
|
|
$this->queryBuilder->expects($this->exactly(2))->method('andWhere')->with($this->equalTo("*eq-return*")); |
154
|
|
|
$this->queryBuilder->expects($this->exactly(2))->method('addSelect'); |
155
|
|
|
$this->queryBuilder->expects($this->once())->method('setMaxResults')->with($this->equalTo(1)); |
156
|
|
|
|
157
|
|
|
$this->statement->method('fetch')->willReturn($expectedData); |
|
|
|
|
158
|
|
|
|
159
|
|
|
/** @var ServiceExample $fazService */ |
160
|
|
|
$fazService = $this->createMock(ServiceExample::class); |
161
|
|
|
|
162
|
|
|
$entity = new EntityExample(null, null, null, $fazService, null, "second_id"); |
163
|
|
|
$entity->id = "123"; |
164
|
|
|
|
165
|
|
|
$this->expr->expects($this->exactly(2))->method("eq")->willReturn("*eq-return*")->withConsecutive( |
|
|
|
|
166
|
|
|
['id', '123'], |
167
|
|
|
['secondId', "'second_id'"] |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
/** @var Column $columnA */ |
171
|
|
|
$columnA = $this->createMock(Column::class); |
172
|
|
|
$columnA->method('getName')->willReturn("id"); |
|
|
|
|
173
|
|
|
|
174
|
|
|
/** @var Column $columnA */ |
175
|
|
|
$columnB = $this->createMock(Column::class); |
176
|
|
|
$columnB->method('getName')->willReturn("secondId"); |
177
|
|
|
|
178
|
|
|
$this->entityMapping->method('collectDBALColumns')->willReturn([ |
|
|
|
|
179
|
|
|
$columnA, |
180
|
|
|
$columnB |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
/** @var array $actualData */ |
184
|
|
|
$actualData = $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager); |
185
|
|
|
|
186
|
|
|
$this->assertEquals($expectedData, $actualData); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @test |
191
|
|
|
*/ |
192
|
|
|
public function shouldNotLoadEntityWithoutId() |
193
|
|
|
{ |
194
|
|
|
/** @var array $expectedData */ |
195
|
|
|
$expectedData = array(); |
196
|
|
|
|
197
|
|
|
$this->classMetaData->identifier = []; |
198
|
|
|
|
199
|
|
|
$this->queryBuilder->expects($this->never())->method('from'); |
200
|
|
|
|
201
|
|
|
$this->statement->method('fetch')->willReturn($expectedData); |
202
|
|
|
|
203
|
|
|
$entity = new EntityExample(); |
204
|
|
|
$entity->id = "some_id"; |
205
|
|
|
|
206
|
|
|
$this->expr->method("eq")->willReturn("*eq-return*"); |
|
|
|
|
207
|
|
|
|
208
|
|
|
/** @var array $actualData */ |
209
|
|
|
$actualData = $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager); |
210
|
|
|
|
211
|
|
|
$this->assertEquals($expectedData, $actualData); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
*/ |
217
|
|
|
public function storesDataInDatabase() |
218
|
|
|
{ |
219
|
|
|
$this->mappings['faz'] = $this->createMock(MappingInterface::class); |
220
|
|
|
|
221
|
|
|
$this->setUp(); |
222
|
|
|
|
223
|
|
|
$this->classMetaData->identifier = ["id", "faz"]; |
224
|
|
|
$this->classMetaData->fieldMappings = [ |
225
|
|
|
"id" => ['columnName' => 'id'], |
226
|
|
|
"faz" => ['columnName' => 'faz'] |
227
|
|
|
]; |
228
|
|
|
|
229
|
|
|
/** @var ServiceExample $fazService */ |
230
|
|
|
$fazService = $this->createMock(ServiceExample::class); |
231
|
|
|
|
232
|
|
|
$entity = new EntityExample(null, null, null, $fazService); |
233
|
|
|
$entity->id = "some_id"; |
234
|
|
|
$entity->foo = "some_ipsum_service"; |
|
|
|
|
235
|
|
|
$entity->bar = "some_dolor_service"; |
|
|
|
|
236
|
|
|
|
237
|
|
|
$this->mappings['foo']->method("revertValue")->willReturn(["foo_column" => "ipsum"]); |
238
|
|
|
$this->mappings['bar']->method("revertValue")->willReturn(["bar_column" => "dolor"]); |
239
|
|
|
$this->mappings['faz']->method("revertValue")->willReturn(["faz_column" => "sit"]); |
240
|
|
|
|
241
|
|
|
$this->entityMapping->method('getEntityClassName')->willReturn(EntityExample::class); |
242
|
|
|
$this->entityMapping->method('getFieldMappings')->willReturn($this->mappings); |
243
|
|
|
$this->entityMapping->method('revertValue')->willReturn([ |
244
|
|
|
'foo_column' => 'ipsum', |
245
|
|
|
'bar_column' => 'dolor', |
246
|
|
|
'faz_column' => 'sit' |
247
|
|
|
]); |
248
|
|
|
|
249
|
|
|
$this->connection->expects($this->once())->method("update")->with( |
250
|
|
|
$this->equalTo("some_table"), |
251
|
|
|
$this->equalTo([ |
252
|
|
|
'foo_column' => 'ipsum', |
253
|
|
|
'bar_column' => 'dolor', |
254
|
|
|
'faz_column' => 'sit' |
255
|
|
|
]), |
256
|
|
|
$this->equalTo([ |
257
|
|
|
'id' => "some_id", |
258
|
|
|
'faz' => $fazService |
259
|
|
|
]) |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$this->dataLoader->storeDBALDataForEntity($entity, $this->entityManager); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @test |
267
|
|
|
*/ |
268
|
|
|
public function shouldNotUpdateIfDataDidNotChange() |
269
|
|
|
{ |
270
|
|
|
$entity = new EntityExample(); |
271
|
|
|
$entity->id = "some_id"; |
272
|
|
|
$entity->foo = "some_ipsum_service"; |
|
|
|
|
273
|
|
|
$entity->bar = "some_dolor_service"; |
|
|
|
|
274
|
|
|
|
275
|
|
|
$this->entityMapping->method('getEntityClassName')->willReturn(EntityExample::class); |
276
|
|
|
$this->entityMapping->method('getFieldMappings')->willReturn($this->mappings); |
277
|
|
|
$this->entityMapping->method('collectDBALColumns')->willReturn([ |
278
|
|
|
new Column("foo_column", Type::getType('string')), |
279
|
|
|
new Column("bar_column", Type::getType('string')), |
280
|
|
|
]); |
281
|
|
|
|
282
|
|
|
$this->mappings['foo']->method("revertValue")->willReturn(["foo_column" => "ipsum"]); |
283
|
|
|
$this->mappings['bar']->method("revertValue")->willReturn(["bar_column" => "dolor"]); |
284
|
|
|
|
285
|
|
|
/** @var array $map */ |
286
|
|
|
$map = array( |
|
|
|
|
287
|
|
|
"some_ipsum_service" => ["foo_column" => "ipsum"], |
288
|
|
|
"some_dolor_service" => ["bar_column" => "dolor"], |
289
|
|
|
); |
290
|
|
|
|
291
|
|
|
# $this->valueResolver->method("revertValue")->will($this->returnCallback( |
292
|
|
|
# function ($fieldMapping, $context, $value) use ($map) { |
293
|
|
|
# return $map[$value]; |
294
|
|
|
# } |
295
|
|
|
# )); |
296
|
|
|
|
297
|
|
|
# $this->valueResolver->method("revertValue")->will($this->returnValueMap([ |
298
|
|
|
# [$this->mappings['foo'], $entity, "some_ipsum_service", ["foo_column" => "ipsum"]], |
299
|
|
|
# [$this->mappings['bar'], $entity, "some_dolor_service", ["bar_column" => "dolor"]], |
300
|
|
|
# ])); |
301
|
|
|
|
302
|
|
|
$this->connection->expects($this->never())->method("update"); |
|
|
|
|
303
|
|
|
|
304
|
|
|
/** @var array $expectedData */ |
305
|
|
|
$expectedData = [ |
306
|
|
|
'foo_column' => 'ipsum', |
307
|
|
|
'bar_column' => 'dolor' |
308
|
|
|
]; |
309
|
|
|
|
310
|
|
|
$this->statement->method('fetch')->willReturn($expectedData); |
311
|
|
|
|
312
|
|
|
$this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager); |
313
|
|
|
$this->dataLoader->storeDBALDataForEntity($entity, $this->entityManager); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @test |
318
|
|
|
*/ |
319
|
|
|
public function shouldRemoveDBALForEntity() |
320
|
|
|
{ |
321
|
|
|
$this->dataLoader->removeDBALDataForEntity(new EntityExample(), $this->entityManager); |
322
|
|
|
$this->assertTrue(true); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @test |
327
|
|
|
*/ |
328
|
|
|
public function shouldPrepareOnMetadataLoad() |
329
|
|
|
{ |
330
|
|
|
$this->dataLoader->prepareOnMetadataLoad($this->entityManager, $this->classMetaData); |
331
|
|
|
$this->assertTrue(true); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths