1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\CallBundle\Tests\Unit\Form\Handler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\FormInterface; |
6
|
|
|
use Symfony\Component\Form\FormFactory; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
10
|
|
|
|
11
|
|
|
use Oro\Bundle\UserBundle\Entity\User; |
12
|
|
|
use Oro\Bundle\ActivityBundle\Manager\ActivityManager; |
13
|
|
|
use Oro\Bundle\AddressBundle\Provider\PhoneProviderInterface; |
14
|
|
|
use Oro\Bundle\EntityBundle\Tools\EntityRoutingHelper; |
15
|
|
|
|
16
|
|
|
use OroCRM\Bundle\CallBundle\Entity\Call; |
17
|
|
|
use OroCRM\Bundle\CallBundle\Entity\Manager\CallActivityManager; |
18
|
|
|
use OroCRM\Bundle\CallBundle\Form\Handler\CallHandler; |
19
|
|
|
use OroCRM\Bundle\CallBundle\Tests\Unit\Fixtures\Entity\TestTarget; |
20
|
|
|
|
21
|
|
|
class CallHandlerTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|FormInterface */ |
24
|
|
|
protected $form; |
25
|
|
|
|
26
|
|
|
/** @var Request */ |
27
|
|
|
protected $request; |
28
|
|
|
|
29
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager */ |
30
|
|
|
protected $manager; |
31
|
|
|
|
32
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|PhoneProviderInterface */ |
33
|
|
|
protected $phoneProvider; |
34
|
|
|
|
35
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ActivityManager */ |
36
|
|
|
protected $activityManager; |
37
|
|
|
|
38
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|CallActivityManager */ |
39
|
|
|
protected $callActivityManager; |
40
|
|
|
|
41
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|EntityRoutingHelper */ |
42
|
|
|
protected $entityRoutingHelper; |
43
|
|
|
|
44
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|FormFactory */ |
45
|
|
|
protected $formFactory; |
46
|
|
|
|
47
|
|
|
/** @var CallHandler */ |
48
|
|
|
protected $handler; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Call |
52
|
|
|
*/ |
53
|
|
|
protected $entity; |
54
|
|
|
|
55
|
|
|
protected function setUp() |
56
|
|
|
{ |
57
|
|
|
$this->form = $this->getMockBuilder('Symfony\Component\Form\Form') |
58
|
|
|
->disableOriginalConstructor() |
59
|
|
|
->getMock(); |
60
|
|
|
$this->request = new Request(); |
61
|
|
|
$this->manager = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') |
62
|
|
|
->disableOriginalConstructor() |
63
|
|
|
->getMock(); |
64
|
|
|
$this->phoneProvider = $this->getMock('Oro\Bundle\AddressBundle\Provider\PhoneProviderInterface'); |
65
|
|
|
$this->activityManager = $this->getMockBuilder('Oro\Bundle\ActivityBundle\Manager\ActivityManager') |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->getMock(); |
68
|
|
|
$this->callActivityManager = $this->getMockBuilder( |
69
|
|
|
'OroCRM\Bundle\CallBundle\Entity\Manager\CallActivityManager' |
70
|
|
|
) |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->getMock(); |
73
|
|
|
$this->entityRoutingHelper = $this->getMockBuilder('Oro\Bundle\EntityBundle\Tools\EntityRoutingHelper') |
74
|
|
|
->disableOriginalConstructor() |
75
|
|
|
->getMock(); |
76
|
|
|
$this->formFactory = $this->getMockBuilder('Symfony\Component\Form\FormFactory') |
77
|
|
|
->disableOriginalConstructor() |
78
|
|
|
->getMock(); |
79
|
|
|
|
80
|
|
|
$this->entity = new Call(); |
81
|
|
|
$this->handler = new CallHandler( |
82
|
|
|
'orocrm_call_form', |
83
|
|
|
'orocrm_call_form', |
84
|
|
|
$this->request, |
85
|
|
|
$this->manager, |
86
|
|
|
$this->phoneProvider, |
87
|
|
|
$this->activityManager, |
88
|
|
|
$this->callActivityManager, |
89
|
|
|
$this->entityRoutingHelper, |
90
|
|
|
$this->formFactory |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testProcessWithContexts() |
95
|
|
|
{ |
96
|
|
|
$context = new User(); |
97
|
|
|
$this->setId($context, 123); |
98
|
|
|
|
99
|
|
|
$owner = new User(); |
100
|
|
|
$this->setId($owner, 321); |
101
|
|
|
$this->entity->setOwner($owner); |
102
|
|
|
|
103
|
|
|
$this->request->setMethod('POST'); |
104
|
|
|
|
105
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
106
|
|
|
->method('createNamed') |
107
|
|
|
->with('orocrm_call_form', 'orocrm_call_form', $this->entity, []) |
108
|
|
|
->will($this->returnValue($this->form)); |
109
|
|
|
|
110
|
|
|
$this->form->expects($this->any()) |
|
|
|
|
111
|
|
|
->method('get') |
112
|
|
|
->will($this->returnValue($this->form)); |
113
|
|
|
|
114
|
|
|
$this->form->expects($this->any()) |
|
|
|
|
115
|
|
|
->method('has') |
116
|
|
|
->will($this->returnValue(true)); |
117
|
|
|
|
118
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
119
|
|
|
->method('isValid') |
120
|
|
|
->will($this->returnValue(true)); |
121
|
|
|
|
122
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
123
|
|
|
->method('setData') |
124
|
|
|
->with($this->identicalTo($this->entity)); |
125
|
|
|
|
126
|
|
|
$this->form->expects($this->any()) |
|
|
|
|
127
|
|
|
->method('getData') |
128
|
|
|
->will($this->returnValue([$context])); |
129
|
|
|
|
130
|
|
|
$this->activityManager->expects($this->never()) |
|
|
|
|
131
|
|
|
->method('removeActivityTarget'); |
132
|
|
|
|
133
|
|
|
$this->activityManager->expects($this->once()) |
|
|
|
|
134
|
|
|
->method('setActivityTargets') |
135
|
|
|
->with( |
136
|
|
|
$this->identicalTo($this->entity), |
137
|
|
|
$this->identicalTo([$context, $owner]) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$this->assertTrue( |
141
|
|
|
$this->handler->process($this->entity) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testProcessGetRequestWithoutTargetEntity() |
146
|
|
|
{ |
147
|
|
|
$this->phoneProvider->expects($this->never()) |
|
|
|
|
148
|
|
|
->method('getPhoneNumber'); |
149
|
|
|
$this->entityRoutingHelper->expects($this->never()) |
|
|
|
|
150
|
|
|
->method('getEntity'); |
151
|
|
|
|
152
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
153
|
|
|
->method('createNamed') |
154
|
|
|
->with('orocrm_call_form', 'orocrm_call_form', $this->entity, []) |
155
|
|
|
->will($this->returnValue($this->form)); |
156
|
|
|
|
157
|
|
|
$this->form->expects($this->never()) |
|
|
|
|
158
|
|
|
->method('submit'); |
159
|
|
|
|
160
|
|
|
$this->assertFalse($this->handler->process($this->entity)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testProcessGetRequestWithTargetEntity() |
164
|
|
|
{ |
165
|
|
|
$this->setId($this->entity, 123); |
166
|
|
|
$targetEntity = new TestTarget(123); |
167
|
|
|
$targetEntity1 = new TestTarget(456); |
168
|
|
|
|
169
|
|
|
$this->request->query->set('entityClass', get_class($targetEntity)); |
170
|
|
|
$this->request->query->set('entityId', $targetEntity->getId()); |
171
|
|
|
|
172
|
|
|
$this->phoneProvider->expects($this->never()) |
|
|
|
|
173
|
|
|
->method('getPhoneNumber'); |
174
|
|
|
$this->phoneProvider->expects($this->once()) |
|
|
|
|
175
|
|
|
->method('getPhoneNumbers') |
176
|
|
|
->with($this->identicalTo($targetEntity)) |
177
|
|
|
->will( |
178
|
|
|
$this->returnValue( |
179
|
|
|
[ |
180
|
|
|
['phone1', $targetEntity], |
181
|
|
|
['phone2', $targetEntity], |
182
|
|
|
['phone1', $targetEntity1] |
183
|
|
|
] |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$this->entityRoutingHelper->expects($this->once()) |
|
|
|
|
188
|
|
|
->method('getEntity') |
189
|
|
|
->with(get_class($targetEntity), $targetEntity->getId()) |
190
|
|
|
->will($this->returnValue($targetEntity)); |
191
|
|
|
|
192
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
193
|
|
|
->method('createNamed') |
194
|
|
|
->with( |
195
|
|
|
'orocrm_call_form', |
196
|
|
|
'orocrm_call_form', |
197
|
|
|
$this->entity, |
198
|
|
|
[ |
199
|
|
|
'phone_suggestions' => ['phone1', 'phone2'] |
200
|
|
|
] |
201
|
|
|
) |
202
|
|
|
->will($this->returnValue($this->form)); |
203
|
|
|
|
204
|
|
|
$this->form->expects($this->never()) |
|
|
|
|
205
|
|
|
->method('submit'); |
206
|
|
|
|
207
|
|
|
$this->assertFalse($this->handler->process($this->entity)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testProcessGetRequestWithNewEntity() |
211
|
|
|
{ |
212
|
|
|
$targetEntity = new TestTarget(123); |
213
|
|
|
$targetEntity1 = new TestTarget(456); |
214
|
|
|
|
215
|
|
|
$this->request->query->set('entityClass', get_class($targetEntity)); |
216
|
|
|
$this->request->query->set('entityId', $targetEntity->getId()); |
217
|
|
|
|
218
|
|
|
$this->phoneProvider->expects($this->once()) |
|
|
|
|
219
|
|
|
->method('getPhoneNumber') |
220
|
|
|
->with($this->identicalTo($targetEntity)) |
221
|
|
|
->will($this->returnValue('phone2')); |
222
|
|
|
$this->phoneProvider->expects($this->once()) |
|
|
|
|
223
|
|
|
->method('getPhoneNumbers') |
224
|
|
|
->with($this->identicalTo($targetEntity)) |
225
|
|
|
->will( |
226
|
|
|
$this->returnValue( |
227
|
|
|
[ |
228
|
|
|
['phone1', $targetEntity], |
229
|
|
|
['phone2', $targetEntity], |
230
|
|
|
['phone1', $targetEntity1] |
231
|
|
|
] |
232
|
|
|
) |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
$this->entityRoutingHelper->expects($this->once()) |
|
|
|
|
236
|
|
|
->method('getEntity') |
237
|
|
|
->with(get_class($targetEntity), $targetEntity->getId()) |
238
|
|
|
->will($this->returnValue($targetEntity)); |
239
|
|
|
|
240
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
241
|
|
|
->method('createNamed') |
242
|
|
|
->with( |
243
|
|
|
'orocrm_call_form', |
244
|
|
|
'orocrm_call_form', |
245
|
|
|
$this->entity, |
246
|
|
|
[ |
247
|
|
|
'phone_suggestions' => ['phone1', 'phone2'] |
248
|
|
|
] |
249
|
|
|
) |
250
|
|
|
->will($this->returnValue($this->form)); |
251
|
|
|
|
252
|
|
|
$this->form->expects($this->never()) |
|
|
|
|
253
|
|
|
->method('submit'); |
254
|
|
|
|
255
|
|
|
$this->assertFalse($this->handler->process($this->entity)); |
256
|
|
|
$this->assertEquals('phone2', $this->entity->getPhoneNumber()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @dataProvider supportedMethods |
261
|
|
|
* |
262
|
|
|
* @param string $method |
263
|
|
|
*/ |
264
|
|
|
public function testProcessInvalidData($method) |
265
|
|
|
{ |
266
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
267
|
|
|
->method('createNamed') |
268
|
|
|
->with('orocrm_call_form', 'orocrm_call_form', $this->entity, []) |
269
|
|
|
->will($this->returnValue($this->form)); |
270
|
|
|
|
271
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
272
|
|
|
->method('submit') |
273
|
|
|
->with($this->request); |
274
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
275
|
|
|
->method('isValid') |
276
|
|
|
->will($this->returnValue(false)); |
277
|
|
|
|
278
|
|
|
$this->phoneProvider->expects($this->never()) |
|
|
|
|
279
|
|
|
->method('getPhoneNumber'); |
280
|
|
|
$this->entityRoutingHelper->expects($this->never()) |
|
|
|
|
281
|
|
|
->method('getEntity'); |
282
|
|
|
$this->entityRoutingHelper->expects($this->never()) |
|
|
|
|
283
|
|
|
->method('getEntityReference'); |
284
|
|
|
$this->callActivityManager->expects($this->never()) |
285
|
|
|
->method('addAssociation'); |
286
|
|
|
|
287
|
|
|
$this->request->setMethod($method); |
288
|
|
|
|
289
|
|
|
$this->assertFalse($this->handler->process($this->entity)); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @dataProvider supportedMethods |
294
|
|
|
* |
295
|
|
|
* @param string $method |
296
|
|
|
*/ |
297
|
|
|
public function testProcessValidDataWithoutTargetEntity($method) |
298
|
|
|
{ |
299
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
300
|
|
|
->method('createNamed') |
301
|
|
|
->with('orocrm_call_form', 'orocrm_call_form', $this->entity, []) |
302
|
|
|
->will($this->returnValue($this->form)); |
303
|
|
|
|
304
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
305
|
|
|
->method('submit') |
306
|
|
|
->with($this->request); |
307
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
308
|
|
|
->method('isValid') |
309
|
|
|
->will($this->returnValue(true)); |
310
|
|
|
|
311
|
|
|
$this->phoneProvider->expects($this->never()) |
|
|
|
|
312
|
|
|
->method('getPhoneNumber'); |
313
|
|
|
$this->entityRoutingHelper->expects($this->never()) |
|
|
|
|
314
|
|
|
->method('getEntity'); |
315
|
|
|
$this->entityRoutingHelper->expects($this->never()) |
|
|
|
|
316
|
|
|
->method('getEntityReference'); |
317
|
|
|
$this->callActivityManager->expects($this->never()) |
318
|
|
|
->method('addAssociation'); |
319
|
|
|
|
320
|
|
|
$this->manager->expects($this->once()) |
|
|
|
|
321
|
|
|
->method('persist') |
322
|
|
|
->with($this->entity); |
323
|
|
|
|
324
|
|
|
$this->manager->expects($this->once()) |
|
|
|
|
325
|
|
|
->method('flush'); |
326
|
|
|
|
327
|
|
|
$this->request->setMethod($method); |
328
|
|
|
|
329
|
|
|
$this->assertTrue($this->handler->process($this->entity)); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @dataProvider supportedMethods |
334
|
|
|
* |
335
|
|
|
* @param string $method |
336
|
|
|
*/ |
337
|
|
|
public function testProcessValidDataWithTargetEntity($method) |
338
|
|
|
{ |
339
|
|
|
$this->entity->setPhoneNumber('phone1'); |
340
|
|
|
|
341
|
|
|
$targetEntity = new TestTarget(123); |
342
|
|
|
$targetEntity1 = new TestTarget(456); |
343
|
|
|
|
344
|
|
|
$this->request->query->set('entityClass', get_class($targetEntity)); |
345
|
|
|
$this->request->query->set('entityId', $targetEntity->getId()); |
346
|
|
|
|
347
|
|
|
$this->formFactory->expects($this->once()) |
|
|
|
|
348
|
|
|
->method('createNamed') |
349
|
|
|
->with('orocrm_call_form', 'orocrm_call_form', $this->entity, []) |
350
|
|
|
->will($this->returnValue($this->form)); |
351
|
|
|
|
352
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
353
|
|
|
->method('submit') |
354
|
|
|
->with($this->request); |
355
|
|
|
$this->form->expects($this->once()) |
|
|
|
|
356
|
|
|
->method('isValid') |
357
|
|
|
->will($this->returnValue(true)); |
358
|
|
|
|
359
|
|
|
$this->phoneProvider->expects($this->never()) |
|
|
|
|
360
|
|
|
->method('getPhoneNumber'); |
361
|
|
|
$this->phoneProvider->expects($this->once()) |
|
|
|
|
362
|
|
|
->method('getPhoneNumbers') |
363
|
|
|
->with($this->identicalTo($targetEntity)) |
364
|
|
|
->will( |
365
|
|
|
$this->returnValue( |
366
|
|
|
[ |
367
|
|
|
['phone1', $targetEntity], |
368
|
|
|
['phone2', $targetEntity], |
369
|
|
|
['phone1', $targetEntity1] |
370
|
|
|
] |
371
|
|
|
) |
372
|
|
|
); |
373
|
|
|
|
374
|
|
|
$this->entityRoutingHelper->expects($this->once()) |
|
|
|
|
375
|
|
|
->method('getEntity') |
376
|
|
|
->with(get_class($targetEntity), $targetEntity->getId()) |
377
|
|
|
->will($this->returnValue($targetEntity)); |
378
|
|
|
// phone1, $targetEntity |
379
|
|
|
$this->callActivityManager->expects($this->at(0)) |
380
|
|
|
->method('addAssociation') |
381
|
|
|
->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity)); |
382
|
|
|
// phone2, $targetEntity |
383
|
|
|
$this->callActivityManager->expects($this->at(1)) |
384
|
|
|
->method('addAssociation') |
385
|
|
|
->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity)); |
386
|
|
|
// phone1, $targetEntity1 |
387
|
|
|
$this->callActivityManager->expects($this->at(2)) |
388
|
|
|
->method('addAssociation') |
389
|
|
|
->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity1)); |
390
|
|
|
|
391
|
|
|
$this->manager->expects($this->once()) |
|
|
|
|
392
|
|
|
->method('persist') |
393
|
|
|
->with($this->entity); |
394
|
|
|
|
395
|
|
|
$this->manager->expects($this->once()) |
|
|
|
|
396
|
|
|
->method('flush'); |
397
|
|
|
|
398
|
|
|
$this->request->setMethod($method); |
399
|
|
|
|
400
|
|
|
$this->assertTrue($this->handler->process($this->entity)); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
public function supportedMethods() |
404
|
|
|
{ |
405
|
|
|
return array( |
406
|
|
|
array('POST'), |
407
|
|
|
array('PUT') |
408
|
|
|
); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param mixed $obj |
413
|
|
|
* @param mixed $val |
414
|
|
|
*/ |
415
|
|
View Code Duplication |
protected function setId($obj, $val) |
|
|
|
|
416
|
|
|
{ |
417
|
|
|
$class = new \ReflectionClass($obj); |
418
|
|
|
$prop = $class->getProperty('id'); |
419
|
|
|
$prop->setAccessible(true); |
420
|
|
|
|
421
|
|
|
$prop->setValue($obj, $val); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.