Completed
Pull Request — master (#333)
by Simon
04:47
created

SDKTest::testExportProductWithoutPurchasePrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 14
nc 2
nop 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Tests\ShopwarePlugins\Connect;
9
10
use Shopware\Connect\SDK;
11
use Shopware\Connect\Struct\Product;
12
13
class SDKTest extends ConnectTestHelper
14
{
15
    /**
16
     * @var \Shopware\Components\Model\ModelManager
17
     */
18
    private $manager;
19
20
    /**
21
     * @var \Enlight_Components_Db_Adapter_Pdo_Mysql
22
     */
23
    private $db;
24
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        $this->manager = Shopware()->Models();
30
        $this->db = Shopware()->Db();
31
        $this->db->delete('sw_connect_shop_config', ['s_shop = ?' => '_price_type']);
32
        $this->db->insert('sw_connect_shop_config', ['s_shop' => '_price_type', 's_config' => SDK::PRICE_TYPE_BOTH]);
33
34
        $this->db->executeQuery(
35
            'DELETE FROM `s_plugin_connect_config` WHERE `name` = "priceFieldForPurchasePriceExport"'
36
        );
37
38
        $this->db->executeQuery(
39
            'INSERT INTO `s_plugin_connect_config`(`name`, `value`, `groupName`)
40
            VALUES ("priceFieldForPurchasePriceExport", "detailPurchasePrice", "export")'
41
        );
42
43
        parent::setUp();
44
    }
45
46
    public function testExportProductWithoutPurchasePrice()
47
    {
48
        $article = $this->getLocalArticle();
49
        $prices = $article->getMainDetail()->getPrices();
50
        if (method_exists('Shopware\Models\Article\Detail', 'setPurchasePrice')) {
51
            $article->getMainDetail()->setPurchasePrice(null);
52
            $this->manager->persist($article->getMainDetail());
53
        } else {
54
            $prices[0]->setBasePrice(null);
55
            $this->manager->Models()->persist($prices[0]);
56
        }
57
58
        $this->manager->flush();
59
60
        $this->getConnectExport()->export([$article->getId()]);
61
62
63
        /** @var \Shopware\CustomModels\Connect\Attribute $model */
64
        $model = $this->manager->getRepository('Shopware\CustomModels\Connect\Attribute')->findOneBy(['sourceId' => $article->getId()]);
65
        $message = $model->getExportMessage();
66
67
        $this->assertContains('Ein Preisfeld für dieses Produkt ist nicht gepfegt', $message);
68
    }
69
70
    public function testHandleProductUpdates()
71
    {
72
        // pseudo verify SDK
73
        $this->db->delete('sw_connect_shop_config', []);
74
        $this->db->insert('sw_connect_shop_config', ['s_shop' => '_self_', 's_config' => -1]);
75
        $this->db->insert('sw_connect_shop_config', ['s_shop' => '_last_update_', 's_config' => time()]);
76
        $this->db->insert('sw_connect_shop_config', ['s_shop' => '_categories_', 's_config' => serialize(['/bücher' => 'Bücher'])]);
77
78
        $offerValidUntil = time() + 1 * 365 * 24 * 60 * 60; // One year
79
        $purchasePrice = 6.99;
80
        $this->dispatchRpcCall('products', 'toShop', [
81
            [
82
                new \Shopware\Connect\Struct\Change\ToShop\InsertOrUpdate([
83
                    'product' => new \Shopware\Connect\Struct\Product([
84
                        'shopId' => 3,
85
                        'revisionId' => time(),
86
                        'sourceId' => 'ABCDEFGH' . time(),
87
                        'ean' => '1234',
88
                        'url' => 'http://shopware.de',
89
                        'title' => 'shopware Connect Test-Produkt',
90
                        'shortDescription' => 'Ein Produkt aus shopware Connect',
91
                        'longDescription' => 'Ein Produkt aus shopware Connect',
92
                        'additionalDescription' => 'Ein Produkt aus shopware Connect',
93
                        'vendor' => 'shopware Connect',
94
                        'stream' => 'Awesome products',
95
                        'price' => 9.99,
96
                        'purchasePrice' => $purchasePrice,
97
                        'purchasePriceHash' => hash_hmac(
98
                            'sha256',
99
                            sprintf('%.3F %d', $purchasePrice, $offerValidUntil), '54642546-0001-48ee-b4d0-4f54af66d822'
100
                        ),
101
                        'offerValidUntil' => $offerValidUntil,
102
                        'availability' => 100,
103
                        'images' => [self::IMAGE_PROVIDER_URL],
104
                        'categories' => ['/bücher' => 'Bücher'],
105
                    ]),
106
                    'revision' => time(),
107
                ])
108
            ]
109
        ]);
110
    }
111
112
    public function testExportProductWithPurchasePrice()
113
    {
114
        $model = $this->manager->getRepository('Shopware\CustomModels\Connect\Attribute')->findOneBy(['articleId' => 3]);
115
        $model->setExportMessage(null);
116
        $this->manager->persist($model);
117
        $this->manager->flush();
118
119
        $article = $this->getLocalArticle();
120
        $detail = $article->getMainDetail();
121
122
        if (method_exists($detail, 'setPurchasePrice')) {
123
            $detail->setPurchasePrice(5.99);
124
            $this->manager->persist($detail);
125
        } else {
126
            $prices = $detail->getPrices();
127
            $prices[0]->setBasePrice(5.99);
128
            $this->manager->persist($prices[0]);
129
        }
130
        $this->manager->flush();
131
132
        // Insert the product
133
        $this->getConnectExport()->export([$article->getId()]);
134
135
        /** @var \Shopware\CustomModels\Connect\Attribute $model */
136
        $model = $this->manager->getRepository('Shopware\CustomModels\Connect\Attribute')->findOneBy(['articleId' => 3]);
137
        $message = $model->getExportMessage();
138
139
        $this->assertNull($message);
140
    }
141
}
142