Completed
Pull Request — master (#564)
by Michał
07:58
created

testCanFetchIdentifierFromObjectContext()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 45
rs 8.8571
cc 1
eloc 36
nc 1
nop 0
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 Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\Common\Persistence\ObjectManager;
24
use Doctrine\Common\Persistence\ObjectRepository;
25
use DoctrineModule\Validator\UniqueObject;
26
use Zend\Validator\Exception;
27
28
/**
29
 * Tests for the UniqueObject validator
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Oskar Bley <[email protected]>
34
 */
35
class UniqueObjectTest extends \PHPUnit_Framework_TestCase
36
{
37
    public function testCanValidateWithNotAvailableObjectInRepository()
38
    {
39
        $repository = $this->createMock(ObjectRepository::class);
40
        $repository
41
            ->expects($this->once())
42
            ->method('findOneBy')
43
            ->with(['matchKey' => 'matchValue'])
44
            ->will($this->returnValue(null));
45
46
        $objectManager = $this->createMock(ObjectManager::class);
47
48
        $validator = new UniqueObject([
49
            'object_repository' => $repository,
50
            'object_manager'    => $objectManager,
51
            'fields'            => 'matchKey',
52
        ]);
53
        $this->assertTrue($validator->isValid('matchValue'));
54
    }
55
56
    public function testCanValidateIfThereIsTheSameObjectInTheRepository()
57
    {
58
        $match = new \stdClass();
59
60
        $classMetadata = $this->createMock(ClassMetadata::class);
61
        $classMetadata
62
            ->expects($this->once())
63
            ->method('getIdentifierFieldNames')
64
            ->will($this->returnValue(['id']));
65
        $classMetadata
66
            ->expects($this->once())
67
            ->method('getIdentifierValues')
68
            ->with($match)
69
            ->will($this->returnValue(['id' => 'identifier']));
70
71
        $objectManager = $this->createMock(ObjectManager::class);
72
        $objectManager->expects($this->any())
73
                      ->method('getClassMetadata')
74
                      ->with('stdClass')
75
                      ->will($this->returnValue($classMetadata));
76
77
        $repository = $this->createMock(ObjectRepository::class);
78
        $repository
79
            ->expects($this->any())
80
            ->method('getClassName')
81
            ->will($this->returnValue('stdClass'));
82
        $repository
83
            ->expects($this->once())
84
            ->method('findOneBy')
85
            ->with(['matchKey' => 'matchValue'])
86
            ->will($this->returnValue($match));
87
88
        $validator = new UniqueObject([
89
            'object_repository' => $repository,
90
            'object_manager'    => $objectManager,
91
            'fields'            => 'matchKey',
92
        ]);
93
        $this->assertTrue($validator->isValid(['matchKey' => 'matchValue', 'id' => 'identifier']));
94
    }
95
96
    public function testCannotValidateIfThereIsAnotherObjectWithTheSameValueInTheRepository()
97
    {
98
        $match = new \stdClass();
99
100
        $classMetadata = $this->createMock(ClassMetadata::class);
101
        $classMetadata
102
            ->expects($this->once())
103
            ->method('getIdentifierFieldNames')
104
            ->will($this->returnValue(['id']));
105
        $classMetadata
106
            ->expects($this->once())
107
            ->method('getIdentifierValues')
108
            ->with($match)
109
            ->will($this->returnValue(['id' => 'identifier']));
110
111
        $objectManager = $this->createMock(ObjectManager::class);
112
        $objectManager->expects($this->any())
113
                      ->method('getClassMetadata')
114
                      ->with('stdClass')
115
                      ->will($this->returnValue($classMetadata));
116
117
        $repository = $this->createMock(ObjectRepository::class);
118
        $repository
119
            ->expects($this->any())
120
            ->method('getClassName')
121
            ->will($this->returnValue('stdClass'));
122
        $repository
123
            ->expects($this->once())
124
            ->method('findOneBy')
125
            ->with(['matchKey' => 'matchValue'])
126
            ->will($this->returnValue($match));
127
128
        $validator = new UniqueObject([
129
            'object_repository' => $repository,
130
            'object_manager'    => $objectManager,
131
            'fields'            => 'matchKey',
132
        ]);
133
        $this->assertFalse($validator->isValid(['matchKey' => 'matchValue', 'id' => 'another identifier']));
134
    }
135
136
    public function testCanFetchIdentifierFromContext()
137
    {
138
        $match = new \stdClass();
139
140
        $classMetadata = $this->createMock(ClassMetadata::class);
141
        $classMetadata
142
            ->expects($this->once())
143
            ->method('getIdentifierFieldNames')
144
            ->will($this->returnValue(['id']));
145
        $classMetadata
146
            ->expects($this->once())
147
            ->method('getIdentifierValues')
148
            ->with($match)
149
            ->will($this->returnValue(['id' => 'identifier']));
150
151
        $objectManager = $this->createMock(ObjectManager::class);
152
        $objectManager->expects($this->any())
153
                      ->method('getClassMetadata')
154
                      ->with('stdClass')
155
                      ->will($this->returnValue($classMetadata));
156
157
        $repository = $this->createMock(ObjectRepository::class);
158
        $repository
159
            ->expects($this->any())
160
            ->method('getClassName')
161
            ->will($this->returnValue('stdClass'));
162
        $repository
163
            ->expects($this->once())
164
            ->method('findOneBy')
165
            ->with(['matchKey' => 'matchValue'])
166
            ->will($this->returnValue($match));
167
168
        $validator = new UniqueObject([
169
            'object_repository' => $repository,
170
            'object_manager'    => $objectManager,
171
            'fields'            => 'matchKey',
172
            'use_context'       => true,
173
        ]);
174
        $this->assertTrue($validator->isValid('matchValue', ['id' => 'identifier']));
175
    }
176
177
    public function testThrowsAnExceptionOnUsedButMissingContext()
178
    {
179
        $this->expectException(Exception\RuntimeException::class);
180
        $this->expectExceptionMessage('Expected context to be an array but is null');
181
182
        $match = new \stdClass();
183
184
        $repository = $this->createMock(ObjectRepository::class);
185
        $repository
186
            ->expects($this->once())
187
            ->method('findOneBy')
188
            ->with(['matchKey' => 'matchValue'])
189
            ->will($this->returnValue($match));
190
191
        $objectManager = $this->createMock(ObjectManager::class);
192
193
        $validator = new UniqueObject([
194
            'object_repository' => $repository,
195
            'object_manager'    => $objectManager,
196
            'fields'            => 'matchKey',
197
            'use_context'       => true,
198
        ]);
199
        $validator->isValid('matchValue');
200
    }
201
202
    public function testThrowsAnExceptionOnMissingIdentifier()
203
    {
204
        $this->expectException(Exception\RuntimeException::class);
205
        $this->expectExceptionMessage('Expected context to contain id');
206
207
        $match = new \stdClass();
208
209
        $classMetadata = $this->createMock(ClassMetadata::class);
210
        $classMetadata
211
            ->expects($this->once())
212
            ->method('getIdentifierFieldNames')
213
            ->will($this->returnValue(['id']));
214
215
        $objectManager = $this->createMock(ObjectManager::class);
216
        $objectManager->expects($this->any())
217
                      ->method('getClassMetadata')
218
                      ->with('stdClass')
219
                      ->will($this->returnValue($classMetadata));
220
221
        $repository = $this->createMock(ObjectRepository::class);
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
    public function testThrowsAnExceptionOnMissingIdentifierInContext()
241
    {
242
        $this->expectException(Exception\RuntimeException::class);
243
        $this->expectExceptionMessage('Expected context to contain id');
244
245
        $match = new \stdClass();
246
247
        $classMetadata = $this->createMock(ClassMetadata::class);
248
        $classMetadata
249
            ->expects($this->once())
250
            ->method('getIdentifierFieldNames')
251
            ->will($this->returnValue(['id']));
252
253
        $objectManager = $this->createMock(ObjectManager::class);
254
        $objectManager->expects($this->any())
255
                      ->method('getClassMetadata')
256
                      ->with('stdClass')
257
                      ->will($this->returnValue($classMetadata));
258
259
        $repository = $this->createMock(ObjectRepository::class);
260
        $repository
261
            ->expects($this->any())
262
            ->method('getClassName')
263
            ->will($this->returnValue('stdClass'));
264
        $repository
265
            ->expects($this->once())
266
            ->method('findOneBy')
267
            ->with(['matchKey' => 'matchValue'])
268
            ->will($this->returnValue($match));
269
270
        $validator = new UniqueObject([
271
            'object_repository' => $repository,
272
            'object_manager'    => $objectManager,
273
            'fields'            => 'matchKey',
274
            'use_context'       => true,
275
        ]);
276
        $validator->isValid('matchValue', []);
277
    }
278
279
    public function testThrowsAnExceptionOnMissingObjectManager()
280
    {
281
        $this->expectException(Exception\InvalidArgumentException::class);
282
        $this->expectExceptionMessage(
283
            sprintf(
284
                'Option "object_manager" is required and must be an instance of %s, nothing given',
285
                ObjectManager::class
286
            )
287
        );
288
289
        $repository = $this->createMock(ObjectRepository::class);
290
291
        new UniqueObject([
292
            'object_repository' => $repository,
293
            'fields'            => 'matchKey',
294
        ]);
295
    }
296
297
    public function testThrowsAnExceptionOnWrongObjectManager()
298
    {
299
        $this->expectException(Exception\InvalidArgumentException::class);
300
        $this->expectExceptionMessage(
301
            sprintf(
302
                'Option "object_manager" is required and must be an instance of %s, stdClass given',
303
                ObjectManager::class
304
            )
305
        );
306
307
        $objectManager = new \stdClass();
308
309
        $repository = $this->createMock(ObjectRepository::class);
310
311
        new UniqueObject([
312
            'object_repository' => $repository,
313
            'object_manager'    => $objectManager,
314
            'fields'            => 'matchKey',
315
        ]);
316
    }
317
318
    public function testCanValidateWithNotAvailableObjectInRepositoryByDateTimeObject()
319
    {
320
        $date       = new \DateTime("17 March 2014");
321
        $repository = $this->createMock(ObjectRepository::class);
322
        $repository
323
            ->expects($this->once())
324
            ->method('findOneBy')
325
            ->with(['date' => $date])
326
            ->will($this->returnValue(null));
327
328
        $objectManager = $this->createMock(ObjectManager::class);
329
330
        $validator = new UniqueObject([
331
            'object_repository' => $repository,
332
            'object_manager'    => $objectManager,
333
            'fields'            => 'date',
334
        ]);
335
336
        $this->assertTrue($validator->isValid($date));
337
    }
338
339
    public function testCanFetchIdentifierFromObjectContext()
340
    {
341
        $context     = new \stdClass();
342
        $context->id = 'identifier';
343
344
        $match = new \stdClass();
345
346
        $classMetadata = $this->createMock(ClassMetadata::class);
347
        $classMetadata
348
            ->expects($this->at(0))
349
            ->method('getIdentifierValues')
350
            ->with($context)
351
            ->will($this->returnValue(['id' => 'identifier']));
352
        $classMetadata
353
            ->expects($this->at(1))
354
            ->method('getIdentifierValues')
355
            ->with($match)
356
            ->will($this->returnValue(['id' => 'identifier']));
357
358
        $objectManager = $this->createMock(ObjectManager::class);
359
        $objectManager->expects($this->any())
360
            ->method('getClassMetadata')
361
            ->with('stdClass')
362
            ->will($this->returnValue($classMetadata));
363
364
        $repository = $this->createMock(ObjectRepository::class);
365
        $repository
366
            ->expects($this->any())
367
            ->method('getClassName')
368
            ->will($this->returnValue('stdClass'));
369
        $repository
370
            ->expects($this->once())
371
            ->method('findOneBy')
372
            ->with(['matchKey' => 'matchValue'])
373
            ->will($this->returnValue($match));
374
375
        $validator = new UniqueObject([
376
            'object_repository' => $repository,
377
            'object_manager'    => $objectManager,
378
            'fields'            => 'matchKey',
379
            'use_context'       => true,
380
        ]);
381
382
        $this->assertTrue($validator->isValid('matchValue', $context));
0 ignored issues
show
Documentation introduced by
$context is of type object<stdClass>, but the function expects a array|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
383
    }
384
385
    public function testErrorMessageIsStringInsteadArray()
386
    {
387
        $match = new \stdClass();
388
389
        $classMetadata = $this->createMock(ClassMetadata::class);
390
        $classMetadata
391
            ->expects($this->once())
392
            ->method('getIdentifierFieldNames')
393
            ->will($this->returnValue(['id']));
394
        $classMetadata
395
            ->expects($this->once())
396
            ->method('getIdentifierValues')
397
            ->with($match)
398
            ->will($this->returnValue(['id' => 'identifier']));
399
400
        $objectManager = $this->createMock(ObjectManager::class);
401
        $objectManager->expects($this->any())
402
                      ->method('getClassMetadata')
403
                      ->with('stdClass')
404
                      ->will($this->returnValue($classMetadata));
405
406
        $repository = $this->createMock(ObjectRepository::class);
407
        $repository
408
            ->expects($this->any())
409
            ->method('getClassName')
410
            ->will($this->returnValue('stdClass'));
411
        $repository
412
            ->expects($this->once())
413
            ->method('findOneBy')
414
            ->with(['matchKey' => 'matchValue'])
415
            ->will($this->returnValue($match));
416
417
        $validator = new UniqueObject([
418
            'object_repository' => $repository,
419
            'object_manager'    => $objectManager,
420
            'fields'            => 'matchKey',
421
            'use_context'       => true,
422
        ]);
423
        $this->assertFalse(
424
            $validator->isValid(
425
                'matchValue',
426
                ['matchKey' => 'matchValue', 'id' => 'another identifier']
427
            )
428
        );
429
        $messageTemplates = $validator->getMessageTemplates();
430
431
        $expectedMessage = str_replace(
432
            '%value%',
433
            'matchValue',
434
            $messageTemplates[UniqueObject::ERROR_OBJECT_NOT_UNIQUE]
435
        );
436
        $messages        = $validator->getMessages();
437
        $receivedMessage = $messages[UniqueObject::ERROR_OBJECT_NOT_UNIQUE];
438
        $this->assertTrue($expectedMessage == $receivedMessage);
439
    }
440
}
441