Completed
Push — 1.8 ( 8d6730...a2d9e7 )
by
unknown
42:28
created

ProxyEntityWriterTest::getCustomer()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
rs 8.5806
cc 4
eloc 28
nc 4
nop 4
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\ImportExport\Writer;
4
5
use Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface;
6
7
use Oro\Bundle\ImportExportBundle\Field\DatabaseHelper;
8
9
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
10
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
11
use OroCRM\Bundle\MagentoBundle\Entity\Order;
12
use OroCRM\Bundle\MagentoBundle\ImportExport\Writer\ProxyEntityWriter;
13
14
class ProxyEntityWriterTest extends \PHPUnit_Framework_TestCase
15
{
16
    /** @var ItemWriterInterface|\PHPUnit_Framework_MockObject_MockObject */
17
    protected $wrapped;
18
19
    /** @var ProxyEntityWriter */
20
    protected $writer;
21
22
    /**
23
     * @var \PHPUnit_Framework_MockObject_MockObject|DatabaseHelper
24
     */
25
    protected $databaseHelper;
26
27
    protected function setUp()
28
    {
29
        $this->wrapped = $this
30
            ->getMockBuilder('Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface')
31
            ->setMethods(['write'])
32
            ->getMock();
33
34
        $this->databaseHelper = $this->getMockBuilder('Oro\Bundle\ImportExportBundle\Field\DatabaseHelper')
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
38
        $this->writer  = new ProxyEntityWriter($this->wrapped, $this->databaseHelper);
39
    }
40
41
    protected function tearDown()
42
    {
43
        unset($this->writer, $this->wrapped);
44
    }
45
46
    /**
47
     * @dataProvider itemsProvider
48
     *
49
     * @param array $items
50
     * @param array $expectedItems
51
     */
52
    public function testWrite(array $items, array $expectedItems)
53
    {
54
        $this->wrapped->expects($this->once())->method('write')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Akeneo\Bundle\Bat...em\ItemWriterInterface>.

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...
55
            ->with($this->equalTo($expectedItems));
56
57
        $stepExecution = $this->getMockBuilder('Akeneo\Bundle\BatchBundle\Entity\StepExecution')
58
            ->disableOriginalConstructor()->getMock();
59
        $this->databaseHelper->expects($this->once())->method('onClear');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Oro\Bundle\Import...e\Field\DatabaseHelper>.

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...
60
        $this->writer->setStepExecution($stepExecution);
61
62
        $this->writer->write($items);
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    public function itemsProvider()
69
    {
70
        $order1 = new Order();
71
        $order2 = new Order();
72
        $order1->setIncrementId('1111');
73
        $order2->setIncrementId('2222');
74
        $order3 = clone $order1;
75
76
        $cart1 = new Cart();
77
        $cart2 = new Cart();
78
        $cart1->setOriginId(1111);
79
        $cart2->setOriginId(2222);
80
        $cart3 = clone $cart1;
81
82
        $customer1 = new Customer();
83
        $customer1->setOriginId(111);
84
        $customer2 = clone $customer1;
85
86
        $customerGuest1 = new Customer();
87
        $customerGuest2 = new Customer();
88
89
        $someEntity  = new \stdClass();
90
        $someEntity2 = new \stdClass();
91
92
        return [
93
            'should skip non-unique orders'                                  => [
94
                '$items'         => [$order1, $order2, $order3],
95
                '$expectedItems' => [$order3->getIncrementId() => $order3, $order2->getIncrementId() => $order2]
96
            ],
97
            'should skip non-unique carts'                                   => [
98
                '$items'         => [$cart1, $cart2, $cart3],
99
                '$expectedItems' => [$cart3->getOriginId() => $cart3, $cart2->getOriginId() => $cart2]
100
            ],
101
            'should skip non-unique customers'                               => [
102
                '$items'         => [$customer1, $customer2],
103
                '$expectedItems' => [$customer2->getOriginId() => $customer2]
104
            ],
105
            'dont skip guest customers'                                      => [
106
                '$items'         => [$customerGuest1, $customerGuest1, $customerGuest2],
107
                '$expectedItems' => [
108
                    spl_object_hash($customerGuest1) => $customerGuest1,
109
                    spl_object_hash($customerGuest2) => $customerGuest2,
110
                ],
111
            ],
112
            'should not break logic with entities that not consist originId' => [
113
                '$items'         => [$someEntity, $someEntity2],
114
                '$expectedItems' => [$someEntity, $someEntity2]
115
            ]
116
        ];
117
    }
118
119 View Code Duplication
    public function testSetStepExecutionSetToWrappedWriter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $wrapped       = $this->getMock('OroCRM\Bundle\MagentoBundle\Tests\Unit\Stub\StepExecutionAwareWriter');
122
        $writer        = new ProxyEntityWriter($wrapped, $this->databaseHelper);
123
        $stepExecution = $this->getMockBuilder('Akeneo\Bundle\BatchBundle\Entity\StepExecution')
124
            ->disableOriginalConstructor()->getMock();
125
        $wrapped->expects($this->once())->method('setStepExecution')
126
            ->with($this->equalTo($stepExecution));
127
128
        $writer->setStepExecution($stepExecution);
129
    }
130
131
    public function testSetStepExecutionDoesNotProvokeErrorWithRegularWriter()
132
    {
133
        $stepExecution = $this->getMockBuilder('Akeneo\Bundle\BatchBundle\Entity\StepExecution')
134
            ->disableOriginalConstructor()->getMock();
135
136
        $this->writer->setStepExecution($stepExecution);
137
    }
138
139
    /**
140
     * @param $actualCustomersArray
141
     * @param $expectedCustomersArray
142
     *
143
     * @dataProvider customerProvider
144
     */
145
    public function testMergeGuestCustomers($actualCustomersArray, $expectedCustomersArray)
146
    {
147
        $this->wrapped
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Akeneo\Bundle\Bat...em\ItemWriterInterface>.

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
            ->expects($this->once())
149
            ->method('write')
150
            ->with($expectedCustomersArray);
151
152
        $this->writer->write($actualCustomersArray);
153
    }
154
155
    /**
156
     * @param string $customerEmail
157
     * @param int $channelId
158
     * @param bool $isGuest
159
     * @param int $originId
160
     *
161
     * @return PHPUnit_Framework_MockObject_MockObject
162
     */
163
    public function getCustomer($customerEmail, $channelId, $isGuest = true, $originId = null)
164
    {
165
        $channel = $this
166
            ->getMockBuilder('OroCRM\Bundle\ChannelBundle\Entity\Channel')
167
            ->setMethods(['getId'])
168
            ->disableOriginalConstructor()
169
            ->getMock();
170
        $customer = $this
171
            ->getMockBuilder('OroCRM\Bundle\MagentoBundle\Entity\Customer')
172
            ->setMethods(['getChannel', 'getOriginId'])
173
            ->disableOriginalConstructor()
174
            ->getMock();
175
        if ($channelId) {
176
            $channel
177
                ->expects($this->any())
178
                ->method('getId')
179
                ->will($this->returnValue($channelId));
180
            $customer
181
                ->expects($this->any())
182
                ->method('getChannel')
183
                ->will($this->returnValue($channel));
184
        }
185
186
        if (!$isGuest && $originId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $originId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
187
            $customer
188
                ->expects($this->any())
189
                ->method('getOriginId')
190
                ->will($this->returnValue($originId));
191
        }
192
193
        $customer->setEmail($customerEmail);
194
        $customer->setGuest($isGuest);
195
196
        return $customer;
197
    }
198
199
    /**
200
     * @return array
201
     */
202
    public function customerProvider()
203
    {
204
        return [
205
            [ //guest mode. customers will be merged
206
                [$this->getCustomer('[email protected]', 1), $this->getCustomer('[email protected]', 1)],
207
                [$this->getUniqueHash('[email protected]', 1) => $this->getCustomer('[email protected]', 1)]
208
            ],
209
            [ //guest mode. customers will be merged
210
                [$this->getCustomer('[email protected]', 1), $this->getCustomer('[email protected]', 1)],
211
                [$this->getUniqueHash('[email protected]', 1) => $this->getCustomer('[email protected]', 1)]
212
            ],
213
            [ //guest mode. customers will be merged
214
                [$this->getCustomer('[email protected]', 1), $this->getCustomer('[email protected]', 1)],
215
                [$this->getUniqueHash('[email protected]', 1) => $this->getCustomer('[email protected]', 1)]
216
            ],
217
            [ //guest mode. customers will be merged
218
                [$this->getCustomer('example&@e.com', 1), $this->getCustomer('example&@e.com', 1)],
219
                [$this->getUniqueHash('example&@e.com', 1) => $this->getCustomer('example&@e.com', 1)]
220
            ],
221
            [ //guest mode. customers won't be merged, different channel
222
                [$this->getCustomer('[email protected]', 1), $this->getCustomer('[email protected]', 2)],
223
                [
224
                    $this->getUniqueHash('[email protected]', 1) => $this->getCustomer('[email protected]', 1),
225
                    $this->getUniqueHash('[email protected]', 2) => $this->getCustomer('[email protected]', 2)
226
                ]
227
            ],
228
            [ //guest mode. customers won't be merged, different email
229
                [$this->getCustomer('[email protected]', 1), $this->getCustomer('[email protected]', 1)],
230
                [
231
                    $this->getUniqueHash('[email protected]', 1) => $this->getCustomer('[email protected]', 1),
232
                    $this->getUniqueHash('[email protected]', 1) => $this->getCustomer('[email protected]', 1)
233
                ]
234
            ],
235
            [ //guest mode. customers will be merged, without channel
236
                [$this->getCustomer('example&@e.com', null), $this->getCustomer('example&@e.com', null)],
237
                [$this->getUniqueHash('example&@e.com', null) => $this->getCustomer('example&@e.com', null)]
238
            ],
239
            [ //not guest mode. customers will be merged with originId
240
                [$this->getCustomer('[email protected]', 1, false, 10), $this->getCustomer('[email protected]', 1, false, 10)],
241
                [10 => $this->getCustomer('[email protected]', 1, false, 10)]
242
            ]
243
        ];
244
    }
245
246
    /**
247
     * @param string $email
248
     * @param int $channelId
249
     *
250
     * @return string
251
     */
252
    public function getUniqueHash($email, $channelId = null)
253
    {
254
        $string = $email;
255
256
        if ($channelId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $channelId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
257
            $string.=$channelId;
258
        }
259
260
        return md5($string);
261
    }
262
}
263