Completed
Push — 1.10 ( bee206...263818 )
by
unknown
08:47
created

ImportReplaceOrAddStrategyTest::doImport()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 27
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 27
loc 27
rs 8.8571
cc 1
eloc 16
nc 1
nop 3
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\Tests\Functional;
4
5
use Akeneo\Bundle\BatchBundle\Job\DoctrineJobRepository as BatchJobRepository;
6
7
use Doctrine\ORM\EntityManager;
8
9
use Symfony\Component\DomCrawler\Form;
10
11
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
12
13
use OroCRM\Bundle\ContactBundle\Entity\Contact;
14
15
/**
16
 * @outputBuffering enabled
17
 * @dbIsolationPerTest
18
 * @dbReindex
19
 */
20
class ImportReplaceOrAddStrategyTest extends WebTestCase
21
{
22
    const ADD_STRATEGY              = 'orocrm_contact.add';
23
    const ADD_OR_REPLACE_STRATEGY   = 'orocrm_contact.add_or_replace';
24
25
    /** @var string */
26
    protected $file;
27
28
    /** {@inheritdoc} */
29
    protected function setUp()
30
    {
31
        $this->initClient(array(), $this->generateBasicAuthHeader());
32
33
        $dataDir = $this->getContainer()
34
            ->get('kernel')
35
            ->locateResource('@OroCRMContactBundle/Tests/Functional/DataFixtures/Data');
36
37
        $this->file = $dataDir . DIRECTORY_SEPARATOR. "contacts.csv";
38
    }
39
40
    /**
41
     * Delete data required because there is commit to job repository in import/export controller action
42
     * Please use
43
     *   $this->getContainer()->get('akeneo_batch.job_repository')->getJobManager()->beginTransaction();
44
     *   $this->getContainer()->get('akeneo_batch.job_repository')->getJobManager()->rollback();
45
     *   $this->getContainer()->get('akeneo_batch.job_repository')->getJobManager()->getConnection()->clear();
46
     * if you don't use controller
47
     */
48 View Code Duplication
    protected function tearDown()
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...
49
    {
50
        // clear DB from separate connection, close to avoid connection limit and memory leak
51
        $batchJobManager = $this->getBatchJobManager();
52
        $batchJobManager->createQuery('DELETE AkeneoBatchBundle:JobInstance')->execute();
53
        $batchJobManager->createQuery('DELETE AkeneoBatchBundle:JobExecution')->execute();
54
        $batchJobManager->createQuery('DELETE AkeneoBatchBundle:StepExecution')->execute();
55
56
        unset($this->file);
57
58
        parent::tearDown();
59
    }
60
61
    /**
62
     * Test how import contact replace strategy. More see CRM-8363
63
     */
64 View Code Duplication
    public function testAddOrReplaceStrategyImport()
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...
65
    {
66
        $this->validateImportFile(self::ADD_OR_REPLACE_STRATEGY);
67
        $this->doImport(self::ADD_OR_REPLACE_STRATEGY, 10, 0);
68
69
        $this->validateImportFile(self::ADD_OR_REPLACE_STRATEGY);
70
        $this->doImport(self::ADD_OR_REPLACE_STRATEGY, 0, 10);
71
    }
72
73
    /**
74
     * Test how import data twice after delete. More see CRM-8364
75
     */
76 View Code Duplication
    public function testAddOrStrategyImport()
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...
77
    {
78
        $this->validateImportFile(self::ADD_OR_REPLACE_STRATEGY);
79
        $this->doImport(self::ADD_OR_REPLACE_STRATEGY, 10, 0);
80
81
        $this->deleteTableRecords();
82
83
        $this->validateImportFile(self::ADD_OR_REPLACE_STRATEGY);
84
        $this->doImport(self::ADD_OR_REPLACE_STRATEGY, 10, 0);
85
    }
86
87
    protected function deleteTableRecords()
88
    {
89
        $em = $this->getContainer()->get('doctrine')->getManager();
90
        $collection = $em->getRepository('OroCRMContactBundle:Contact')->findAll();
91
92
        foreach ($collection as $item) {
93
            $em->remove($item);
94
        }
95
96
        $em->flush();
97
    }
98
99
    /**
100
     * @param string $strategy
101
     */
102
    protected function validateImportFile($strategy)
103
    {
104
        $crawler = $this->client->request(
105
            'GET',
106
            $this->getUrl(
107
                'oro_importexport_import_form',
108
                array(
109
                    'entity'           => Contact::class,
110
                    '_widgetContainer' => 'dialog'
111
                )
112
            )
113
        );
114
        $result = $this->client->getResponse();
115
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
116
        $this->assertContains($strategy, $result->getContent());
117
118
        $this->assertTrue(file_exists($this->file));
119
120
        /** @var Form $form */
121
        $form = $crawler->selectButton('Submit')->form();
122
123
        /** TODO Change after BAP-1813 */
124
        $form->getFormNode()->setAttribute(
125
            'action',
126
            $form->getFormNode()->getAttribute('action') . '&_widgetContainer=dialog'
127
        );
128
129
        $form['oro_importexport_import[file]']->upload($this->file);
130
        $form['oro_importexport_import[processorAlias]'] = $strategy;
131
132
        $this->client->followRedirects(true);
133
        $this->client->submit($form);
134
135
        $result = $this->client->getResponse();
136
137
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
138
139
        $crawler = $this->client->getCrawler();
140
        $this->assertEquals(0, $crawler->filter('.import-errors')->count());
141
    }
142
143
    /**
144
     * @param string $strategy
145
     * @param int $added
146
     * @param int $replaced
147
     */
148 View Code Duplication
    protected function doImport($strategy, $added, $replaced)
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...
149
    {
150
        // test import
151
        $this->client->followRedirects(false);
152
        $this->client->request(
153
            'GET',
154
            $this->getUrl(
155
                'oro_importexport_import_process',
156
                array(
157
                    'processorAlias' => $strategy,
158
                    '_format'        => 'json'
159
                )
160
            )
161
        );
162
163
        $data = $this->getJsonResponseContent($this->client->getResponse(), 200);
0 ignored issues
show
Documentation introduced by
$this->client->getResponse() is of type object|null, but the function expects a object<Symfony\Component\HttpFoundation\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164
165
        $this->assertEquals(
166
            [
167
                'success'    => true,
168
                'message'    => 'File was successfully imported.',
169
                'errorsUrl'  => null,
170
                'importInfo' => sprintf('%s entities were added, %s entities were updated', $added, $replaced)
171
            ],
172
            $data
173
        );
174
    }
175
176
    /**
177
     * @return EntityManager
178
     */
179
    protected function getBatchJobManager()
180
    {
181
        /** @var BatchJobRepository $batchJobRepository */
182
        $batchJobRepository = $this->getContainer()->get('akeneo_batch.job_repository');
183
184
        return $batchJobRepository->getJobManager();
185
    }
186
}
187