1
|
|
|
<?php |
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\Tests\DataMapper; |
3
|
|
|
|
4
|
|
|
use Ajir\RabbitMqSqlBundle\DataMapper\DataMapper; |
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DataMapperTest |
9
|
|
|
* |
10
|
|
|
* @author Florian Ajir <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class DataMapperTest extends PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The simple mapping fixture |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $mapping; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The relationnal mapping fixture |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $relationnalMapping; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
public function testGetTargetEntity() |
33
|
|
|
{ |
34
|
|
|
$mapper = new DataMapper($this->mapping); |
35
|
|
|
$expected = 'Group'; |
36
|
|
|
$this->assertEquals($expected, $mapper->getTargetEntity('User', 'Groups', 'manyToMany')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
public function testGetFieldColumn() |
43
|
|
|
{ |
44
|
|
|
$mapper = new DataMapper($this->mapping); |
45
|
|
|
$expected = 'identifier'; |
46
|
|
|
$this->assertEquals($expected, $mapper->getFieldColumn('Customer', 'sku')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
public function testGetFieldMaxLength() |
53
|
|
|
{ |
54
|
|
|
$mapper = new DataMapper($this->mapping); |
55
|
|
|
$expected = 23; |
56
|
|
|
$this->assertEquals($expected, $mapper->getFieldMaxLength('Customer', 'sku')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public function testGetFieldsName() |
63
|
|
|
{ |
64
|
|
|
$mapper = new DataMapper($this->mapping); |
65
|
|
|
$expected = array('sku', 'parent_id'); |
66
|
|
|
$this->assertEquals($expected, $mapper->getFieldsName('User')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
|
|
public function testGetFieldNullable() |
73
|
|
|
{ |
74
|
|
|
$mapper = new DataMapper($this->mapping); |
75
|
|
|
$this->assertFalse($mapper->isFieldNullable('User', 'sku')); |
76
|
|
|
$this->assertTrue($mapper->isFieldNullable('User', 'parent_id')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
public function testGetFieldType() |
83
|
|
|
{ |
84
|
|
|
$mapper = new DataMapper($this->mapping); |
85
|
|
|
$this->assertEquals('int', $mapper->getFieldType('User', 'parent_id')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
public function testGetFieldMapping() |
92
|
|
|
{ |
93
|
|
|
$mapper = new DataMapper($this->mapping); |
94
|
|
|
$expected = array( |
95
|
|
|
'column' => 'sku', |
96
|
|
|
'length' => 23, |
97
|
|
|
'type' => 'string', |
98
|
|
|
'nullable' => false, |
99
|
|
|
); |
100
|
|
|
$this->assertEquals( |
101
|
|
|
$expected, |
102
|
|
|
$mapper->getFieldMapping('User', 'sku') |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
public function testGetRelation() |
110
|
|
|
{ |
111
|
|
|
$mapper = new DataMapper($this->mapping); |
112
|
|
|
$expected = 'oneToOne'; |
113
|
|
|
$this->assertEquals( |
114
|
|
|
$expected, |
115
|
|
|
$mapper->getRelation('User', 'Customer') |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
public function testGetTableName() |
123
|
|
|
{ |
124
|
|
|
$mapper = new DataMapper($this->mapping); |
125
|
|
|
$expected = 'customer'; |
126
|
|
|
$this->assertEquals( |
127
|
|
|
$expected, |
128
|
|
|
$mapper->getTableName('Customer') |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* |
134
|
|
|
*/ |
135
|
|
|
public function testGetIdentifier() |
136
|
|
|
{ |
137
|
|
|
$mapper = new DataMapper($this->mapping); |
138
|
|
|
$expected = 'sku'; |
139
|
|
|
$this->assertEquals( |
140
|
|
|
$expected, |
141
|
|
|
$mapper->getIdentifier('User') |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* |
147
|
|
|
*/ |
148
|
|
|
public function testGetRelationInfos() |
149
|
|
|
{ |
150
|
|
|
$mapper = new DataMapper($this->mapping); |
151
|
|
|
$expected = array( |
152
|
|
|
'joinColumn' => array( |
153
|
|
|
'referencedColumnName' => 'id', |
154
|
|
|
'name' => 'customer_id', |
155
|
|
|
), |
156
|
|
|
'targetEntity' => 'Customer', |
157
|
|
|
'table' => 'customer', |
158
|
|
|
); |
159
|
|
|
$this->assertEquals( |
160
|
|
|
$expected, |
161
|
|
|
$mapper->getRelationInfos('User', 'Customer', 'oneToOne') |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* |
167
|
|
|
*/ |
168
|
|
|
public function testGetJoinTable() |
169
|
|
|
{ |
170
|
|
|
$mapper = new DataMapper($this->mapping); |
171
|
|
|
$expected = array( |
172
|
|
|
'joinColumns' => array( |
173
|
|
|
'user_id' => array( |
174
|
|
|
'referencedColumnName' => 'id', |
175
|
|
|
), |
176
|
|
|
), |
177
|
|
|
'name' => 'users_groups', |
178
|
|
|
'inverseJoinColumns' => array( |
179
|
|
|
'group_id' => array( |
180
|
|
|
'referencedColumnName' => 'id', |
181
|
|
|
), |
182
|
|
|
), |
183
|
|
|
); |
184
|
|
|
$this->assertEquals( |
185
|
|
|
$expected, |
186
|
|
|
$mapper->getJoinTable('User', 'Groups', 'manyToMany') |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
*/ |
193
|
|
|
public function testRelationExpectCollection() |
194
|
|
|
{ |
195
|
|
|
$mapper = new DataMapper($this->mapping); |
196
|
|
|
$expected = true; |
197
|
|
|
$this->assertEquals( |
198
|
|
|
$expected, |
199
|
|
|
$mapper->isCollection('manyToMany') |
200
|
|
|
); |
201
|
|
|
$expected = true; |
202
|
|
|
$this->assertEquals( |
203
|
|
|
$expected, |
204
|
|
|
$mapper->isCollection('oneToMany') |
205
|
|
|
); |
206
|
|
|
$expected = false; |
207
|
|
|
$this->assertEquals( |
208
|
|
|
$expected, |
209
|
|
|
$mapper->isCollection('oneToOne') |
210
|
|
|
); |
211
|
|
|
$expected = false; |
212
|
|
|
$this->assertEquals( |
213
|
|
|
$expected, |
214
|
|
|
$mapper->isCollection('manyToOne') |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* |
220
|
|
|
*/ |
221
|
|
|
public function testGetDiscriminator() |
222
|
|
|
{ |
223
|
|
|
$mapper = new DataMapper($this->mapping); |
224
|
|
|
$expected = 'dtype'; |
225
|
|
|
$this->assertEquals( |
226
|
|
|
$expected, |
227
|
|
|
$mapper->getDiscriminator('Supplier') |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* |
233
|
|
|
*/ |
234
|
|
|
public function testFieldLengthNotNumeric() |
235
|
|
|
{ |
236
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
237
|
|
|
$mapper = new DataMapper(array( |
238
|
|
|
'Test' => array( |
239
|
|
|
'table' => 'test', |
240
|
|
|
'fields' => array( |
241
|
|
|
'sku' => array( |
242
|
|
|
'column' => 'identifier', |
243
|
|
|
'length' => 'error' |
244
|
|
|
) |
245
|
|
|
), |
246
|
|
|
) |
247
|
|
|
)); |
248
|
|
|
$mapper->getFieldMaxLength('Test', 'sku'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* |
253
|
|
|
*/ |
254
|
|
|
public function testGetFixedFieldMapping() |
255
|
|
|
{ |
256
|
|
|
$mapper = new DataMapper(array( |
257
|
|
|
'Test' => array( |
258
|
|
|
'table' => 'test', |
259
|
|
|
'fields' => array( |
260
|
|
|
'sku' => array( |
261
|
|
|
'column' => 'identifier', |
262
|
|
|
'value' => '1234567' |
263
|
|
|
) |
264
|
|
|
), |
265
|
|
|
) |
266
|
|
|
)); |
267
|
|
|
$fixedMapping = $mapper->getFixedFieldMapping('Test', 'sku'); |
268
|
|
|
$this->assertArrayHasKey('identifier', $fixedMapping); |
269
|
|
|
$this->assertEquals('1234567', $fixedMapping['identifier']); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Sets up the fixture |
274
|
|
|
*/ |
275
|
|
|
protected function setUp() |
276
|
|
|
{ |
277
|
|
|
$this->mapping = array(); |
278
|
|
|
$this->setUpAddressMapping(); |
279
|
|
|
$this->setUpCustomerMapping(); |
280
|
|
|
$this->setUpGroupMapping(); |
281
|
|
|
$this->setUpSupplierMapping(); |
282
|
|
|
$this->setUpUserMapping(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Sets up the fixture |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
private function setUpAddressMapping() |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$this->mapping['Address'] = array( |
291
|
|
|
'table' => 'address', |
292
|
|
|
'fields' => array( |
293
|
|
|
'sku' => array( |
294
|
|
|
'column' => 'sku', |
295
|
|
|
'length' => 255, |
296
|
|
|
'type' => 'string', |
297
|
|
|
'nullable' => false |
298
|
|
|
), |
299
|
|
|
) |
300
|
|
|
); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Sets up the fixture |
305
|
|
|
*/ |
306
|
|
View Code Duplication |
private function setUpCustomerMapping() |
|
|
|
|
307
|
|
|
{ |
308
|
|
|
$this->mapping['Customer'] = array( |
309
|
|
|
'table' => 'customer', |
310
|
|
|
'fields' => array( |
311
|
|
|
'sku' => array( |
312
|
|
|
'column' => 'identifier', |
313
|
|
|
'length' => 23, |
314
|
|
|
'type' => 'string', |
315
|
|
|
'nullable' => false, |
316
|
|
|
) |
317
|
|
|
) |
318
|
|
|
); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Sets up the fixture |
323
|
|
|
*/ |
324
|
|
View Code Duplication |
private function setUpGroupMapping() |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
$this->mapping['Group'] = array( |
327
|
|
|
'table' => 'group', |
328
|
|
|
'fields' => array( |
329
|
|
|
'sku' => array( |
330
|
|
|
'column' => 'sku', |
331
|
|
|
'length' => 23, |
332
|
|
|
'type' => 'string', |
333
|
|
|
'nullable' => false, |
334
|
|
|
), |
335
|
|
|
) |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Sets up the fixture |
341
|
|
|
*/ |
342
|
|
View Code Duplication |
private function setUpSupplierMapping() |
|
|
|
|
343
|
|
|
{ |
344
|
|
|
$this->mapping['Supplier'] = array( |
345
|
|
|
'discriminator' => 'dtype', |
346
|
|
|
'fields' => array( |
347
|
|
|
'sku' => array( |
348
|
|
|
'column' => 'identifier', |
349
|
|
|
'length' => 23, |
350
|
|
|
'type' => 'string', |
351
|
|
|
'nullable' => false, |
352
|
|
|
) |
353
|
|
|
) |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Sets up the fixture |
359
|
|
|
*/ |
360
|
|
|
private function setUpUserMapping() |
361
|
|
|
{ |
362
|
|
|
$this->mapping['User'] = array( |
363
|
|
|
'oneToOne' => array( |
364
|
|
|
'Customer' => array( |
365
|
|
|
'joinColumn' => array( |
366
|
|
|
'referencedColumnName' => 'id', |
367
|
|
|
'name' => 'customer_id', |
368
|
|
|
), |
369
|
|
|
'targetEntity' => 'Customer', |
370
|
|
|
), |
371
|
|
|
), |
372
|
|
|
'oneToMany' => array( |
373
|
|
|
'Children' => array( |
374
|
|
|
'joinColumn' => array( |
375
|
|
|
'referencedColumnName' => 'id', |
376
|
|
|
'name' => 'parent_id', |
377
|
|
|
), |
378
|
|
|
'targetEntity' => 'User', |
379
|
|
|
), |
380
|
|
|
), |
381
|
|
|
'fields' => array( |
382
|
|
|
'sku' => array( |
383
|
|
|
'column' => 'sku', |
384
|
|
|
'length' => 23, |
385
|
|
|
'type' => 'string', |
386
|
|
|
'nullable' => false, |
387
|
|
|
), |
388
|
|
|
'parent_id' => array( |
389
|
|
|
'column' => 'parent_id', |
390
|
|
|
'type' => 'int', |
391
|
|
|
'nullable' => true, |
392
|
|
|
), |
393
|
|
|
), |
394
|
|
|
'manyToOne' => array( |
395
|
|
|
'Address' => array( |
396
|
|
|
'joinColumn' => array( |
397
|
|
|
'referencedColumnName' => 'id', |
398
|
|
|
'name' => 'address_id', |
399
|
|
|
), |
400
|
|
|
'targetEntity' => 'Address', |
401
|
|
|
), |
402
|
|
|
), |
403
|
|
|
'table' => 'user', |
404
|
|
|
'identifier' => 'sku', |
405
|
|
|
'manyToMany' => array( |
406
|
|
|
'Groups' => array( |
407
|
|
|
'joinTable' => array( |
408
|
|
|
'joinColumns' => array( |
409
|
|
|
'user_id' => array( |
410
|
|
|
'referencedColumnName' => 'id', |
411
|
|
|
), |
412
|
|
|
), |
413
|
|
|
'name' => 'users_groups', |
414
|
|
|
'inverseJoinColumns' => array( |
415
|
|
|
'group_id' => array( |
416
|
|
|
'referencedColumnName' => 'id', |
417
|
|
|
), |
418
|
|
|
), |
419
|
|
|
), |
420
|
|
|
'targetEntity' => 'Group', |
421
|
|
|
), |
422
|
|
|
), |
423
|
|
|
); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.