Completed
Push — 1.9 ( 5c3e2e...1529fd )
by
unknown
60:20
created

CartStrategyTest::contactInfoDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 148
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
dl 0
loc 148
rs 8.2857
c 3
b 3
f 0
cc 1
eloc 88
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\ImportExport\Strategy;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
8
use Symfony\Component\PropertyAccess\PropertyAccess;
9
10
use Oro\Bundle\AddressBundle\Entity\Country;
11
use Oro\Bundle\IntegrationBundle\Entity\Channel;
12
use Oro\Bundle\ImportExportBundle\Context\ContextInterface;
13
14
use OroCRM\Bundle\MagentoBundle\Entity\CartAddress;
15
use OroCRM\Bundle\MagentoBundle\Entity\CartItem;
16
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
17
use OroCRM\Bundle\MagentoBundle\Entity\CartStatus;
18
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
19
use OroCRM\Bundle\MagentoBundle\ImportExport\Strategy\CartStrategy;
20
21
class CartStrategyTest extends AbstractStrategyTest
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function getStrategy()
27
    {
28
        $strategy = new CartStrategy(
29
            $this->eventDispatcher,
30
            $this->strategyHelper,
31
            $this->fieldHelper,
32
            $this->databaseHelper
33
        );
34
35
        $strategy->setOwnerHelper($this->defaultOwnerHelper);
36
        $strategy->setLogger($this->logger);
37
        $strategy->setChannelHelper($this->channelHelper);
38
        $strategy->setAddressHelper($this->addressHelper);
39
40
        return $strategy;
41
    }
42
43
    /**
44
     * @param mixed $expected
45
     * @param mixed $entity
46
     * @param mixed $databaseEntity
47
     *
48
     * @dataProvider contactInfoDataProvider
49
     */
50
    public function testContactInfo($expected, $entity, $databaseEntity = null)
51
    {
52
        $strategy = $this->getStrategy();
53
54
        /** @var \PHPUnit_Framework_MockObject_MockObject|ContextInterface $context */
55
        $context = $this->getMock('Oro\Bundle\ImportExportBundle\Context\ContextInterface');
56
        $strategy->setImportExportContext($context);
57
        $strategy->setEntityName('OroCRM\Bundle\MagentoBundle\Entity\Cart');
58
59
        $execution = $this->getMock('Akeneo\Bundle\BatchBundle\Item\ExecutionContext');
60
        $this->jobExecution->expects($this->any())->method('getExecutionContext')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Akeneo\Bundle\BatchBundle\Entity\JobExecution.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
            ->will($this->returnValue($execution));
62
        $strategy->setStepExecution($this->stepExecution);
63
64
        $this->databaseHelper->expects($this->once())->method('getEntityReference')->will($this->returnArgument(0));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Oro\Bundle\ImportExportBundle\Field\DatabaseHelper.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
        $this->databaseHelper->expects($this->once())->method('findOneByIdentity')->willReturn($databaseEntity);
66
67
        $actualEntity = $strategy->process($entity);
68
        if ($actualEntity) {
69
            $expected->setImportedAt($actualEntity->getImportedAt());
70
            $expected->setSyncedAt($actualEntity->getSyncedAt());
71
        }
72
73
        $this->assertEquals($expected, $actualEntity);
74
    }
75
76
    /**
77
     * @return array
78
     *
79
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
80
     */
81
    public function contactInfoDataProvider()
82
    {
83
        return [
84
            'items count' => [null, $this->getEntity()],
85
            'without contact info' => [null, $this->getEntity(['itemsCount' => 1])],
86
            'email' => [
87
                $this->getEntity(['itemsCount' => 1, 'email' => '[email protected]']),
88
                $this->getEntity(['itemsCount' => 1, 'email' => '[email protected]']),
89
90
            ],
91
            'dont change status' => [
92
                $this->getEntity(
93
                    ['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('custom')]
94
                ),
95
                $this->getEntity(
96
                    ['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('custom')]
97
                ),
98
                $this->getEntity(
99
                    ['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('custom')]
100
                )
101
            ],
102
            'change status' => [
103
                'expected' => $this->getEntity(
104
                    ['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('expired')]
105
                ),
106
                'entity' => $this->getEntity(
107
                    ['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('expired')]
108
                ),
109
                'databaseEntity' => $this->getEntity(
110
                    ['itemsCount' => 1, 'email' => '[email protected]', 'status' => new CartStatus('open')]
111
                )
112
            ],
113
            'update customer email' => [
114
                'expected' => $this->getEntity(
115
                    [
116
                        'itemsCount' => 1,
117
                        'email' => '[email protected]',
118
                        'customer' => $this->getCustomer('[email protected]')
119
                    ]
120
                ),
121
                'entity' => $this->getEntity(
122
                    [
123
                        'itemsCount' => 1,
124
                        'email' => '[email protected]',
125
                        'customer' => $this->getCustomer()
126
                    ]
127
                ),
128
                'databaseEntity' => $this->getEntity(
129
                    [
130
                        'itemsCount' => 1,
131
                        'email' => '[email protected]',
132
                        'customer' => $this->getCustomer('[email protected]')
133
                    ]
134
                )
135
            ],
136
            'add new cart item and remove old' => [
137
                'expected' => $this->getEntity(
138
                    [
139
                        'itemsCount' => 1,
140
                        'email' => '[email protected]',
141
                        'cartItems' => new ArrayCollection([$this->getCartItem(1)]),
142
                        'itemsQty' => 1,
143
                    ]
144
                ),
145
                'entity' => $this->getEntity(
146
                    [
147
                        'itemsCount' => 1,
148
                        'email' => '[email protected]',
149
                        'cartItems' => new ArrayCollection([$this->getCartItem(1)]),
150
                        'itemsQty' => 1,
151
                    ]
152
                ),
153
                'databaseEntity' => $this->getEntity(
154
                    [
155
                        'itemsCount' => 1,
156
                        'email' => '[email protected]',
157
                        'cartItems' => new ArrayCollection([$this->getCartItem(2)]),
158
                        'itemsQty' => 1,
159
                    ]
160
                )
161
            ],
162
            'add new cart item and keep old one' => [
163
                'expected' => $this->getEntity(
164
                    [
165
                        'itemsCount' => 1,
166
                        'email' => '[email protected]',
167
                        'cartItems' => new ArrayCollection([$this->getCartItem(1), $this->getCartItem(2)]),
168
                        'itemsQty' => 2,
169
                    ]
170
                ),
171
                'entity' => $this->getEntity(
172
                    [
173
                        'itemsCount' => 1,
174
                        'email' => '[email protected]',
175
                        'cartItems' => new ArrayCollection([$this->getCartItem(1), $this->getCartItem(2)]),
176
                        'itemsQty' => 2,
177
                    ]
178
                ),
179
                'databaseEntity' => $this->getEntity(
180
                    [
181
                        'itemsCount' => 1,
182
                        'email' => '[email protected]',
183
                        'cartItems' => new ArrayCollection([$this->getCartItem(2)]),
184
                        'itemsQty' => 1,
185
                    ]
186
                )
187
            ],
188
            'update existing address' => [
189
                'expected' => $this->getEntity(
190
                    [
191
                        'itemsCount' => 1,
192
                        'email' => '[email protected]',
193
                        'shippingAddress' => $this->getAddress('US')
194
                    ]
195
                ),
196
                'entity' => $this->getEntity(
197
                    [
198
                        'itemsCount' => 1,
199
                        'email' => '[email protected]',
200
                        'shippingAddress' => $this->getAddress('US')
201
                    ]
202
                ),
203
                'databaseEntity' => $this->getEntity(
204
                    [
205
                        'itemsCount' => 1,
206
                        'email' => '[email protected]',
207
                        'shippingAddress' => $this->getAddress('US')
208
                    ]
209
                )
210
            ],
211
            'drop without country' => [
212
                'expected' => $this->getEntity(
213
                    [
214
                        'itemsCount' => 1,
215
                        'email' => '[email protected]',
216
                        'billingAddress' => null
217
                    ]
218
                ),
219
                'entity' => $this->getEntity(
220
                    [
221
                        'itemsCount' => 1,
222
                        'email' => '[email protected]',
223
                        'billingAddress' => $this->getAddress()
224
                    ]
225
                )
226
            ]
227
        ];
228
    }
229
230
    /**
231
     * Test setting removed field on removed cart items
232
     */
233
    public function testUpdateRemovedCartItems()
234
    {
235
        $channel = new Channel();
236
237
        $cartItem1 = new CartItem();
238
        $cartItem1->setName('Cart Item 1');
239
240
        $cartItem2 = new CartItem();
241
        $cartItem2->setName('Cart Item 2');
242
243
        $cartItem3 = new CartItem();
244
        $cartItem3->setName('Cart Item 3');
245
246
        $existingCartItems = new ArrayCollection();
247
        $existingCartItems->add($cartItem1);
248
        $existingCartItems->add($cartItem2);
249
250
        $existingCart = new Cart();
251
        $existingCart->setCartItems($existingCartItems);
252
        $existingCart->setChannel($channel);
253
        $existingCart->setItemsQty(2);
254
255
        $newCartItems = new ArrayCollection();
256
        $newCartItems->add($cartItem2);
257
        $newCartItems->add($cartItem3);
258
259
        $newCart = new Cart();
260
        $newCart->setCartItems($newCartItems);
261
        $newCart->setChannel($channel);
262
        $newCart->setItemsQty(2);
263
264
        $this->databaseHelper->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Oro\Bundle\ImportExportBundle\Field\DatabaseHelper.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
265
            ->method('findOneByIdentity')
266
            ->will($this->returnValue($existingCart));
267
268
        $this->databaseHelper->expects($this->once())
269
            ->method('getEntityReference')
270
            ->will($this->returnValue($channel));
271
272
        $this->databaseHelper->expects($this->once())
273
            ->method('getIdentifier')
274
            ->will($this->returnValue('identifier'));
275
276
        $this->databaseHelper->expects($this->once())
277
            ->method('find')
278
            ->with('OroCRM\Bundle\MagentoBundle\Entity\Cart', 'identifier')
279
            ->will($this->returnValue($newCart));
280
281
        $strategy = $this->getStrategy();
282
        /** @var \PHPUnit_Framework_MockObject_MockObject|ContextInterface $context */
283
        $context = $this->getMock('Oro\Bundle\ImportExportBundle\Context\ContextInterface');
284
        $strategy->setImportExportContext($context);
285
        $strategy->setEntityName('OroCRM\Bundle\MagentoBundle\Entity\Cart');
286
        $execution = $this->getMock('Akeneo\Bundle\BatchBundle\Item\ExecutionContext');
287
        $this->jobExecution->expects($this->any())->method('getExecutionContext')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Akeneo\Bundle\BatchBundle\Entity\JobExecution.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
288
            ->will($this->returnValue($execution));
289
        $strategy->setStepExecution($this->stepExecution);
290
291
        $strategy->process($existingCart);
292
293
        $this->assertTrue($cartItem1->isRemoved());
294
        $this->assertFalse($cartItem2->isRemoved());
295
        $this->assertFalse($cartItem3->isRemoved());
296
    }
297
298
    /**
299
     * @param int $originId
300
     *
301
     * @return CartItem
302
     */
303
    protected function getCartItem($originId)
304
    {
305
        $cartItem = new CartItem();
306
        $cartItem->setOriginId($originId);
307
308
        return $cartItem;
309
    }
310
311
    /**
312
     * @param string $countryCode
313
     * @return CartAddress
314
     */
315
    protected function getAddress($countryCode = null)
316
    {
317
        $address = new CartAddress();
318
319
        if ($countryCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $countryCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
320
            $address->setCountry(new Country($countryCode));
321
        }
322
323
        return $address;
324
    }
325
326
    /**
327
     * @param string $email
328
     * @return Customer
329
     */
330
    protected function getCustomer($email = null)
331
    {
332
        $customer = new Customer();
333
        if ($email) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $email of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
334
            $customer->setEmail($email);
335
        }
336
337
        return $customer;
338
    }
339
340
    /**
341
     * @param array $properties
342
     *
343
     * @return Cart
344
     */
345
    protected function getEntity(array $properties = [])
346
    {
347
        $cart = new Cart();
348
349
        $channel = new Channel();
350
        $cart->setChannel($channel);
351
352
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
353
354
        foreach ($properties as $property => $value) {
355
            if ($value instanceof ArrayCollection) {
356
                foreach ($value as $item) {
357
                    try {
358
                        $propertyAccessor->setValue($item, 'cart', $cart);
359
                    } catch (NoSuchPropertyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
360
                    }
361
                }
362
            } elseif (is_object($value)) {
363
                try {
364
                    $propertyAccessor->setValue($value, 'cart', $cart);
365
                } catch (NoSuchPropertyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
366
                }
367
            }
368
369
            $propertyAccessor->setValue($cart, $property, $value);
370
        }
371
372
        return $cart;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $cart; of type object|array is incompatible with the return type documented by OroCRM\Bundle\MagentoBun...StrategyTest::getEntity of type OroCRM\Bundle\MagentoBundle\Entity\Cart as it can also be of type array which is not included in this return type.
Loading history...
373
    }
374
}
375