Completed
Push — 1.10 ( 93e51c...7f32fb )
by
unknown
11:06
created

testConvertAddressForCustomerByIds()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 16
nc 4
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Command;
4
5
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
6
7
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
8
use OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture\CopyCustomerAddressToContact\LoadMagentoChannel;
9
10
/**
11
 * @dbIsolationPerTest
12
 */
13
class CopyCustomerAddressesToContactCommandTest extends WebTestCase
14
{
15
    public function setUp()
16
    {
17
        $this->initClient();
18
        $this->loadFixtures([LoadMagentoChannel::class]);
19
    }
20
21
    public function testCommand()
22
    {
23
        $entityManager = $this->getContainer()->get('doctrine');
24
        $repo = $entityManager->getRepository('OroCRM\Bundle\MagentoBundle\Entity\Customer');
25
        $customers = $repo->findAll();
26
        /** @var Customer $customer */
27
        foreach ($customers as $customer) {
28
            self::assertEquals(0, $customer->getContact()->getAddresses()->count());
29
        }
30
31
        $result = $this->runCommand('oro:magento:copy-data-to-contact:addresses', ['--batch-size=2']);
32
33
        $customers = $repo->findAll();
34
        /** @var Customer $customer */
35
        foreach ($customers as $customer) {
36
            self::assertEquals(1, $customer->getContact()->getAddresses()->count());
37
        }
38
39
        $this->assertContains('Executing command started.', $result);
40
        $this->assertContains('Executing command finished.', $result);
41
    }
42
43 View Code Duplication
    public function testConvertAddressForCustomerById()
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...
44
    {
45
        $testCustomer = $this->getReference('customer_1');
46
        $id = $testCustomer->getId();
47
48
        $entityManager = $this->getContainer()->get('doctrine');
49
        $repo = $entityManager->getRepository('OroCRM\Bundle\MagentoBundle\Entity\Customer');
50
        /** @var Customer $customer */
51
        $customer = $repo->find($id);
52
        self::assertEquals(0, $customer->getContact()->getAddresses()->count());
53
54
        $result = $this->runCommand('oro:magento:copy-data-to-contact:addresses', ['--id=' . $id]);
55
56
        /** @var Customer $customer */
57
        $customer = $repo->find($id);
58
        self::assertEquals(1, $customer->getContact()->getAddresses()->count());
59
60
        $this->assertContains('Executing command started.', $result);
61
        $this->assertContains('Executing command finished.', $result);
62
    }
63
64
    public function testConvertAddressForCustomerByIds()
65
    {
66
        $testCustomerId1 = $this->getReference('customer_1')->getId();
67
        $testCustomerId2 = $this->getReference('customer_2')->getId();
68
69
        $entityManager = $this->getContainer()->get('doctrine');
70
        $repo = $entityManager->getRepository('OroCRM\Bundle\MagentoBundle\Entity\Customer');
71
        $customers = $repo->findBy(['id' => [$testCustomerId1, $testCustomerId2]]);
72
        /** @var Customer $customer */
73
        foreach ($customers as $customer) {
74
            self::assertEquals(0, $customer->getContact()->getAddresses()->count());
75
        }
76
77
        $result = $this->runCommand('oro:magento:copy-data-to-contact:addresses', [
78
            '--id=' . $testCustomerId1,
79
            '--id=' . $testCustomerId2
80
        ]);
81
82
        $customers = $repo->findBy(['id' => [$testCustomerId1, $testCustomerId2]]);
83
        /** @var Customer $customer */
84
        foreach ($customers as $customer) {
85
            self::assertEquals(1, $customer->getContact()->getAddresses()->count());
86
        }
87
88
        $this->assertContains('Executing command started.', $result);
89
        $this->assertContains('Executing command finished.', $result);
90
    }
91
92
    public function testConvertAddressForCustomerByIntegrationId()
93
    {
94
        $integrationId = $this->getReference('integration')->getId();
95
        $entityManager = $this->getContainer()->get('doctrine');
96
        $repo = $entityManager->getRepository('OroCRM\Bundle\MagentoBundle\Entity\Customer');
97
        $customers = $repo->findBy(['channel' => $integrationId]);
98
        /** @var Customer $customer */
99
        foreach ($customers as $customer) {
100
            self::assertEquals(0, $customer->getContact()->getAddresses()->count());
101
        }
102
103
        $result = $this->runCommand('oro:magento:copy-data-to-contact:addresses', [
104
            '--integration-id=' . $integrationId,
105
            '--batch-size=2'
106
        ]);
107
108
        $customers = $repo->findBy(['channel' => $integrationId]);
109
        /** @var Customer $customer */
110
        foreach ($customers as $customer) {
111
            self::assertEquals(1, $customer->getContact()->getAddresses()->count());
112
        }
113
114
        $this->assertContains('Executing command started.', $result);
115
        $this->assertContains('Executing command finished.', $result);
116
    }
117
118 View Code Duplication
    public function testConvertAddressForCustomerByIdAndAccountHasAddress()
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...
119
    {
120
        $testCustomerId1 = $this->getReference('customer_1')->getId();
121
122
        $entityManager = $this->getContainer()->get('doctrine');
123
        $repo = $entityManager->getRepository('OroCRM\Bundle\MagentoBundle\Entity\Customer');
124
        /** @var Customer $customer */
125
        $customer = $repo->find($testCustomerId1);
126
        self::assertEquals(0, $customer->getContact()->getAddresses()->count());
127
128
        for ($i = 0; $i < 2; $i++) {
129
            $result = $this->runCommand('oro:magento:copy-data-to-contact:addresses', [
130
                '--id=' . $testCustomerId1
131
            ]);
132
        }
133
134
        /** @var Customer $customer */
135
        $customer = $repo->find($testCustomerId1);
136
        self::assertEquals(1, $customer->getContact()->getAddresses()->count());
137
138
        $this->assertContains('Executing command started.', $result);
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
139
        $this->assertContains('Executing command finished.', $result);
140
    }
141
}
142