1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\Form\Handler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\Form; |
6
|
|
|
|
7
|
|
|
use OroCRM\Bundle\MagentoBundle\Service\CustomerStateHandler; |
8
|
|
|
use OroCRM\Bundle\MagentoBundle\Service\StateManager; |
9
|
|
|
use Oro\Bundle\FormBundle\Tests\Unit\Model\UpdateHandlerTest; |
10
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
11
|
|
|
use OroCRM\Bundle\MagentoBundle\Form\Handler\CustomerHandler; |
12
|
|
|
|
13
|
|
|
class CustomerHandlerTest extends UpdateHandlerTest |
14
|
|
|
{ |
15
|
|
|
protected function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
|
19
|
|
|
$this->handler = new CustomerHandler( |
20
|
|
|
$this->request, |
21
|
|
|
$this->session, |
22
|
|
|
$this->router, |
23
|
|
|
$this->doctrineHelper, |
24
|
|
|
$this->eventDispatcher |
25
|
|
|
); |
26
|
|
|
$this->handler->setStateHandler(new CustomerStateHandler(new StateManager($this->doctrineHelper))); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testHandleUpdateWorksWithValidForm() |
30
|
|
|
{ |
31
|
|
|
$entity = $this->getObject(); |
32
|
|
|
|
33
|
|
|
$this->request->expects($this->once()) |
|
|
|
|
34
|
|
|
->method('getMethod') |
35
|
|
|
->will($this->returnValue('POST')); |
36
|
|
|
$this->form->expects($this->once()) |
37
|
|
|
->method('submit') |
38
|
|
|
->with($this->request); |
39
|
|
|
$this->form->expects($this->once()) |
40
|
|
|
->method('isValid') |
41
|
|
|
->will($this->returnValue(true)); |
42
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
43
|
|
|
->disableOriginalConstructor() |
44
|
|
|
->getMock(); |
45
|
|
|
$em->expects($this->atLeastOnce()) |
46
|
|
|
->method('persist'); |
47
|
|
|
|
48
|
|
|
$em->expects($this->atLeastOnce()) |
49
|
|
|
->method('flush'); |
50
|
|
|
$this->doctrineHelper->expects($this->atLeastOnce()) |
51
|
|
|
->method('getEntityManager') |
52
|
|
|
->with($entity) |
53
|
|
|
->will($this->returnValue($em)); |
54
|
|
|
$this->doctrineHelper->expects($this->once()) |
55
|
|
|
->method('getSingleEntityIdentifier') |
56
|
|
|
->with($entity) |
57
|
|
|
->will($this->returnValue(1)); |
58
|
|
|
|
59
|
|
|
$expected = $this->getExpectedSaveData($this->form, $entity); |
60
|
|
|
$expected['savedId'] = 1; |
61
|
|
|
|
62
|
|
|
$result = $this->handler->handleUpdate( |
63
|
|
|
$entity, |
64
|
|
|
$this->form, |
65
|
|
|
['route' => 'test_update'], |
66
|
|
|
['route' => 'test_view'], |
67
|
|
|
'Saved' |
68
|
|
|
); |
69
|
|
|
$this->assertEquals($expected, $result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testUpdateWithInvalidForm() |
73
|
|
|
{ |
74
|
|
|
$data = $this->getObject(); |
75
|
|
|
$this->form->expects($this->once()) |
76
|
|
|
->method('setData') |
77
|
|
|
->with($data); |
78
|
|
|
$this->request->expects($this->once()) |
|
|
|
|
79
|
|
|
->method('getMethod') |
80
|
|
|
->will($this->returnValue('POST')); |
81
|
|
|
$this->form->expects($this->once()) |
82
|
|
|
->method('submit') |
83
|
|
|
->with($this->request); |
84
|
|
|
$this->form->expects($this->once()) |
85
|
|
|
->method('isValid') |
86
|
|
|
->will($this->returnValue(false)); |
87
|
|
|
|
88
|
|
|
$expected = $this->getExpectedSaveData($this->form, $data); |
89
|
|
|
|
90
|
|
|
$result = $this->handler->update($data, $this->form, 'Saved'); |
|
|
|
|
91
|
|
|
$this->assertEquals($expected, $result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testUpdateWorksWithValidForm() |
95
|
|
|
{ |
96
|
|
|
$data = $this->getObject(); |
97
|
|
|
$this->form->expects($this->once()) |
98
|
|
|
->method('setData') |
99
|
|
|
->with($data); |
100
|
|
|
$this->request->expects($this->once()) |
|
|
|
|
101
|
|
|
->method('getMethod') |
102
|
|
|
->will($this->returnValue('POST')); |
103
|
|
|
$this->form->expects($this->once()) |
104
|
|
|
->method('submit') |
105
|
|
|
->with($this->request); |
106
|
|
|
$this->form->expects($this->once()) |
107
|
|
|
->method('isValid') |
108
|
|
|
->will($this->returnValue(true)); |
109
|
|
|
|
110
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
111
|
|
|
->disableOriginalConstructor() |
112
|
|
|
->getMock(); |
113
|
|
|
$em->expects($this->exactly(2)) |
114
|
|
|
->method('persist') |
115
|
|
|
->with($data); |
116
|
|
|
$em->expects($this->exactly(2)) |
117
|
|
|
->method('flush'); |
118
|
|
|
$this->doctrineHelper->expects($this->exactly(2)) |
119
|
|
|
->method('getEntityManager') |
120
|
|
|
->with($data) |
121
|
|
|
->will($this->returnValue($em)); |
122
|
|
|
$this->doctrineHelper->expects($this->once()) |
123
|
|
|
->method('getSingleEntityIdentifier') |
124
|
|
|
->with($data) |
125
|
|
|
->will($this->returnValue(1)); |
126
|
|
|
|
127
|
|
|
$expected = $this->getExpectedSaveData($this->form, $data); |
128
|
|
|
$expected['savedId'] = 1; |
129
|
|
|
|
130
|
|
|
$result = $this->handler->update($data, $this->form, 'Saved'); |
|
|
|
|
131
|
|
|
$this->assertEquals($expected, $result); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testHandleUpdateWorksWithInvalidForm() |
135
|
|
|
{ |
136
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|Form $form */ |
137
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
138
|
|
|
->disableOriginalConstructor() |
139
|
|
|
->getMock(); |
140
|
|
|
$entity = $this->getObject(); |
141
|
|
|
$form->expects($this->once()) |
|
|
|
|
142
|
|
|
->method('setData') |
143
|
|
|
->with($entity); |
144
|
|
|
$this->request->expects($this->once()) |
|
|
|
|
145
|
|
|
->method('getMethod') |
146
|
|
|
->will($this->returnValue('POST')); |
147
|
|
|
$form->expects($this->once()) |
|
|
|
|
148
|
|
|
->method('submit') |
149
|
|
|
->with($this->request); |
150
|
|
|
$form->expects($this->once()) |
|
|
|
|
151
|
|
|
->method('isValid') |
152
|
|
|
->will($this->returnValue(false)); |
153
|
|
|
|
154
|
|
|
$expected = $this->getExpectedSaveData($form, $entity); |
155
|
|
|
|
156
|
|
|
$result = $this->handler->handleUpdate( |
157
|
|
|
$entity, |
158
|
|
|
$form, |
159
|
|
|
['route' => 'test_update'], |
160
|
|
|
['route' => 'test_view'], |
161
|
|
|
'Saved' |
162
|
|
|
); |
163
|
|
|
$this->assertEquals($expected, $result); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @expectedException \Exception |
168
|
|
|
* @expectedExceptionMessage Test flush exception |
169
|
|
|
*/ |
170
|
|
|
public function testHandleUpdateWorksWhenFormFlushFailed() |
171
|
|
|
{ |
172
|
|
|
$entity = $this->getObject(); |
173
|
|
|
$this->form->expects($this->once()) |
174
|
|
|
->method('setData') |
175
|
|
|
->with($entity); |
176
|
|
|
$this->request->expects($this->once()) |
|
|
|
|
177
|
|
|
->method('getMethod') |
178
|
|
|
->will($this->returnValue('POST')); |
179
|
|
|
$this->form->expects($this->once()) |
180
|
|
|
->method('submit') |
181
|
|
|
->with($this->request); |
182
|
|
|
$this->form->expects($this->once()) |
183
|
|
|
->method('isValid') |
184
|
|
|
->will($this->returnValue(true)); |
185
|
|
|
|
186
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
187
|
|
|
->disableOriginalConstructor() |
188
|
|
|
->getMock(); |
189
|
|
|
$em->expects($this->once()) |
190
|
|
|
->method('persist') |
191
|
|
|
->with($entity); |
192
|
|
|
$em->expects($this->once()) |
193
|
|
|
->method('flush') |
194
|
|
|
->willThrowException(new \Exception('Test flush exception')); |
195
|
|
|
$this->doctrineHelper->expects($this->once()) |
196
|
|
|
->method('getEntityManager') |
197
|
|
|
->with($entity) |
198
|
|
|
->will($this->returnValue($em)); |
199
|
|
|
|
200
|
|
|
$this->handler->handleUpdate( |
201
|
|
|
$entity, |
202
|
|
|
$this->form, |
203
|
|
|
['route' => 'test_update'], |
204
|
|
|
['route' => 'test_view'], |
205
|
|
|
'Saved' |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @expectedException \Exception |
211
|
|
|
* @expectedExceptionMessage Test flush exception |
212
|
|
|
*/ |
213
|
|
|
public function testUpdateWorksWhenFormFlushFailed() |
214
|
|
|
{ |
215
|
|
|
$data = $this->getObject(); |
216
|
|
|
$this->form->expects($this->once()) |
217
|
|
|
->method('setData') |
218
|
|
|
->with($data); |
219
|
|
|
$this->request->expects($this->once()) |
|
|
|
|
220
|
|
|
->method('getMethod') |
221
|
|
|
->will($this->returnValue('POST')); |
222
|
|
|
$this->form->expects($this->once()) |
223
|
|
|
->method('submit') |
224
|
|
|
->with($this->request); |
225
|
|
|
$this->form->expects($this->once()) |
226
|
|
|
->method('isValid') |
227
|
|
|
->will($this->returnValue(true)); |
228
|
|
|
|
229
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
230
|
|
|
->disableOriginalConstructor() |
231
|
|
|
->getMock(); |
232
|
|
|
$em->expects($this->once()) |
233
|
|
|
->method('persist') |
234
|
|
|
->with($data); |
235
|
|
|
$em->expects($this->once()) |
236
|
|
|
->method('flush') |
237
|
|
|
->willThrowException(new \Exception('Test flush exception')); |
238
|
|
|
$this->doctrineHelper->expects($this->once()) |
239
|
|
|
->method('getEntityManager') |
240
|
|
|
->with($data) |
241
|
|
|
->will($this->returnValue($em)); |
242
|
|
|
|
243
|
|
|
$this->handler->update($data, $this->form, 'Saved'); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Test should not be called because handler does not trigger form events |
248
|
|
|
*/ |
249
|
|
|
public function testHandleUpdateBeforeFormDataSetInterrupted() |
250
|
|
|
{ |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Test should not be called because handler does not trigger form events |
255
|
|
|
*/ |
256
|
|
|
public function testHandleUpdateInterruptedBeforeFormSubmit() |
257
|
|
|
{ |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Test should not be called because handler does not trigger form events |
262
|
|
|
*/ |
263
|
|
|
public function testUpdateInterruptedBeforeFormSubmit() |
264
|
|
|
{ |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return object |
269
|
|
|
*/ |
270
|
|
|
protected function getObject() |
271
|
|
|
{ |
272
|
|
|
return new Customer(); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.