Completed
Pull Request — master (#336)
by Stefan
05:11
created

ConnectExportTest::insertVariants()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 122
Code Lines 84

Duplication

Lines 12
Ratio 9.84 %

Importance

Changes 0
Metric Value
dl 12
loc 122
rs 8.2857
c 0
b 0
f 0
cc 2
eloc 84
nc 2
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 Tests\ShopwarePlugins\Connect;
4
5
use Shopware\CustomModels\Connect\Attribute;
6
use Shopware\Models\Article\Configurator\Group;
7
use ShopwarePlugins\Connect\Components\ConnectExport;
8
use ShopwarePlugins\Connect\Components\ErrorHandler;
9
use ShopwarePlugins\Connect\Components\Validator\ProductAttributesValidator\ProductsAttributesValidator;
10
use ShopwarePlugins\Connect\Components\Config;
11
12
class ConnectExportTest extends ConnectTestHelper
13
{
14
    /**
15
     * @var \ShopwarePlugins\Connect\Components\ConnectExport
16
     */
17
    private $connectExport;
18
19
    /**
20
     * @var \ShopwarePlugins\Connect\Components\Config
21
     */
22
    private $config;
23
24
    /**
25
     * @var \Shopware\Components\Model\ModelManager
26
     */
27
    private $manager;
28
29
    public static function setUpBeforeClass()
30
    {
31
        parent::setUpBeforeClass();
32
33
        $conn = Shopware()->Db();
34
        $conn->delete('sw_connect_shop_config', array('s_shop = ?' => '_price_type'));
35
        $conn->insert('sw_connect_shop_config', array('s_shop' => '_price_type', 's_config' => 3));
36
    }
37
38
    public function setUp()
39
    {
40
        parent::setUp();
41
42
        $this->manager = Shopware()->Models();
43
        $this->config = new Config($this->manager);
44
        $this->connectExport = new ConnectExport(
45
            $this->getHelper(),
46
            $this->getSDK(),
47
            $this->manager,
48
            new ProductsAttributesValidator(),
49
            new Config($this->manager),
50
            new ErrorHandler(),
51
            Shopware()->Container()->get('events')
52
        );
53
54
        if (method_exists('Shopware\Models\Article\Detail', 'setPurchasePrice')) {
55
            $purchasePrice = 'detailPurchasePrice';
56
        } else {
57
            $purchasePrice = 'basePrice';
58
        }
59
        $configs = array(
60
            'priceGroupForPriceExport' => array('EK', null, 'export'),
61
            'priceGroupForPurchasePriceExport' => array('EK', null, 'export'),
62
            'priceFieldForPriceExport' => array('price', null, 'export'),
63
            'priceFieldForPurchasePriceExport' => array($purchasePrice, null, 'export'),
64
        );
65
66
        foreach ($configs as $name => $values) {
67
            list($value, $shopId, $group) = $values;
68
69
            $this->config->setConfig(
70
                $name,
71
                $value,
72
                $shopId,
73
                $group
74
            );
75
        }
76
    }
77
78
    public function testExport()
79
    {
80
        /** @var \Shopware\Models\Article\Article $model */
81
        $model = $this->manager->getRepository('Shopware\Models\Article\Article')->find(3);
82
        $detail = $model->getMainDetail();
83
84
        //fixes wrong sort mode in demo data
85
        $propertyGroup = $model->getPropertyGroup();
86
        $propertyGroup->setSortMode(0);
87
        $this->manager->persist($propertyGroup);
88
89
        /** @var \Shopware\Models\Article\Price $prices */
90
        $prices = $detail->getPrices();
91
        if (method_exists($detail, 'setPurchasePrice')) {
92
            $detail->setPurchasePrice(9.89);
93
        } else {
94
            $prices[0]->setBasePrice(9.89);
95
            $detail->setPrices($prices);
96
        }
97
98
        $this->manager->persist($detail);
99
100
        $connectAttribute = $this->getHelper()->getOrCreateConnectAttributeByModel($model);
101
        $connectAttribute->setExportStatus(Attribute::STATUS_INSERT);
102
        $connectAttribute->setExported(true);
103
        $this->manager->persist($connectAttribute);
104
        $this->manager->flush();
105
        $this->manager->clear();
106
107
        $errors = $this->connectExport->export(array(3));
108
109
        $this->assertEmpty($errors);
110
        $sql = 'SELECT export_status, export_message, exported FROM s_plugin_connect_items WHERE article_detail_id = ?';
111
        $row = Shopware()->Db()->fetchRow($sql, array($detail->getId()));
112
113
        $this->assertEquals('update', $row['export_status']);
114
        $this->assertNull($row['export_message']);
115
        $this->assertEquals(1, $row['exported']);
116
    }
117
118
    /**
119
     * @depends testExport
120
     */
121
    public function testExportErrors()
122
    {
123
        $model = $this->manager->getRepository('Shopware\Models\Article\Article')->find(4);
124
        $detail = $model->getMainDetail();
125
        $detail->setPurchasePrice(0);
126
        $this->manager->persist($detail);
127
        $this->manager->flush();
128
129
        $connectAttribute = $this->getHelper()->getOrCreateConnectAttributeByModel($model);
130
        $errors = $this->connectExport->export(array(4));
131
132
        $this->assertNotEmpty($errors);
133
134
        $sql = 'SELECT export_status, export_message FROM s_plugin_connect_items WHERE article_id = ?';
135
        $row = Shopware()->Db()->fetchRow($sql, array(4));
136
137
        $this->assertEquals('error-price', $row['export_status']);
138
        $this->assertContains('Ein Preisfeld für dieses Produkt ist nicht gepfegt', $row['export_message']);
139
    }
140
141
    public function testSyncDeleteArticle()
142
    {
143
        $articleId = $this->insertVariants();
144
        $modelManager = $this->manager;
145
        $article = $modelManager->getRepository('Shopware\Models\Article\Article')->find($articleId);
146
147
        $this->connectExport->setDeleteStatusForVariants($article);
148
        $result = Shopware()->Db()->executeQuery(
149
            'SELECT export_status, exported
150
              FROM s_plugin_connect_items
151
              WHERE article_id = ?',
152
            array($articleId)
153
        )->fetchAll();
154
155
        foreach ($result as $connectAttribute) {
156
            $this->assertEquals('delete', $connectAttribute['export_status']);
157
            $this->assertEquals(0, $connectAttribute['exported']);
158
        }
159
160
        $this->assertEquals(4, Shopware()->Db()->query('SELECT COUNT(*) FROM sw_connect_change WHERE c_entity_id LIKE "1919%"')->fetchColumn());
161
    }
162
163
    public function testDeleteVariant()
164
    {
165
        $articleId = $this->insertVariants();
166
        $modelManager = $this->manager;
167
        /** @var \Shopware\Models\Article\Article $article */
168
        $article = $modelManager->getRepository('Shopware\Models\Article\Article')->find($articleId);
169
        $detail = $article->getMainDetail();
170
171
        $this->connectExport->syncDeleteDetail($detail);
172
173
        $this->assertEquals(1, Shopware()->Db()->query('SELECT COUNT(*) FROM sw_connect_change WHERE c_entity_id LIKE "1919%"')->fetchColumn());
174
        $this->assertEquals(1, Shopware()->Db()->query('SELECT COUNT(*) FROM s_plugin_connect_items WHERE source_id = "1919" AND export_status = "delete"')->fetchColumn());
175
    }
176
177
    public function testDeleteNotExportedVariant()
178
    {
179
        $articleId = $this->insertVariants();
180
        $modelManager = $this->manager;
181
        /** @var \Shopware\Models\Article\Article $article */
182
        $article = $modelManager->getRepository('Shopware\Models\Article\Article')->find($articleId);
183
        $detail = $article->getMainDetail();
184
        Shopware()->Db()->executeUpdate('UPDATE s_plugin_connect_items SET export_status = NULL, exported = 0 where source_id = "1919"');
185
        $this->connectExport->syncDeleteDetail($detail);
186
187
        $this->assertEquals(0, Shopware()->Db()->query('SELECT COUNT(*) FROM sw_connect_change WHERE c_entity_id LIKE "1919%"')->fetchColumn());
188
        $this->assertEquals(1, Shopware()->Db()->query('SELECT COUNT(*) FROM s_plugin_connect_items WHERE source_id = "1919" AND export_status IS NULL')->fetchColumn());
189
    }
190
191
    private function insertVariants()
192
    {
193
        //clear connect_change table
194
        Shopware()->Db()->exec('DELETE FROM sw_connect_change WHERE c_entity_id LIKE "1919%"');
195
        //clear s_articles table
196
        Shopware()->Db()->exec('DELETE FROM s_articles WHERE name = "Turnschuh"');
197
        //clear s_articles_detail table
198
        Shopware()->Db()->exec('DELETE FROM s_articles_details WHERE ordernumber LIKE "1919%"');
199
        //clear connect_items table
200
        Shopware()->Db()->exec('DELETE FROM s_plugin_connect_items WHERE source_id LIKE "1919%"');
201
202
203
        $minimalTestArticle = array(
204
            'name' => 'Turnschuh',
205
            'active' => true,
206
            'tax' => 19,
207
            'supplier' => 'Turnschuh Inc.',
208
            'categories' => array(
209
                array('id' => 15),
210
            ),
211
            'mainDetail' => array(
212
                'number' => '1919',
213
            ),
214
        );
215
216
        $articleResource = \Shopware\Components\Api\Manager::getResource('article');
217
        /** @var \Shopware\Models\Article\Article $article */
218
        $article = $articleResource->create($minimalTestArticle);
219
220
        $updateArticle = array(
221
            'configuratorSet' => array(
222
                'groups' => array(
223
                    array(
224
                        'name' => 'Größe',
225
                        'options' => array(
226
                            array('name' => 'S'),
227
                            array('name' => 'M'),
228
                            array('name' => 'L'),
229
                            array('name' => 'XL'),
230
                            array('name' => 'XXL'),
231
                        )
232
                    ),
233
                    array(
234
                        'name' => 'Farbe',
235
                        'options' => array(
236
                            array('name' => 'Weiß'),
237
                            array('name' => 'Gelb'),
238
                            array('name' => 'Blau'),
239
                            array('name' => 'Schwarz'),
240
                            array('name' => 'Rot'),
241
                        )
242
                    ),
243
                )
244
            ),
245
            'taxId' => 1,
246
            'variants' => array(
247
                array(
248
                    'isMain' => true,
249
                    'number' => '1919',
250
                    'inStock' => 15,
251
                    'standard' => null,
252
                    'additionaltext' => 'L / Schwarz',
253
                    'configuratorOptions' => array(
254
                        array('group' => 'Größe', 'groupId' => null, 'optionId' => null, 'option' => 'L'),
255
                        array('group' => 'Farbe', 'groupId' => null, 'optionId' => null, 'option' => 'Schwarz'),
256
                    ),
257
                ),
258
                array(
259
                    'isMain' => false,
260
                    'number' => '1919-1',
261
                    'inStock' => 15,
262
                    'standard' => null,
263
                    'additionnaltext' => 'S / Schwarz',
264
                    'configuratorOptions' => array(
265
                        array('group' => 'Größe', 'groupId' => null, 'optionId' => null,'option' => 'S'),
266
                        array('group' => 'Farbe', 'groupId' => null, 'optionId' => null, 'option' => 'Schwarz'),
267
                    ),
268
                ),
269
                array(
270
                    'isMain' => false,
271
                    'number' => '1919-2',
272
                    'inStock' => 15,
273
                    'standard' => null,
274
                    'additionnaltext' => 'S / Rot',
275
                    'configuratorOptions' => array(
276
                        array('group' => 'Größe', 'groupId' => null, 'optionId' => null,'option' => 'S'),
277
                        array('group' => 'Farbe', 'groupId' => null, 'optionId' => null,'option' => 'Rot'),
278
                    ),
279
                ),
280
                array(
281
                    'isMain' => false,
282
                    'number' => '1919-3',
283
                    'inStock' => 15,
284
                    'standard' => null,
285
                    'additionnaltext' => 'XL / Rot',
286
                    'configuratorOptions' => array(
287
                        array('group' => 'Größe', 'groupId' => null, 'optionId' => null, 'option' => 'XL'),
288
                        array('group' => 'Farbe', 'groupId' => null, 'optionId' => null,'option' => 'Rot'),
289
                    ),
290
                )
291
            )
292
        );
293
294
        /** @var \Shopware\Models\Article\Article $article */
295
        $article = $articleResource->update($article->getId(), $updateArticle);
296
297
        /** @var \Shopware\Models\Article\Detail $detail */
298
        foreach ($article->getDetails() as $detail) {
299
            Shopware()->Db()->executeQuery(
300
                'INSERT INTO s_plugin_connect_items (article_id, article_detail_id, source_id, category, exported)
301
                  VALUES (?, ?, ?, ?, ?)',
302
                array(
303
                    $article->getId(),
304
                    $detail->getId(),
305
                    $detail->getNumber(),
306
                    '/bücher',
307
                    1
308
                ));
309
        }
310
311
        return $article->getId();
312
    }
313
314
    //todo: test export marketplace attributes
315
}
316
 
317