Completed
Push — master ( c2375b...0b658a )
by
unknown
09:35
created

ProxyEntityWriterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
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 only exist in PHPUnit_Framework_MockObject_MockObject, but not in Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface.

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...
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 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...
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
    public function testMergeGuestCustomers()
140
    {
141
        $channel = $this
142
            ->getMockBuilder('OroCRM\Bundle\ChannelBundle\Entity\Channel')
143
            ->setMethods(['getId'])
144
            ->disableOriginalConstructor()
145
            ->getMock();
146
        $channel
147
            ->expects($this->exactly(2))
148
            ->method('getId')
149
            ->will($this->returnValue(100));
150
151
        $customer1 = $this
152
            ->getMockBuilder('OroCRM\Bundle\MagentoBundle\Entity\Customer')
153
            ->setMethods(['getChannel'])
154
            ->disableOriginalConstructor()
155
            ->getMock();
156
        $customer1
157
            ->expects($this->exactly(1))
158
            ->method('getChannel')
159
            ->will($this->returnValue($channel));
160
161
        $customer2 = $this
162
            ->getMockBuilder('OroCRM\Bundle\MagentoBundle\Entity\Customer')
163
            ->setMethods(['getChannel'])
164
            ->disableOriginalConstructor()
165
            ->getMock();
166
        $customer2
167
            ->expects($this->exactly(1))
168
            ->method('getChannel')
169
            ->will($this->returnValue($channel));
170
171
        $customer1->setEmail('[email protected]');
172
        $customer1->setGuest(true);
173
        $customer2->setEmail('[email protected]');
174
        $customer2->setGuest(true);
175
176
        $this->wrapped
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface.

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...
177
            ->expects($this->once())
178
            ->method('write')
179
            ->with(['test1test.com100' => $customer1]);
180
181
        $this->writer->write([$customer1, $customer2]);
182
    }
183
}
184