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

ProductToShopTest   F

Complexity

Total Complexity 52

Size/Duplication

Total Lines 754
Duplicated Lines 13 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 52
c 0
b 0
f 0
lcom 1
cbo 11
dl 98
loc 754
rs 1.3333

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ProductToShopTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ProductToShopTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Tests\ShopwarePlugins\Connect;
4
5
use Shopware\Connect\Gateway\PDO;
6
use Shopware\Connect\Struct\Product;
7
use Shopware\Connect\Struct\ProductUpdate;
8
use Shopware\Connect\Struct\ShopConfiguration;
9
use Shopware\CustomModels\Connect\ProductStreamAttribute;
10
use Shopware\Models\ProductStream\ProductStream;
11
use ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver;
12
use ShopwarePlugins\Connect\Components\CategoryResolver\DefaultCategoryResolver;
13
use ShopwarePlugins\Connect\Components\Config;
14
use ShopwarePlugins\Connect\Components\Gateway\ProductTranslationsGateway\PdoProductTranslationsGateway;
15
use ShopwarePlugins\Connect\Components\Marketplace\MarketplaceGateway;
16
use ShopwarePlugins\Connect\Components\ProductToShop;
17
use ShopwarePlugins\Connect\Components\VariantConfigurator;
18
use Shopware\Models\Article\Article;
19
use Shopware\Models\Property;
20
21
class ProductToShopTest extends ConnectTestHelper
22
{
23
    /** @var  \ShopwarePlugins\Connect\Components\ProductToShop */
24
    private $productToShop;
25
26
    /**
27
     * @var \Shopware\Components\Model\ModelManager
28
     */
29
    private $modelManager;
30
31
    private $db;
32
33
    /**
34
     * @var \Shopware\Connect\Gateway
35
     */
36
    private $gateway;
37
38
    public function tearDown()
39
    {
40
        $conn = Shopware()->Db();
41
        $conn->delete('s_plugin_connect_config', array('name = ?' => 'activateProductsAutomatically'));
42
        $conn->delete('s_plugin_connect_config', array('name = ?' => 'createUnitsAutomatically'));
43
        $conn->delete('s_plugin_connect_config', array('name = ?' => 'yd'));
44
        $conn->delete('s_plugin_connect_config', array('name = ?' => 'm'));
45
    }
46
47
    public function setUp()
48
    {
49
        parent::setUp();
50
51
        $this->db = Shopware()->Db();
52
        $this->db->delete('s_plugin_connect_config', array('name = ?' => 'activateProductsAutomatically'));
53
        $this->db->delete('s_plugin_connect_config', array('name = ?' => 'createUnitsAutomatically'));
54
55
        $this->gateway = new PDO($this->db->getConnection());
56
57
        $this->modelManager = Shopware()->Models();
58
        $this->productToShop = new ProductToShop(
59
            $this->getHelper(),
60
            $this->modelManager,
61
            $this->getImageImport(),
62
            new Config($this->modelManager),
63
            new VariantConfigurator(
64
                $this->modelManager,
65
                new PdoProductTranslationsGateway(Shopware()->Db())
66
            ),
67
            new MarketplaceGateway($this->modelManager),
68
            new PdoProductTranslationsGateway(Shopware()->Db()),
69
            new DefaultCategoryResolver(
70
                $this->modelManager,
71
                $this->modelManager->getRepository('Shopware\CustomModels\Connect\RemoteCategory'),
72
                $this->modelManager->getRepository('Shopware\CustomModels\Connect\ProductToRemoteCategory')
73
            ),
74
            $this->gateway,
75
            Shopware()->Container()->get('events')
76
        );
77
    }
78
79
    private function deleteVendorIfExists($vendorName)
80
    {
81
        $vendorCount = $this->db->query(
82
            'SELECT COUNT(id)
83
              FROM s_articles_supplier as supplier
84
              WHERE supplier.name = :supplierName',
85
            array('supplierName' => $vendorName)
86
        )->fetchColumn();
87
88
        if ($vendorCount) {
89
            Shopware()->Db()->delete('s_articles_supplier', array('name=?' => $vendorName));
90
        }
91
    }
92
93
    public function truncateProperties()
94
    {
95
        $this->db->query("SET FOREIGN_KEY_CHECKS = 0");
96
        $this->db->query("TRUNCATE s_filter_articles");
97
        $this->db->query("TRUNCATE s_filter_relations");
98
        $this->db->query("TRUNCATE s_filter_attributes");
99
        $this->db->query("TRUNCATE s_filter");
100
        $this->db->query("TRUNCATE s_filter_options_attributes");
101
        $this->db->query("TRUNCATE s_filter_options");
102
        $this->db->query("TRUNCATE s_filter_values_attributes");
103
        $this->db->query("TRUNCATE s_filter_values");
104
        $this->db->query("SET FOREIGN_KEY_CHECKS = 1");
105
    }
106
107
    public function testInsertArticle()
108
    {
109
        $product = $this->getProduct();
110
        $product->minPurchaseQuantity = 5;
111
112
        $this->productToShop->insertOrUpdate($product);
113
114
        $articlesCount = Shopware()->Db()->query(
115
            'SELECT COUNT(s_articles.id)
116
              FROM s_plugin_connect_items
117
              LEFT JOIN s_articles ON (s_plugin_connect_items.article_id = s_articles.id)
118
              WHERE s_plugin_connect_items.source_id = :sourceId',
119
            array('sourceId' => $product->sourceId)
120
        )->fetchColumn();
121
122
        $this->assertEquals(1, $articlesCount);
123
124
        /** @var \Shopware\CustomModels\Connect\Attribute $connectAttribute */
125
        $connectAttribute = $this->modelManager
126
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
127
            ->findOneBy(array('sourceId' => $product->sourceId));
128
        $detail = $connectAttribute->getArticleDetail();
129
        
130
        $this->assertEquals($product->minPurchaseQuantity, $detail->getMinPurchase());
131
    }
132
133
    public function testInsertArticleTranslations()
134
    {
135
        $product = $this->getProduct();
136
        $this->productToShop->insertOrUpdate($product);
137
        $productRepository = Shopware()->Models()->getRepository('Shopware\Models\Article\Article');
138
        /** @var \Shopware\Models\Article\Article $productModel */
139
        $productModel = $productRepository->findOneBy(array('name' => $product->title));
140
141
        $articleTranslation = Shopware()->Db()->query(
142
            'SELECT objectdata
143
              FROM s_core_translations
144
              WHERE objectkey = :productId AND objectlanguage = 2 AND objecttype = :objectType',
145
            array('productId' => $productModel->getId(), 'objectType' => 'article')
146
        )->fetchColumn();
147
148
        $this->assertNotFalse($articleTranslation);
149
        $articleTranslation = unserialize($articleTranslation);
150
151
        $additionalDescription = array_key_exists(PdoProductTranslationsGateway::CONNECT_DESCRIPTION, $articleTranslation) ? $articleTranslation[PdoProductTranslationsGateway::CONNECT_DESCRIPTION] : '';
152
153
        $this->assertEquals($product->translations['en']->title, $articleTranslation['txtArtikel']);
154
        $this->assertEquals($product->translations['en']->longDescription, $articleTranslation['txtlangbeschreibung']);
155
        $this->assertEquals($product->translations['en']->shortDescription, $articleTranslation['txtshortdescription']);
156
        $this->assertEquals($product->translations['en']->additionalDescription, $additionalDescription);
157
    }
158
159
    public function testInsertVariantOptionsAndGroupsTranslations()
160
    {
161
        $variants = $this->getVariants();
162
        // insert variants
163
        foreach ($variants as $variant) {
164
            $this->productToShop->insertOrUpdate($variant);
165
        }
166
167
        $groupRepository = Shopware()->Models()->getRepository('Shopware\Models\Article\Configurator\Group');
168
        $optionRepository = Shopware()->Models()->getRepository('Shopware\Models\Article\Configurator\Option');
169
        foreach ($variants as $variant) {
170
            foreach ($variant->translations as $translation) {
171
                // check configurator group translations
172
                foreach ($translation->variantLabels as $groupKey => $groupTranslation) {
173
                    $group = $groupRepository->findOneBy(array('name' => $groupKey));
174
175
                    $objectData = Shopware()->Db()->query(
176
                        'SELECT objectdata
177
                          FROM s_core_translations
178
                          WHERE objectkey = :groupId AND objectlanguage = 2 AND objecttype = :objectType',
179
                        array('groupId' => $group->getId(), 'objectType' => 'configuratorgroup')
180
                    )->fetchColumn();
181
182
                    $objectData = unserialize($objectData);
183
                    $this->assertEquals($groupTranslation, $objectData['name']);
184
                }
185
186
                foreach ($translation->variantValues as $optionKey => $optionTranslation) {
187
                    $option =  $optionRepository->findOneBy(array('name' => $optionKey));
188
                    $objectData = Shopware()->Db()->query(
189
                        'SELECT objectdata
190
                          FROM s_core_translations
191
                          WHERE objectkey = :optionId AND objectlanguage = 2 AND objecttype = :objectType',
192
                        array('optionId' => $option->getId(), 'objectType' => 'configuratoroption')
193
                    )->fetchColumn();
194
195
                    $objectData = unserialize($objectData);
196
                    $this->assertEquals($optionTranslation, $objectData['name']);
197
                }
198
            }
199
        }
200
    }
201
202
    public function testInsertVariants()
203
    {
204
        $variants = $this->getVariants();
205
206
        foreach ($variants as $variant) {
207
            $this->productToShop->insertOrUpdate($variant);
208
        }
209
210
        $connectAttribute = $this->modelManager
211
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
212
            ->findOneBy(array('sourceId' => $variants[0]->sourceId));
213
        $article = $connectAttribute->getArticle();
214
        // check articles details count
215
        $this->assertEquals(4, count($article->getDetails()));
216
        // check configurator set
217
        $this->assertNotNull($article->getConfiguratorSet());
218
        // check configurator group
219
        $group = $this->modelManager
220
            ->getRepository('Shopware\Models\Article\Configurator\Group')
221
            ->findOneBy(array('name' => 'Farbe'));
222
        $this->assertNotNull($group);
223
        // check group options
224
        $groupOptionValues = $articleOptionValues = array('Weiss-Blau', 'Weiss-Rot', 'Blau-Rot', 'Schwarz-Rot');
225
        foreach ($group->getOptions() as $option) {
226
            foreach ($groupOptionValues as $key => $groupOptionValue) {
227
                if (strpos($option->getName(), $groupOptionValue) == 0) {
228
                    unset($groupOptionValues[$key]);
229
                }
230
            }
231
        }
232
        $this->assertEmpty($groupOptionValues);
233
        // check configuration set options
234
        $this->assertEquals(4, count($article->getConfiguratorSet()->getOptions()));
235
        foreach ($article->getConfiguratorSet()->getOptions() as $option) {
236
            foreach ($articleOptionValues as $key => $articleOptionValue) {
237
                if (strpos($option->getName(), $articleOptionValue) == 0) {
238
                    unset($articleOptionValues[$key]);
239
                }
240
            }
241
        }
242
        $this->assertEmpty($articleOptionValues);
243
    }
244
245
    public function testUpdateVariant()
246
    {
247
        $variants = $this->getVariants();
248
        // insert variants
249
        foreach ($variants as $variant) {
250
            $this->productToShop->insertOrUpdate($variant);
251
        }
252
253
        $newTitle = 'Massimport#updateVariant' . rand(1, 10000000);
254
        $newPrice = 22.48;
255
        $newPurchasePrice = 8.48;
256
        $newLongDesc = 'Updated connect variant - long description';
257
        $newShortDesc = 'Updated connect variant - short description';
258
        $newVat = 0.07;
259
        $newSku = $variants[1]->sku . 'M';
260
        $variants[1]->title = $newTitle;
261
        $variants[1]->price = $newPrice;
262
        $variants[1]->purchasePrice = $newPurchasePrice;
263
        $variants[1]->longDescription = $newLongDesc;
264
        $variants[1]->shortDescription = $newShortDesc;
265
        $variants[1]->images[] = self::IMAGE_PROVIDER_URL . '?' . $variants[1]->sourceId;
266
        $variants[1]->variantImages[] = self::IMAGE_PROVIDER_URL . '?' . $variants[1]->sourceId;
267
        $variants[1]->vat = $newVat;
268
        $variants[1]->sku = $newSku;
269
270
        $this->productToShop->insertOrUpdate($variants[1]);
271
272
        /** @var \Shopware\CustomModels\Connect\Attribute $connectAttribute */
273
        $connectAttribute = $this->modelManager
274
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
275
            ->findOneBy(array('sourceId' => $variants[1]->sourceId));
276
        $this->assertEquals($newTitle, $connectAttribute->getArticle()->getName());
277
        $this->assertEquals($newLongDesc, $connectAttribute->getArticle()->getDescriptionLong());
278
        $this->assertEquals($newShortDesc, $connectAttribute->getArticle()->getDescription());
279
        $detail = $connectAttribute->getArticleDetail();
280
        /** @var \Shopware\Models\Article\Price[] $prices */
281
        $prices = $detail->getPrices();
282
283
        $this->assertEquals($newPrice, $prices[0]->getPrice());
284
        if (method_exists($detail, 'getPurchasePrice')) {
285
            $this->assertEquals($newPurchasePrice, $detail->getPurchasePrice());
286
        } else {
287
            $this->assertEquals($newPurchasePrice, $prices[0]->getBasePrice());
288
        }
289
290
        $this->assertEquals(2, $connectAttribute->getArticle()->getImages()->count());
291
        $this->assertEquals(1, $detail->getImages()->count());
292
        $this->assertEquals(7.00, $connectAttribute->getArticle()->getTax()->getTax());
293
        $this->assertEquals('SC-3-' . $newSku, $detail->getNumber());
294
    }
295
296
    public function testImportWithoutTitle()
297
    {
298
        $product = new Product();
299
        $this->assertEmpty($this->productToShop->insertOrUpdate($product));
300
    }
301
302
    public function testImportWithoutVendor()
303
    {
304
        $product = new Product();
305
        $this->assertEmpty($this->productToShop->insertOrUpdate($product));
306
    }
307
308
    public function testDelete()
309
    {
310
        $variants = $this->getVariants();
311
        // insert variants
312
        foreach ($variants as $variant) {
313
            $this->productToShop->insertOrUpdate($variant);
314
        }
315
316
        // test delete only one variant
317
        $this->productToShop->delete($variants[1]->shopId, $variants[1]->sourceId);
318
319
        $connectAttribute = $this->modelManager
320
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
321
            ->findOneBy(array('sourceId' => $variants[2]->sourceId));
322
323
        $article = $connectAttribute->getArticle();
324
        // check articles details count
325
        $this->assertEquals(3, count($article->getDetails()));
326
327
        // test delete article - main article variant
328
        $this->productToShop->delete($variants[0]->shopId, $variants[0]->sourceId);
329
330
        $articlesCount = Shopware()->Db()->query(
331
            'SELECT COUNT(s_articles.id)
332
              FROM s_plugin_connect_items
333
              LEFT JOIN s_articles ON (s_plugin_connect_items.article_id = s_articles.id)
334
              WHERE s_plugin_connect_items.source_id = :sourceId',
335
            array('sourceId' => $variants[0]->sourceId)
336
        )->fetchColumn();
337
338
        $this->assertEquals(0, $articlesCount);
339
340
        $attributesCount = Shopware()->Db()->query(
341
            'SELECT COUNT(s_plugin_connect_items.id)
342
              FROM s_plugin_connect_items
343
              WHERE s_plugin_connect_items.article_id = :articleId',
344
            array('articleId' => $article->getId())
345
        )->fetchColumn();
346
347
        $this->assertEquals(2, $attributesCount);
348
    }
349
350
    public function testInsertPurchasePriceHash()
351
    {
352
        $product = $this->getProduct();
353
        $this->productToShop->insertOrUpdate($product);
354
355
        $articlesCount = Shopware()->Db()->query(
356
            'SELECT COUNT(s_articles.id)
357
              FROM s_plugin_connect_items
358
              LEFT JOIN s_articles ON (s_plugin_connect_items.article_id = s_articles.id)
359
              WHERE s_plugin_connect_items.purchase_price_hash = :purchasePriceHash
360
              AND s_plugin_connect_items.offer_valid_until = :offerValidUntil
361
              AND s_plugin_connect_items.source_id = :sourceId',
362
            array(
363
                'purchasePriceHash' => $product->purchasePriceHash,
364
                'offerValidUntil' => $product->offerValidUntil,
365
                'sourceId' => $product->sourceId,
366
            )
367
        )->fetchColumn();
368
369
        $this->assertEquals(1, $articlesCount);
370
    }
371
372
    public function testUpdate()
373
    {
374
        $product = $this->getProduct();
375
        $this->productToShop->insertOrUpdate($product);
376
377
        $articlesCount = Shopware()->Db()->query(
378
            'SELECT COUNT(s_articles.id)
379
              FROM s_plugin_connect_items
380
              LEFT JOIN s_articles ON (s_plugin_connect_items.article_id = s_articles.id)
381
              WHERE s_plugin_connect_items.source_id = :sourceId',
382
            array('sourceId' => $product->sourceId)
383
        )->fetchColumn();
384
385
        $this->assertEquals(1, $articlesCount);
386
387
        $purchasePrice = 8.99;
388
        $offerValidUntil = time() + 1 * 365 * 24 * 60 * 60; // One year
389
        $productUpdate = new ProductUpdate(array(
390
            'price' => 10.99,
391
            'purchasePrice' => $purchasePrice,
392
            'purchasePriceHash' => hash_hmac(
393
                'sha256',
394
                sprintf('%.3F %d', $purchasePrice, $offerValidUntil), '54642546-0001-48ee-b4d0-4f54af66d822'
395
            ),
396
            'offerValidUntil' => $offerValidUntil,
397
            'availability' => 80,
398
        ));
399
400
        $this->productToShop->update($product->shopId, $product->sourceId, $productUpdate);
401
402
        /** @var \Shopware\CustomModels\Connect\Attribute $connectAttribute */
403
        $connectAttribute = $this->modelManager
404
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
405
            ->findOneBy(array('sourceId' => $product->sourceId));
406
407
        $this->assertEquals($productUpdate->purchasePriceHash, $connectAttribute->getPurchasePriceHash());
408
        $this->assertEquals($productUpdate->offerValidUntil, $connectAttribute->getOfferValidUntil());
409
        $this->assertEquals($productUpdate->purchasePrice, $connectAttribute->getPurchasePrice());
410
411
        $this->assertEquals($productUpdate->availability, $connectAttribute->getArticleDetail()->getInStock());
412
        $detail = $connectAttribute->getArticleDetail();
413
        /** @var \Shopware\Models\Article\Price[] $prices */
414
        $prices = $detail->getPrices();
415
        $this->assertEquals($productUpdate->price, $prices[0]->getPrice());
416
        if (method_exists($detail, 'getPurchasePrice')) {
417
            $this->assertEquals($productUpdate->purchasePrice, $detail->getPurchasePrice());
418
        } else {
419
            $this->assertEquals($productUpdate->purchasePrice, $prices[0]->getBasePrice());
420
        }
421
    }
422
423
    public function testChangeAvailability()
424
    {
425
        $product = $this->getProduct();
426
        $this->productToShop->insertOrUpdate($product);
427
428
        $articlesCount = Shopware()->Db()->query(
429
            'SELECT COUNT(s_articles.id)
430
              FROM s_plugin_connect_items
431
              LEFT JOIN s_articles ON (s_plugin_connect_items.article_id = s_articles.id)
432
              WHERE s_plugin_connect_items.source_id = :sourceId',
433
            array('sourceId' => $product->sourceId)
434
        )->fetchColumn();
435
436
        $this->assertEquals(1, $articlesCount);
437
438
        $newAvailability = 20;
439
        $this->productToShop->changeAvailability($product->shopId, $product->sourceId, $newAvailability);
440
441
        /** @var \Shopware\CustomModels\Connect\Attribute $connectAttribute */
442
        $connectAttribute = $this->modelManager
443
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
444
            ->findOneBy(array('sourceId' => $product->sourceId));
445
446
        $this->assertEquals($newAvailability, $connectAttribute->getArticleDetail()->getInStock());
447
    }
448
449
    public function testMakeMainVariant()
450
    {
451
        $variants = $this->getVariants();
452
453
        foreach ($variants as $variant) {
454
            $this->productToShop->insertOrUpdate($variant);
455
        }
456
457
        $this->productToShop->makeMainVariant($variants[3]->shopId, $variants[3]->sourceId, null);
458
459
        /** @var \Shopware\CustomModels\Connect\Attribute $connectAttribute */
460
        $connectAttribute = $this->modelManager
461
            ->getRepository('Shopware\CustomModels\Connect\Attribute')
462
            ->findOneBy(array('sourceId' => $variants[3]->sourceId));
463
464
        $this->assertEquals(1, $connectAttribute->getArticleDetail()->getKind());
465
        $this->assertEquals(
466
            $connectAttribute->getArticleDetailId(),
467
            $connectAttribute->getArticle()->getMainDetail()->getId()
468
        );
469
    }
470
471
    public function testInsertArticleAndAutomaticallyCreateCategories()
472
    {
473
        $config =  new \ShopwarePlugins\Connect\Components\Config($this->modelManager);
474
        $productToShop = new ProductToShop(
475
            $this->getHelper(),
476
            $this->modelManager,
477
            $this->getImageImport(),
478
            new Config($this->modelManager),
479
            new VariantConfigurator(
480
                $this->modelManager,
481
                new PdoProductTranslationsGateway(Shopware()->Db())
482
            ),
483
            new MarketplaceGateway($this->modelManager),
484
            new PdoProductTranslationsGateway(Shopware()->Db()),
485
            new AutoCategoryResolver(
486
                $this->modelManager,
487
                $this->modelManager->getRepository('Shopware\Models\Category\Category'),
488
                $this->modelManager->getRepository('Shopware\CustomModels\Connect\RemoteCategory'),
489
                $config
490
            ),
491
            $this->gateway,
492
            Shopware()->Container()->get('events')
493
        );
494
495
        $product = $this->getProduct();
496
        $parentCategory1 = 'MassImport#' . rand(1, 999999999);
497
        $childCategory = 'MassImport#' . rand(1, 999999999);
498
        $parentCategory2 = 'MassImport#' . rand(1, 999999999);
499
        // add custom categories
500
        $product->categories = array_merge($product->categories, array(
501
            '/' . strtolower($parentCategory1) => $parentCategory1,
502
            '/' . strtolower($parentCategory1) . '/' . strtolower($childCategory) => $childCategory,
503
            '/' . strtolower($parentCategory2) => $parentCategory2,
504
        ));
505
506
        $productToShop->insertOrUpdate($product);
507
508
        $categoryRepository = Shopware()->Models()->getRepository('Shopware\Models\Category\Category');
509
        /** @var \Shopware\Models\Category\Category $childCategoryModel */
510
        $childCategoryModel = $categoryRepository->findOneBy(array('name' => $childCategory));
511
512
        $this->assertInstanceOf('Shopware\Models\Category\Category', $childCategoryModel);
513
        $this->assertEquals(
514
            $config->getDefaultShopCategory()->getName(),
515
            $childCategoryModel->getParent()->getName()
516
        );
517
518
        /** @var Article $article */
519
        $article = $this->modelManager->getRepository(Article::class)->findOneByName($product->title);
520
521
        $assignCategories = $article->getCategories();
522
        $this->assertEquals(1, count($assignCategories));
523
        $this->assertEquals($childCategory, $assignCategories[0]->getName());
524
    }
525
526
    public function testAutomaticallyCreateUnits()
527
    {
528
        $conn = Shopware()->Db();
529
        $conn->insert('s_plugin_connect_config', array(
530
            'name' => 'createUnitsAutomatically',
531
            'value' => '1'
532
        ));
533
        $product = $this->getProduct();
534
        $unit = 'yd';
535
        $product->attributes['unit'] = $unit;
536
        $product->attributes['quantity'] = 1;
537
        $product->attributes['ref_quantity'] = 5;
538
539
        $this->productToShop->insertOrUpdate($product);
540
541
        /** @var \Shopware\Models\Article\Article $article */
542
        $article = $this->modelManager->getRepository('Shopware\Models\Article\Article')->findOneBy(array(
543
            'name' => $product->title
544
        ));
545
        $this->assertInstanceOf('Shopware\Models\Article\Article', $article);
546
        $this->assertInstanceOf('Shopware\Models\Article\Unit', $article->getMainDetail()->getUnit());
547
        $this->assertEquals('yd', $article->getMainDetail()->getUnit()->getUnit());
548
        $this->assertEquals('Yard', $article->getMainDetail()->getUnit()->getName());
549
    }
550
551
    /**
552
     * Connect units must be stored in config table
553
     * during product import
554
     *
555
     * @throws \Zend_Db_Statement_Exception
556
     */
557
    public function testStoreUnitsOnProductImport()
558
    {
559
        $product = $this->getProduct();
560
        $unit = 'm';
561
        $product->attributes['unit'] = $unit;
562
        $product->attributes['quantity'] = 1;
563
        $product->attributes['ref_quantity'] = 5;
564
565
        $this->productToShop->insertOrUpdate($product);
566
567
        $query = Shopware()->Db()->query(
568
            'SELECT COUNT(id)
569
              FROM s_plugin_connect_config
570
              WHERE `name` = :configName
571
              AND groupName = :groupName',
572
            array('configName' => 'm', 'groupName' => 'units')
573
        );
574
575
        $this->assertEquals(1, $query->fetchColumn());
576
    }
577
578
    public function testInsertArticleVendorArray()
579
    {
580
        $product = $this->getProduct();
581
        $this->deleteVendorIfExists($product->vendor['name']);
582
583
        $this->productToShop->insertOrUpdate($product);
584
585
        $supplier = $this->db->query(
586
            'SELECT *
587
              FROM s_articles_supplier as supplier
588
              WHERE supplier.name = :supplierName',
589
            array('supplierName' => $product->vendor['name'])
590
        )->fetchObject();
591
592
        $this->assertEquals($product->vendor['name'], $supplier->name);
593
        $this->assertEquals($product->vendor['url'], $supplier->link);
594
        $this->assertEquals($product->vendor['description'], $supplier->description);
595
        $this->assertEquals($product->vendor['page_title'], $supplier->meta_title);
596
    }
597
598
    public function testInsertArticleVendorString()
599
    {
600
        $product = $this->getProduct();
601
        $product->vendor = 'shopware Connect';
602
        $this->deleteVendorIfExists($product->vendor);
603
604
        $this->productToShop->insertOrUpdate($product);
605
        $supplier = $this->db->query(
606
            'SELECT *
607
              FROM s_articles_supplier as supplier
608
              WHERE supplier.name = :supplierName',
609
            array('supplierName' => $product->vendor)
610
        )->fetchObject();
611
612
        $supplierAttr = $this->db->query(
613
            'SELECT *
614
              FROM s_articles_supplier_attributes as sa
615
              WHERE sa.supplierID = :supplierId',
616
            array('supplierId' => $supplier->id)
617
        )->fetchObject();
618
619
        $this->assertEquals($product->vendor, $supplier->name);
620
        $this->assertEquals(1, $supplierAttr->connect_is_remote);
621
    }
622
623
    public function testInsertArticleProperties()
624
    {
625
        $product = $this->getProduct();
626
        $product->properties = $this->getProperties();
627
628
        $this->truncateProperties();
629
630
        $this->productToShop->insertOrUpdate($product);
631
632
        /** @var Article $article */
633
        $article = $this->modelManager->getRepository(Article::class)->findOneBy(array(
634
            'name' => $product->title
635
        ));
636
637
        /** @var \Shopware\Connect\Struct\Property $firstProperty */
638
        $firstProperty = reset($product->properties);
639
640
        /** @var Property\Group $group */
641
        $group = $this->modelManager->getRepository(Property\Group::class)->findOneBy(array(
642
            'name' => $firstProperty->groupName
643
        ));
644
645
        $this->assertEquals(3, count($article->getPropertyValues()));
646
        $this->assertEquals(2, count($group->getOptions()));
647
        $this->assertEquals($firstProperty->groupName, $article->getPropertyGroup()->getName());
648
649
        /** @var Property\Value $propertyValue */
650
        foreach ($article->getPropertyValues() as $index => $propertyValue) {
651
            $property = $product->properties[$index];
652
            $this->assertEquals($property->value, $propertyValue->getValue());
653
            $this->assertEquals($property->valuePosition, $propertyValue->getPosition());
654
        }
655
656
        /** @var Property\Option $option */
657
        foreach ($group->getOptions() as $index => $option) {
658
            $property = $product->properties[$index];
659
            $this->assertEquals($property->option, $option->getName());
660
        }
661
    }
662
663
    public function testCreateStreamAndAddProductToStream()
664
    {
665
        $product = $this->getProduct();
666
        $this->productToShop->insertOrUpdate($product);
667
668
        /** @var Article $article */
669
        $article = $this->modelManager->getRepository(Article::class)->findOneBy(array(
670
            'name' => $product->title
671
        ));
672
673
        /** @var ProductStream $stream */
674
        $stream = $this->modelManager->getRepository(ProductStream::class)->findOneBy(['name' => $product->stream]);
675
        $this->assertNotNull($stream);
676
        $this->assertEquals(1, $stream->getAttribute()->getConnectIsRemote());
677
678
        $connection = $this->modelManager->getConnection();
679
        $builder = $connection->createQueryBuilder();
680
        $builder->select('*')
681
            ->from('s_product_streams_selection')
682
            ->where('stream_id = :streamId')
683
            ->andWhere('article_id = :articleId')
684
            ->setParameter('streamId', $stream->getId())
685
            ->setParameter('articleId', $article->getId());
686
687
        $this->assertNotEmpty($builder->execute()->fetchAll());
688
    }
689
690
    public function testAutomaticallyActivateArticles()
691
    {
692
        $conn = Shopware()->Db();
693
        $conn->insert('s_plugin_connect_config', array(
694
            'name' => 'activateProductsAutomatically',
695
            'value' => '1',
696
            'groupName' => 'general',
697
        ));
698
699
        $product = $this->getProduct();
700
        $this->productToShop->insertOrUpdate($product);
701
        /** @var \Shopware\Models\Article\Article $article */
702
        $article = $this->modelManager->getRepository('Shopware\Models\Article\Article')->findOneBy(array(
703
            'name' => $product->title
704
        ));
705
        $this->assertInstanceOf('Shopware\Models\Article\Article', $article);
706
        $this->assertInstanceOf('Shopware\Models\Article\Detail', $article->getMainDetail());
707
        $this->assertTrue($article->getActive(), 'Article is activated');
708
        $this->assertEquals(1, $article->getMainDetail()->getActive(), 'Detail is activated');
709
    }
710
711
    public function testInsertArticleWithSellNotInStock()
712
    {
713
        $product = $this->getProduct();
714
715
        $shopConfiguration = new ShopConfiguration();
716
        $shopConfiguration->sellNotInStock = true;
717
718
        $this->gateway->setShopConfiguration($product->shopId, $shopConfiguration);
719
720
        $this->productToShop->insertOrUpdate($product);
721
        /** @var \Shopware\Models\Article\Article $article */
722
        $article = $this->modelManager->getRepository('Shopware\Models\Article\Article')->findOneBy(array(
723
            'name' => $product->title
724
        ));
725
726
        $this->assertFalse($article->getLastStock());
727
    }
728
729
    /**
730
     * Test inserting variant with same values (Black for example)
731
     * for 1st and 2nd color
732
     */
733
    public function testInsertVariantWithSameValues()
734
    {
735
        $variants = $this->getVariants();
736
        // duplicate color value
737
        $variants[0]->variant['Farbe2'] = $variants[0]->variant['Farbe'];
738
        // insert variants
739
        foreach ($variants as $variant) {
740
            $this->productToShop->insertOrUpdate($variant);
741
        }
742
743
        $group = $this->modelManager
744
            ->getRepository('Shopware\Models\Article\Configurator\Group')
745
            ->findOneBy(array('name' => 'Farbe'));
746
        $this->assertNotNull($group);
747
748
        $group2 = $this->modelManager
749
            ->getRepository('Shopware\Models\Article\Configurator\Group')
750
            ->findOneBy(array('name' => 'Farbe2'));
751
        $this->assertNotNull($group2);
752
753
        // check group options
754
        $colorValue = null;
755
        foreach ($group->getOptions() as $option) {
756
            if ($option->getName() == $variants[0]->variant['Farbe']) {
757
                $colorValue = $variants[0]->variant['Farbe2'];
758
            }
759
        }
760
761
        $colorValue2 = null;
762
        foreach ($group2->getOptions() as $option) {
763
            if ($option->getName() == $variants[0]->variant['Farbe2']) {
764
                $colorValue2 = $variants[0]->variant['Farbe2'];
765
            }
766
        }
767
768
        $this->assertNotNull($colorValue);
769
        $this->assertNotNull($colorValue2);
770
        $this->assertEquals($colorValue, $colorValue2);
771
        $this->assertEquals(0, strpos($colorValue, 'Schwarz-Rot'));
772
        $this->assertEquals(0, strpos($colorValue2, 'Schwarz-Rot'));
773
    }
774
}
775