1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineModuleTest\Validator\Adapter; |
21
|
|
|
|
22
|
|
|
use stdClass; |
23
|
|
|
use PHPUnit_Framework_TestCase as BaseTestCase; |
24
|
|
|
use DoctrineModule\Validator\UniqueObject; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Tests for the UniqueObject validator |
28
|
|
|
* |
29
|
|
|
* @license MIT |
30
|
|
|
* @link http://www.doctrine-project.org/ |
31
|
|
|
* @author Oskar Bley <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class UniqueObjectTest extends BaseTestCase |
34
|
|
|
{ |
35
|
|
|
public function testCanValidateWithNotAvailableObjectInRepository() |
36
|
|
|
{ |
37
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
38
|
|
|
$repository |
39
|
|
|
->expects($this->once()) |
40
|
|
|
->method('findOneBy') |
41
|
|
|
->with(['matchKey' => 'matchValue']) |
42
|
|
|
->will($this->returnValue(null)); |
43
|
|
|
|
44
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
45
|
|
|
|
46
|
|
|
$validator = new UniqueObject([ |
47
|
|
|
'object_repository' => $repository, |
48
|
|
|
'object_manager' => $objectManager, |
49
|
|
|
'fields' => 'matchKey', |
50
|
|
|
]); |
51
|
|
|
$this->assertTrue($validator->isValid('matchValue')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testCanValidateIfThereIsTheSameObjectInTheRepository() |
55
|
|
|
{ |
56
|
|
|
$match = new stdClass(); |
57
|
|
|
|
58
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
59
|
|
|
$classMetadata |
60
|
|
|
->expects($this->once()) |
61
|
|
|
->method('getIdentifierFieldNames') |
62
|
|
|
->will($this->returnValue(['id'])); |
63
|
|
|
$classMetadata |
64
|
|
|
->expects($this->once()) |
65
|
|
|
->method('getIdentifierValues') |
66
|
|
|
->with($match) |
67
|
|
|
->will($this->returnValue(['id' => 'identifier'])); |
68
|
|
|
|
69
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
70
|
|
|
$objectManager->expects($this->any()) |
71
|
|
|
->method('getClassMetadata') |
72
|
|
|
->with('stdClass') |
73
|
|
|
->will($this->returnValue($classMetadata)); |
74
|
|
|
|
75
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
76
|
|
|
$repository |
77
|
|
|
->expects($this->any()) |
78
|
|
|
->method('getClassName') |
79
|
|
|
->will($this->returnValue('stdClass')); |
80
|
|
|
$repository |
81
|
|
|
->expects($this->once()) |
82
|
|
|
->method('findOneBy') |
83
|
|
|
->with(['matchKey' => 'matchValue']) |
84
|
|
|
->will($this->returnValue($match)); |
85
|
|
|
|
86
|
|
|
$validator = new UniqueObject([ |
87
|
|
|
'object_repository' => $repository, |
88
|
|
|
'object_manager' => $objectManager, |
89
|
|
|
'fields' => 'matchKey', |
90
|
|
|
]); |
91
|
|
|
$this->assertTrue($validator->isValid(['matchKey' => 'matchValue', 'id' => 'identifier'])); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testCannotValidateIfThereIsAnotherObjectWithTheSameValueInTheRepository() |
95
|
|
|
{ |
96
|
|
|
$match = new stdClass(); |
97
|
|
|
|
98
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
99
|
|
|
$classMetadata |
100
|
|
|
->expects($this->once()) |
101
|
|
|
->method('getIdentifierFieldNames') |
102
|
|
|
->will($this->returnValue(['id'])); |
103
|
|
|
$classMetadata |
104
|
|
|
->expects($this->once()) |
105
|
|
|
->method('getIdentifierValues') |
106
|
|
|
->with($match) |
107
|
|
|
->will($this->returnValue(['id' => 'identifier'])); |
108
|
|
|
|
109
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
110
|
|
|
$objectManager->expects($this->any()) |
111
|
|
|
->method('getClassMetadata') |
112
|
|
|
->with('stdClass') |
113
|
|
|
->will($this->returnValue($classMetadata)); |
114
|
|
|
|
115
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
116
|
|
|
$repository |
117
|
|
|
->expects($this->any()) |
118
|
|
|
->method('getClassName') |
119
|
|
|
->will($this->returnValue('stdClass')); |
120
|
|
|
$repository |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('findOneBy') |
123
|
|
|
->with(['matchKey' => 'matchValue']) |
124
|
|
|
->will($this->returnValue($match)); |
125
|
|
|
|
126
|
|
|
$validator = new UniqueObject([ |
127
|
|
|
'object_repository' => $repository, |
128
|
|
|
'object_manager' => $objectManager, |
129
|
|
|
'fields' => 'matchKey', |
130
|
|
|
]); |
131
|
|
|
$this->assertFalse($validator->isValid(['matchKey' => 'matchValue', 'id' => 'another identifier'])); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testCanFetchIdentifierFromContext() |
135
|
|
|
{ |
136
|
|
|
$match = new stdClass(); |
137
|
|
|
|
138
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
139
|
|
|
$classMetadata |
140
|
|
|
->expects($this->once()) |
141
|
|
|
->method('getIdentifierFieldNames') |
142
|
|
|
->will($this->returnValue(['id'])); |
143
|
|
|
$classMetadata |
144
|
|
|
->expects($this->once()) |
145
|
|
|
->method('getIdentifierValues') |
146
|
|
|
->with($match) |
147
|
|
|
->will($this->returnValue(['id' => 'identifier'])); |
148
|
|
|
|
149
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
150
|
|
|
$objectManager->expects($this->any()) |
151
|
|
|
->method('getClassMetadata') |
152
|
|
|
->with('stdClass') |
153
|
|
|
->will($this->returnValue($classMetadata)); |
154
|
|
|
|
155
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
156
|
|
|
$repository |
157
|
|
|
->expects($this->any()) |
158
|
|
|
->method('getClassName') |
159
|
|
|
->will($this->returnValue('stdClass')); |
160
|
|
|
$repository |
161
|
|
|
->expects($this->once()) |
162
|
|
|
->method('findOneBy') |
163
|
|
|
->with(['matchKey' => 'matchValue']) |
164
|
|
|
->will($this->returnValue($match)); |
165
|
|
|
|
166
|
|
|
$validator = new UniqueObject([ |
167
|
|
|
'object_repository' => $repository, |
168
|
|
|
'object_manager' => $objectManager, |
169
|
|
|
'fields' => 'matchKey', |
170
|
|
|
'use_context' => true, |
171
|
|
|
]); |
172
|
|
|
$this->assertTrue($validator->isValid('matchValue', ['id' => 'identifier'])); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @expectedException \Zend\Validator\Exception\RuntimeException |
177
|
|
|
* @expectedExceptionMessage Expected context to be an array but is null |
178
|
|
|
*/ |
179
|
|
|
public function testThrowsAnExceptionOnUsedButMissingContext() |
180
|
|
|
{ |
181
|
|
|
$match = new stdClass(); |
182
|
|
|
|
183
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
184
|
|
|
$repository |
185
|
|
|
->expects($this->once()) |
186
|
|
|
->method('findOneBy') |
187
|
|
|
->with(['matchKey' => 'matchValue']) |
188
|
|
|
->will($this->returnValue($match)); |
189
|
|
|
|
190
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
191
|
|
|
|
192
|
|
|
$validator = new UniqueObject([ |
193
|
|
|
'object_repository' => $repository, |
194
|
|
|
'object_manager' => $objectManager, |
195
|
|
|
'fields' => 'matchKey', |
196
|
|
|
'use_context' => true, |
197
|
|
|
]); |
198
|
|
|
$validator->isValid('matchValue'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @expectedException \Zend\Validator\Exception\RuntimeException |
203
|
|
|
* @expectedExceptionMessage Expected context to contain id |
204
|
|
|
*/ |
205
|
|
|
public function testThrowsAnExceptionOnMissingIdentifier() |
206
|
|
|
{ |
207
|
|
|
$match = new stdClass(); |
208
|
|
|
|
209
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
210
|
|
|
$classMetadata |
211
|
|
|
->expects($this->once()) |
212
|
|
|
->method('getIdentifierFieldNames') |
213
|
|
|
->will($this->returnValue(['id'])); |
214
|
|
|
|
215
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
216
|
|
|
$objectManager->expects($this->any()) |
217
|
|
|
->method('getClassMetadata') |
218
|
|
|
->with('stdClass') |
219
|
|
|
->will($this->returnValue($classMetadata)); |
220
|
|
|
|
221
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
222
|
|
|
$repository |
223
|
|
|
->expects($this->any()) |
224
|
|
|
->method('getClassName') |
225
|
|
|
->will($this->returnValue('stdClass')); |
226
|
|
|
$repository |
227
|
|
|
->expects($this->once()) |
228
|
|
|
->method('findOneBy') |
229
|
|
|
->with(['matchKey' => 'matchValue']) |
230
|
|
|
->will($this->returnValue($match)); |
231
|
|
|
|
232
|
|
|
$validator = new UniqueObject([ |
233
|
|
|
'object_repository' => $repository, |
234
|
|
|
'object_manager' => $objectManager, |
235
|
|
|
'fields' => 'matchKey', |
236
|
|
|
]); |
237
|
|
|
$validator->isValid('matchValue'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @expectedException \Zend\Validator\Exception\RuntimeException |
242
|
|
|
* @expectedExceptionMessage Expected context to contain id |
243
|
|
|
*/ |
244
|
|
|
public function testThrowsAnExceptionOnMissingIdentifierInContext() |
245
|
|
|
{ |
246
|
|
|
$match = new stdClass(); |
247
|
|
|
|
248
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
249
|
|
|
$classMetadata |
250
|
|
|
->expects($this->once()) |
251
|
|
|
->method('getIdentifierFieldNames') |
252
|
|
|
->will($this->returnValue(['id'])); |
253
|
|
|
|
254
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
255
|
|
|
$objectManager->expects($this->any()) |
256
|
|
|
->method('getClassMetadata') |
257
|
|
|
->with('stdClass') |
258
|
|
|
->will($this->returnValue($classMetadata)); |
259
|
|
|
|
260
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
261
|
|
|
$repository |
262
|
|
|
->expects($this->any()) |
263
|
|
|
->method('getClassName') |
264
|
|
|
->will($this->returnValue('stdClass')); |
265
|
|
|
$repository |
266
|
|
|
->expects($this->once()) |
267
|
|
|
->method('findOneBy') |
268
|
|
|
->with(['matchKey' => 'matchValue']) |
269
|
|
|
->will($this->returnValue($match)); |
270
|
|
|
|
271
|
|
|
$validator = new UniqueObject([ |
272
|
|
|
'object_repository' => $repository, |
273
|
|
|
'object_manager' => $objectManager, |
274
|
|
|
'fields' => 'matchKey', |
275
|
|
|
'use_context' => true, |
276
|
|
|
]); |
277
|
|
|
$validator->isValid('matchValue', []); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @expectedException \Zend\Validator\Exception\InvalidArgumentException |
282
|
|
|
* @expectedExceptionMessage Option "object_manager" is required and must be |
283
|
|
|
* an instance of Doctrine\Common\Persistence\ObjectManager, nothing given |
284
|
|
|
*/ |
285
|
|
|
public function testThrowsAnExceptionOnMissingObjectManager() |
286
|
|
|
{ |
287
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
288
|
|
|
|
289
|
|
|
new UniqueObject([ |
290
|
|
|
'object_repository' => $repository, |
291
|
|
|
'fields' => 'matchKey', |
292
|
|
|
]); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @expectedException \Zend\Validator\Exception\InvalidArgumentException |
297
|
|
|
* @expectedExceptionMessage Option "object_manager" is required and must be |
298
|
|
|
* an instance of Doctrine\Common\Persistence\ObjectManager, nothing given |
299
|
|
|
*/ |
300
|
|
|
public function testThrowsAnExceptionOnWrongObjectManager() |
301
|
|
|
{ |
302
|
|
|
$objectManager = new stdClass(); |
303
|
|
|
|
304
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
305
|
|
|
|
306
|
|
|
new UniqueObject([ |
307
|
|
|
'object_repository' => $repository, |
308
|
|
|
'object_manager' => $objectManager, |
309
|
|
|
'fields' => 'matchKey', |
310
|
|
|
]); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function testCanValidateWithNotAvailableObjectInRepositoryByDateTimeObject() |
314
|
|
|
{ |
315
|
|
|
$date = new \DateTime("17 March 2014"); |
316
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
317
|
|
|
$repository |
318
|
|
|
->expects($this->once()) |
319
|
|
|
->method('findOneBy') |
320
|
|
|
->with(['date' => $date]) |
321
|
|
|
->will($this->returnValue(null)); |
322
|
|
|
|
323
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
324
|
|
|
|
325
|
|
|
$validator = new UniqueObject([ |
326
|
|
|
'object_repository' => $repository, |
327
|
|
|
'object_manager' => $objectManager, |
328
|
|
|
'fields' => 'date', |
329
|
|
|
]); |
330
|
|
|
|
331
|
|
|
$this->assertTrue($validator->isValid($date)); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function testCanFetchIdentifierFromObjectContext() |
335
|
|
|
{ |
336
|
|
|
$context = new stdClass(); |
337
|
|
|
$context->id = 'identifier'; |
338
|
|
|
|
339
|
|
|
$match = new stdClass(); |
340
|
|
|
|
341
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
342
|
|
|
$classMetadata |
343
|
|
|
->expects($this->at(0)) |
344
|
|
|
->method('getIdentifierValues') |
345
|
|
|
->with($context) |
346
|
|
|
->will($this->returnValue(['id' => 'identifier'])); |
347
|
|
|
$classMetadata |
348
|
|
|
->expects($this->at(1)) |
349
|
|
|
->method('getIdentifierValues') |
350
|
|
|
->with($match) |
351
|
|
|
->will($this->returnValue(['id' => 'identifier'])); |
352
|
|
|
|
353
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
354
|
|
|
$objectManager->expects($this->any()) |
355
|
|
|
->method('getClassMetadata') |
356
|
|
|
->with('stdClass') |
357
|
|
|
->will($this->returnValue($classMetadata)); |
358
|
|
|
|
359
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
360
|
|
|
$repository |
361
|
|
|
->expects($this->any()) |
362
|
|
|
->method('getClassName') |
363
|
|
|
->will($this->returnValue('stdClass')); |
364
|
|
|
$repository |
365
|
|
|
->expects($this->once()) |
366
|
|
|
->method('findOneBy') |
367
|
|
|
->with(['matchKey' => 'matchValue']) |
368
|
|
|
->will($this->returnValue($match)); |
369
|
|
|
|
370
|
|
|
$validator = new UniqueObject([ |
371
|
|
|
'object_repository' => $repository, |
372
|
|
|
'object_manager' => $objectManager, |
373
|
|
|
'fields' => 'matchKey', |
374
|
|
|
'use_context' => true, |
375
|
|
|
]); |
376
|
|
|
|
377
|
|
|
$this->assertTrue($validator->isValid('matchValue', $context)); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function testErrorMessageIsStringInsteadArray() |
381
|
|
|
{ |
382
|
|
|
$match = new stdClass(); |
383
|
|
|
|
384
|
|
|
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata'); |
385
|
|
|
$classMetadata |
386
|
|
|
->expects($this->once()) |
387
|
|
|
->method('getIdentifierFieldNames') |
388
|
|
|
->will($this->returnValue(['id'])); |
389
|
|
|
$classMetadata |
390
|
|
|
->expects($this->once()) |
391
|
|
|
->method('getIdentifierValues') |
392
|
|
|
->with($match) |
393
|
|
|
->will($this->returnValue(['id' => 'identifier'])); |
394
|
|
|
|
395
|
|
|
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); |
396
|
|
|
$objectManager->expects($this->any()) |
397
|
|
|
->method('getClassMetadata') |
398
|
|
|
->with('stdClass') |
399
|
|
|
->will($this->returnValue($classMetadata)); |
400
|
|
|
|
401
|
|
|
$repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); |
402
|
|
|
$repository |
403
|
|
|
->expects($this->any()) |
404
|
|
|
->method('getClassName') |
405
|
|
|
->will($this->returnValue('stdClass')); |
406
|
|
|
$repository |
407
|
|
|
->expects($this->once()) |
408
|
|
|
->method('findOneBy') |
409
|
|
|
->with(['matchKey' => 'matchValue']) |
410
|
|
|
->will($this->returnValue($match)); |
411
|
|
|
|
412
|
|
|
$validator = new UniqueObject([ |
413
|
|
|
'object_repository' => $repository, |
414
|
|
|
'object_manager' => $objectManager, |
415
|
|
|
'fields' => 'matchKey', |
416
|
|
|
'use_context' => true, |
417
|
|
|
]); |
418
|
|
|
$this->assertFalse( |
419
|
|
|
$validator->isValid( |
420
|
|
|
'matchValue', |
421
|
|
|
['matchKey' => 'matchValue', 'id' => 'another identifier'] |
422
|
|
|
) |
423
|
|
|
); |
424
|
|
|
$messageTemplates = $validator->getMessageTemplates(); |
425
|
|
|
|
426
|
|
|
$expectedMessage = str_replace( |
427
|
|
|
'%value%', |
428
|
|
|
'matchValue', |
429
|
|
|
$messageTemplates[UniqueObject::ERROR_OBJECT_NOT_UNIQUE] |
430
|
|
|
); |
431
|
|
|
$messages = $validator->getMessages(); |
432
|
|
|
$receivedMessage = $messages[UniqueObject::ERROR_OBJECT_NOT_UNIQUE]; |
433
|
|
|
$this->assertTrue($expectedMessage == $receivedMessage); |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
|
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: