Completed
Push — master ( 4284f6...258d83 )
by
unknown
09:47
created

testHandleUpdateWorksWhenFormFlushFailed()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 38
rs 8.8571
c 1
b 1
f 0
cc 1
eloc 33
nc 1
nop 0
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)));
0 ignored issues
show
Unused Code introduced by
The call to StateManager::__construct() has too many arguments starting with $this->doctrineHelper.

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.

Loading history...
27
    }
28
29
    public function testHandleUpdateWorksWithValidForm()
30
    {
31
        $entity = $this->getObject();
32
33
        $this->request->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method update() does not exist on Oro\Bundle\FormBundle\Model\UpdateHandler. Did you maybe mean handleUpdate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method update() does not exist on Oro\Bundle\FormBundle\Model\UpdateHandler. Did you maybe mean handleUpdate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\Form\Form>.

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.

Loading history...
142
            ->method('setData')
143
            ->with($entity);
144
        $this->request->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
145
            ->method('getMethod')
146
            ->will($this->returnValue('POST'));
147
        $form->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\Form\Form>.

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.

Loading history...
148
            ->method('submit')
149
            ->with($this->request);
150
        $form->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\Form\Form>.

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.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component\HttpFoundation\Request>.

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.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method update() does not exist on Oro\Bundle\FormBundle\Model\UpdateHandler. Did you maybe mean handleUpdate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OroCRM\Bundl...ndle\Entity\Customer(); (OroCRM\Bundle\MagentoBundle\Entity\Customer) is incompatible with the return type of the parent method Oro\Bundle\FormBundle\Te...eHandlerTest::getObject of type stdClass.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
273
    }
274
}
275