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

LifecycleTest::insertVariants()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 141
Code Lines 90

Duplication

Lines 15
Ratio 10.64 %

Importance

Changes 0
Metric Value
dl 15
loc 141
rs 8.2857
c 0
b 0
f 0
cc 2
eloc 90
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\Subscribers;
4
5
use ShopwarePlugins\Connect\Components\Config;
6
use ShopwarePlugins\Connect\Components\ConnectExport;
7
use ShopwarePlugins\Connect\Components\Validator\ProductAttributesValidator\ProductsAttributesValidator;
8
use Tests\ShopwarePlugins\Connect\ConnectTestHelper;
9
use ShopwarePlugins\Connect\Components\ErrorHandler;
10
11
class LifecycleTest extends ConnectTestHelper
12
{
13
    /**
14
     * @var \Shopware\Components\Model\ModelManager
15
     */
16
    private $manager;
17
18
    private $db;
19
20
    private $config;
21
22
    private $connectExport;
23
24
    public static function setUpBeforeClass()
25
    {
26
        parent::setUpBeforeClass();
27
28
        $conn = Shopware()->Db();
29
        $conn->delete('sw_connect_shop_config', ['s_shop = ?' => '_price_type']);
30
        $conn->insert('sw_connect_shop_config', ['s_shop' => '_price_type', 's_config' => 3]);
31
    }
32
33
    public function setUp()
34
    {
35
        parent::setUp();
36
37
        // disable auth and acl
38
        Shopware()->Plugins()->Backend()->Auth()->setNoAuth();
39
        Shopware()->Plugins()->Backend()->Auth()->setNoAcl();
40
41
        $this->manager = Shopware()->Models();
42
        $this->db = Shopware()->Db();
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
            $this->config,
50
            new ErrorHandler(),
51
            Shopware()->Container()->get('events')
52
        );
53
54
        $configs = [
55
            'priceGroupForPriceExport' => ['EK', null, 'export'],
56
            'priceGroupForPurchasePriceExport' => ['EK', null, 'export'],
57
            'priceFieldForPriceExport' => ['price', null, 'export'],
58
            'priceFieldForPurchasePriceExport' => ['detailPurchasePrice', null, 'export'],
59
        ];
60
61
        foreach ($configs as $name => $values) {
62
            list($value, $shopId, $group) = $values;
63
64
            $this->config->setConfig(
65
                $name,
66
                $value,
67
                $shopId,
68
                $group
69
            );
70
        }
71
    }
72
73
    public function testUpdatePrices()
74
    {
75
        $articleId = $this->insertVariants();
76
        $modelManager = $this->manager;
77
        $article = $modelManager->getRepository('Shopware\Models\Article\Article')->find($articleId);
78
79
        $connectAttribute = $this->getHelper()->getOrCreateConnectAttributeByModel($article);
80
        $connectAttribute->setExportStatus(null);
81
        $connectAttribute->setExported(false);
82
        $connectAttribute->setShopId(null);
83
        $this->manager->persist($connectAttribute);
84
        $this->manager->flush();
85
86
        $this->connectExport->export([$connectAttribute->getSourceId()]);
87
88
        $detail = $article->getMainDetail();
89
        $price = $detail->getPrices()->first();
90
91
        $this->Request()
92
            ->setMethod('POST')
93
            ->setPost('prices',
94
                [
95
                    0 =>
96
                        [
97
                            'id' => $price->getId(),
98
                            'from' => 1,
99
                            'to' => 5,
100
                            'price' => 238.00,
101
                            'pseudoPrice' => 0,
102
                            'percent' => 0,
103
                            'cloned' => false,
104
                            'customerGroupKey' => 'EK',
105
                            'customerGroup' =>
106
                                [
107
                                    0 =>
108
                                        [
109
                                            'id' => 1,
110
                                            'key' => 'EK',
111
                                            'name' => 'Shopkunden',
112
                                            'tax' => true,
113
                                            'taxInput' => true,
114
                                            'mode' => false,
115
                                            'discount' => 0,
116
                                        ],
117
                                ],
118
                        ],
119
                ])
120
            ->setPost('controller', 'Article')
121
            ->setPost('module', 'backend')
122
            ->setPost('action', 'saveDetail')
123
            ->setPost('number', 'llc1408017')
124
            ->setPost('price', 238.00)
125
            ->setPost('additionalText', 'L / Schwarz')
126
            ->setPost('supplierNumber', '')
127
            ->setPost('active', false)
128
            ->setPost('inStock', 15)
129
            ->setPost('stockMin', 0)
130
            ->setPost('weight', 0)
131
            ->setPost('kind', 1)
132
            ->setPost('position', 0)
133
            ->setPost('shippingFree', false)
134
            ->setPost('minPurchase', 1)
135
            ->setPost('purchasePrice', 38.99)
136
            ->setPost('articleId', $articleId)
137
            ->setPost('standard', false)
138
            ->setPost('id', $detail->getId());
139
        ;
140
141
        $this->dispatch('backend/Article/saveDetail');
142
143
        $this->assertEquals(200, $this->Response()->getHttpResponseCode());
144
        $this->assertTrue($this->View()->success);
145
146
        $changes = $this->db->query('SELECT * FROM sw_connect_change WHERE c_entity_id = ? ORDER BY c_revision', ['llc1408017'])->fetchAll();
147
        $this->assertCount(2, $changes);
148
        $updateChange = $changes[1];
149
        $this->assertEquals('update', $updateChange['c_operation']);
150
        $product = unserialize($updateChange['c_payload']);
151
        $this->assertEquals(200.00, $product->price);
152
153
    }
154
155
    private function insertVariants()
156
    {
157
        //clear connect_change table
158
        $this->db->exec('DELETE FROM sw_connect_change WHERE c_entity_id LIKE "llc1408017%"');
159
        //clear s_articles table
160
        $this->db->exec('DELETE FROM s_articles WHERE name = "LifecycleArticle"');
161
        //clear s_articles_detail table
162
        $this->db->exec('DELETE FROM s_articles_details WHERE ordernumber LIKE "llc1408017%"');
163
        //clear connect_items table
164
        $this->db->exec('DELETE FROM s_plugin_connect_items WHERE source_id LIKE "llc1408017%"');
165
166
        $minimalTestArticle = [
167
            'name' => 'LifecycleArticle',
168
            'active' => true,
169
            'tax' => 19,
170
            'supplier' => 'LifecycleArticle Inc.',
171
            'categories' => [
172
                ['id' => 15],
173
            ],
174
            'mainDetail' => [
175
                'number' => 'llc1408017',
176
            ],
177
        ];
178
179
        $articleResource = \Shopware\Components\Api\Manager::getResource('article');
180
        /** @var \Shopware\Models\Article\Article $article */
181
        $article = $articleResource->create($minimalTestArticle);
182
183
        $updateArticle = [
184
            'configuratorSet' => [
185
                'groups' => [
186
                    [
187
                        'name' => 'Größe',
188
                        'options' => [
189
                            ['name' => 'S'],
190
                            ['name' => 'M'],
191
                            ['name' => 'L'],
192
                            ['name' => 'XL'],
193
                            ['name' => 'XXL'],
194
                        ]
195
                    ],
196
                    [
197
                        'name' => 'Farbe',
198
                        'options' => [
199
                            ['name' => 'Weiß'],
200
                            ['name' => 'Gelb'],
201
                            ['name' => 'Blau'],
202
                            ['name' => 'Schwarz'],
203
                            ['name' => 'Rot'],
204
                        ]
205
                    ],
206
                ]
207
            ],
208
            'taxId' => 1,
209
            'variants' => [
210
                [
211
                    'isMain' => true,
212
                    'number' => 'llc1408017',
213
                    'inStock' => 15,
214
                    'standard' => null,
215
                    'additionaltext' => 'L / Schwarz',
216
                    'purchasePrice' => 38.99,
217
                    'configuratorOptions' => [
218
                        ['group' => 'Größe', 'groupId' => null, 'optionId' => null, 'option' => 'L'],
219
                        ['group' => 'Farbe', 'groupId' => null, 'optionId' => null, 'option' => 'Schwarz'],
220
                    ],
221
                ],
222
                [
223
                    'isMain' => false,
224
                    'number' => 'llc1408017-1',
225
                    'inStock' => 15,
226
                    'standard' => null,
227
                    'additionnaltext' => 'S / Schwarz',
228
                    'purchasePrice' => 38.99,
229
                    'configuratorOptions' => [
230
                        ['group' => 'Größe', 'groupId' => null, 'optionId' => null,'option' => 'S'],
231
                        ['group' => 'Farbe', 'groupId' => null, 'optionId' => null, 'option' => 'Schwarz'],
232
                    ],
233
                ],
234
                [
235
                    'isMain' => false,
236
                    'number' => 'llc1408017-2',
237
                    'inStock' => 15,
238
                    'standard' => null,
239
                    'additionnaltext' => 'S / Rot',
240
                    'purchasePrice' => 38.99,
241
                    'configuratorOptions' => [
242
                        ['group' => 'Größe', 'groupId' => null, 'optionId' => null,'option' => 'S'],
243
                        ['group' => 'Farbe', 'groupId' => null, 'optionId' => null,'option' => 'Rot'],
244
                    ],
245
                ],
246
                [
247
                    'isMain' => false,
248
                    'number' => 'llc1408017-3',
249
                    'inStock' => 15,
250
                    'standard' => null,
251
                    'additionnaltext' => 'XL / Rot',
252
                    'purchasePrice' => 38.99,
253
                    'configuratorOptions' => [
254
                        ['group' => 'Größe', 'groupId' => null, 'optionId' => null, 'option' => 'XL'],
255
                        ['group' => 'Farbe', 'groupId' => null, 'optionId' => null,'option' => 'Rot'],
256
                    ],
257
                ]
258
            ]
259
        ];
260
261
        /** @var \Shopware\Models\Article\Article $article */
262
        $article = $articleResource->update($article->getId(), $updateArticle);
263
264
        $this->db->insert(
265
            's_articles_prices',
266
            [
267
                'pricegroup' => 'EK',
268
                'from' => 1,
269
                'to' => 5,
270
                'price' => 123.99,
271
                'articleID' => $article->getId(),
272
                'articledetailsID' => $article->getMainDetail()->getId(),
273
                'pseudoprice' => 0
274
            ]
275
        );
276
277
        /** @var \Shopware\Models\Article\Detail $detail */
278
        foreach ($article->getDetails() as $detail) {
279
            $this->db->executeQuery(
280
                'INSERT INTO s_plugin_connect_items (article_id, article_detail_id, source_id, category, exported)
281
                  VALUES (?, ?, ?, ?, ?)
282
                  ON DUPLICATE KEY UPDATE
283
                  `category` = VALUES(`category`),
284
                  `exported` = VALUES(`exported`)',
285
                [
286
                    $article->getId(),
287
                    $detail->getId(),
288
                    $detail->getNumber(),
289
                    '/bücher',
290
                    1
291
                ]);
292
        }
293
294
        return $article->getId();
295
    }
296
}