UniqueObjectTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 270
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A getValidator() 0 9 1
A testValidatorPassesIfObjectNotExist() 0 15 1
A testValidatorPassesIfObjectNotExistWithNoContext() 0 20 1
A testValidatorFailsIfObjectExistsAndIsNotTheSameId() 0 47 1
A testValidatorPassesIfObjectExistsAndHasSameId() 0 47 1
B testValidatorPassesIfObjectExistsAndHasTheSameIds() 0 43 1
A testValidatorPassesIfObjectExistsAndHasTheSameIdsWithObjectAsId() 0 70 1
1
<?php
2
3
namespace JhFlexiTimeTest\Validator;
4
5
use JhFlexiTime\Validator\UniqueObject;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use JhUser\Entity\User;
9
10
/**
11
 * Class UniqueObjectTest
12
 * @package JhFlexiTimeTest\Validator
13
 * @author Aydin Hassan <[email protected]>
14
 */
15
class UniqueObjectTest extends \PHPUnit_Framework_TestCase
16
{
17
    protected $validator;
18
    protected $objectManager;
19
    protected $repository;
20
21
    public function setUp()
22
    {
23
        $this->objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
24
        $this->repository    = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
25
    }
26
27
    protected function getValidator($fields, $useContext = true)
28
    {
29
        return $this->validator = new UniqueObject([
30
            'object_manager'    => $this->objectManager,
31
            'object_repository' => $this->repository,
32
            'fields'            => $fields,
33
            'use_context'       => $useContext,
34
        ]);
35
    }
36
37
    public function testValidatorPassesIfObjectNotExist()
38
    {
39
        $validator  = $this->getValidator(['date'], false);
40
41
        $date = '12-04-88';
42
        $user = 2;
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
44
        $this->repository
45
            ->expects($this->once())
46
            ->method('findOneBy')
47
            ->with(['date' => $date])
48
            ->will($this->returnValue(null));
49
50
        $this->assertTrue($validator->isValid('12-04-88'));
51
    }
52
53
    public function testValidatorPassesIfObjectNotExistWithNoContext()
54
    {
55
        $validator  = $this->getValidator(['date', 'user']);
56
57
        $date = '12-04-88';
58
        $user = 2;
59
60
        $this->repository
61
            ->expects($this->once())
62
            ->method('findOneBy')
63
            ->with(['user' => $user, 'date' => $date])
64
            ->will($this->returnValue(null));
65
66
        $context = [
67
            'user' => $user,
68
            'date' => $date,
69
        ];
70
71
        $this->assertTrue($validator->isValid('12-04-88', $context));
72
    }
73
74
    public function testValidatorFailsIfObjectExistsAndIsNotTheSameId()
75
    {
76
        $validator  = $this->getValidator(['someField', 'someOtherField']);
77
78
        $context = [
79
            'someField'         => 1,
80
            'someOtherField'    => 2,
81
            'id'                => 1,
82
        ];
83
84
        $match = [
85
            'someField'         => 1,
86
            'someOtherField'    => 2,
87
            'id'                => 2,
88
        ];
89
90
        $match  = (object) $match;
91
92
        $classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
93
        $classMetadata
94
            ->expects($this->once())
95
            ->method('getIdentifierFieldNames')
96
            ->will($this->returnValue(['id']));
97
        $classMetadata
98
            ->expects($this->once())
99
            ->method('getIdentifierValues')
100
            ->with($match)
101
            ->will($this->returnValue(['id' => 2]));
102
103
        $this->objectManager->expects($this->any())
104
            ->method('getClassMetadata')
105
            ->with('stdClass')
106
            ->will($this->returnValue($classMetadata));
107
108
        $this->repository
109
            ->expects($this->any())
110
            ->method('getClassName')
111
            ->will($this->returnValue('stdClass'));
112
113
        $this->repository
114
            ->expects($this->once())
115
            ->method('findOneBy')
116
            ->with(['someField' => 1, 'someOtherField' => 2])
117
            ->will($this->returnValue($match));
118
119
        $this->assertFalse($validator->isValid('12-04-88', $context));
120
    }
121
122
    public function testValidatorPassesIfObjectExistsAndHasSameId()
123
    {
124
        $validator  = $this->getValidator(['someField', 'someOtherField']);
125
126
        $context = [
127
            'someField'         => 1,
128
            'someOtherField'    => 2,
129
            'id'                => 1,
130
        ];
131
132
        $match = [
133
            'someField'         => 1,
134
            'someOtherField'    => 2,
135
            'id'                => 1,
136
        ];
137
138
        $match  = (object) $match;
139
140
        $classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
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' => 1]));
150
151
        $this->objectManager->expects($this->any())
152
            ->method('getClassMetadata')
153
            ->with('stdClass')
154
            ->will($this->returnValue($classMetadata));
155
156
        $this->repository
157
            ->expects($this->any())
158
            ->method('getClassName')
159
            ->will($this->returnValue('stdClass'));
160
161
        $this->repository
162
            ->expects($this->once())
163
            ->method('findOneBy')
164
            ->with(['someField' => 1, 'someOtherField' => 2])
165
            ->will($this->returnValue($match));
166
167
        $this->assertTrue($validator->isValid('12-04-88', $context));
168
    }
169
170
    public function testValidatorPassesIfObjectExistsAndHasTheSameIds()
171
    {
172
        $validator  = $this->getValidator(['user', 'date']);
173
174
        $date = '12-04-88';
175
        $user = 2;
176
177
        $context = [
178
            'user' => $user,
179
            'date' => $date,
180
        ];
181
182
        $object = (object) $context;
183
184
        $classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
185
        $classMetadata
186
            ->expects($this->once())
187
            ->method('getIdentifierFieldNames')
188
            ->will($this->returnValue(['user', 'date']));
189
        $classMetadata
190
            ->expects($this->once())
191
            ->method('getIdentifierValues')
192
            ->with($object)
193
            ->will($this->returnValue(['user' => $user, 'date' => $date]));
194
195
        $this->objectManager->expects($this->any())
196
            ->method('getClassMetadata')
197
            ->with('stdClass')
198
            ->will($this->returnValue($classMetadata));
199
200
        $this->repository
201
            ->expects($this->any())
202
            ->method('getClassName')
203
            ->will($this->returnValue('stdClass'));
204
205
        $this->repository
206
            ->expects($this->once())
207
            ->method('findOneBy')
208
            ->with(['user' => $user, 'date' => $date])
209
            ->will($this->returnValue($object));
210
211
        $this->assertTrue($validator->isValid('12-04-88', $context));
212
    }
213
214
    public function testValidatorPassesIfObjectExistsAndHasTheSameIdsWithObjectAsId()
215
    {
216
        $validator  = $this->getValidator(['user', 'date']);
217
218
        $date = new \JhFlexiTime\DateTime\DateTime("12/04/88");
219
220
        $user = new User;
221
222
        $context = [
223
            'user' => 2,
224
            'date' => $date,
225
        ];
226
227
        $object = (object) $context;
228
229
        $foundIdentifiers = [
230
            'user' => $user,
231
            'date' => $date,
232
        ];
233
234
        $bookingClassMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
235
        $bookingClassMetadata
236
            ->expects($this->once())
237
            ->method('getIdentifierValues')
238
            ->with($object)
239
            ->will($this->returnValue($foundIdentifiers));
240
241
        $bookingClassMetadata
242
            ->expects($this->once())
243
            ->method('getIdentifierFieldNames')
244
            ->will($this->returnValue(['user', 'date']));
245
246
247
        $userIdValues = [0 => 2];
248
        $userClassMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
249
        $userClassMetadata
250
            ->expects($this->once())
251
            ->method('getIdentifierValues')
252
            ->with($user)
253
            ->will($this->returnValue($userIdValues));
254
255
256
        $metaMap = [
257
            ['stdClass', $bookingClassMetadata],
258
            ['JhUser\Entity\User', $userClassMetadata],
259
        ];
260
261
        $this->objectManager->expects($this->any())
262
            ->method('getClassMetadata')
263
            ->will($this->returnValueMap($metaMap));
264
265
        $e = new \Doctrine\Common\Persistence\Mapping\MappingException();
266
        $this->objectManager->expects($this->at(3))
267
            ->method('getClassMetadata')
268
            ->with('JhFlexiTime\DateTime\DateTime')
269
            ->will($this->throwException($e));
270
271
        $this->repository
272
            ->expects($this->any())
273
            ->method('getClassName')
274
            ->will($this->returnValue('stdClass'));
275
276
        $this->repository
277
            ->expects($this->once())
278
            ->method('findOneBy')
279
            ->with(['user' => 2, 'date' => $date])
280
            ->will($this->returnValue($object));
281
282
        $this->assertTrue($validator->isValid('12-04-88', $context));
283
    }
284
}
285