Completed
Pull Request — master (#317)
by Simon
11:54 queued 07:21
created

LocalProductQueryTest::getLocalProductQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
1
<?php
2
3
namespace Tests\ShopwarePlugins\Connect\Component\ProductQuery;
4
5
use Shopware\Components\Model\ModelManager;
6
use Shopware\Connect\Struct\PriceRange;
7
use Shopware\Connect\Struct\Product;
8
use Shopware\Connect\Struct\Translation;
9
use ShopwarePlugins\Connect\Components\Config;
10
use ShopwarePlugins\Connect\Components\Marketplace\MarketplaceGateway;
11
use ShopwarePlugins\Connect\Components\ProductQuery;
12
use ShopwarePlugins\Connect\Components\ProductQuery\LocalProductQuery;
13
use Shopware\Bundle\StoreFrontBundle\Struct\Media;
14
use Shopware\Models\Property;
15
use Tests\ShopwarePlugins\Connect\ConnectTestHelper;
16
17
class LocalProductQueryTest extends ConnectTestHelper
18
{
19
    /**
20
     * @var LocalProductQuery
21
     */
22
    protected $localProductQuery;
23
24
    protected $productTranslator;
25
26
    protected $mediaService;
27
28
    private $translations;
29
30
    protected $localMediaService;
31
32
    protected $contextService;
33
34
    /** @var \Shopware\Models\Article\Article $article */
35
    private $article;
36
37
    /**
38
     * @var \Enlight_Components_Db_Adapter_Pdo_Mysql
39
     */
40
    private $db;
41
42
    /**
43
     * @var ModelManager
44
     */
45
    private $manager;
46
47
    protected $productContext;
48
49
    public function setUp()
50
    {
51
        parent::setUp();
52
53
        $this->db = Shopware()->Db();
54
        $this->manager = Shopware()->Models();
55
        $this->createArticle();
56
57
        $this->translations = array(
58
            'en' => new Translation(
59
                array(
60
                    'title' => 'Glas -Teetasse 0,25l EN',
61
                    'shortDescription' => 'shopware Connect local product short description EN',
62
                    'longDescription' => 'shopware Connect local product long description EN',
63
                    'url' => $this->getProductBaseUrl() . '22/shId/2'
64
                )
65
            ),
66
            'nl' => new Translation(
67
                array(
68
                    'title' => 'Glas -Teetasse 0,25l NL',
69
                    'shortDescription' => 'shopware Connect local product short description NL',
70
                    'longDescription' => 'shopware Connect local product long description NL',
71
                    'url' => $this->getProductBaseUrl() . '22/shId/176'
72
                )
73
            ),
74
        );
75
76
        $this->productTranslator = $this->getMockBuilder('\\ShopwarePlugins\\Connect\\Components\\Translations\\ProductTranslator')
77
            ->disableOriginalConstructor()
78
            ->getMock();
79
80
        $this->mediaService = $this->getMockBuilder('\\Shopware\\Bundle\\MediaBundle\\MediaService')
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
84
        $this->mediaService->expects($this->any())
85
            ->method('getUrl')
86
            ->with('/media/image/tea_pavilion.jpg')
87
            ->willReturn('http://myshop/media/image/2e/4f/tea_pavilion.jpg');
88
89
        $this->productTranslator->expects($this->any())
90
            ->method('translate')
91
            ->willReturn($this->translations);
92
93
        $this->productTranslator->expects($this->any())
94
            ->method('translateConfiguratorGroup')
95
            ->willReturn($this->translations);
96
97
        $this->productTranslator->expects($this->any())
98
            ->method('translateConfiguratorOption')
99
            ->willReturn($this->translations);		
100
101
        $this->localMediaService = $this->getMockBuilder('\\ShopwarePlugins\\Connect\\Components\\MediaService\\LocalMediaService')
102
            ->disableOriginalConstructor()
103
            ->getMock();
104
105
        $this->contextService = $this->getMockBuilder('\\Shopware\\Bundle\\StoreFrontBundle\\Service\\Core\\ContextService')
106
            ->disableOriginalConstructor()
107
            ->getMock();
108
109
        $this->productContext = $this->getMockBuilder('\\Shopware\\Bundle\\StoreFrontBundle\\Struct\\ProductContext')
110
            ->disableOriginalConstructor()
111
            ->getMock();
112
        $this->contextService->expects($this->any())
113
            ->method('createShopContext')
114
            ->willReturn($this->productContext);
115
    }
116
117
    public function getLocalProductQuery()
118
    {
119
        if (!$this->localProductQuery) {
120
            /** @var \ShopwarePlugins\Connect\Components\Config $configComponent */
121
            $configComponent = new Config(Shopware()->Models());
122
123
            $this->localProductQuery = new LocalProductQuery(
124
                Shopware()->Models(),
125
                $this->getProductBaseUrl(),
126
                $configComponent,
127
                new MarketplaceGateway(Shopware()->Models()),
128
                $this->productTranslator,
129
                $this->contextService,
130
                $this->localMediaService,
131
                Shopware()->Container()->get('events'),
132
                $this->mediaService
133
            );
134
        }
135
        return $this->localProductQuery;
136
    }
137
138 View Code Duplication
    public function getProductBaseUrl()
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...
139
    {
140
        if (!Shopware()->Front()->Router()) {
141
            return null;
142
        }
143
144
        return Shopware()->Front()->Router()->assemble(array(
145
            'module' => 'frontend',
146
            'controller' => 'connect_product_gateway',
147
            'action' => 'product',
148
            'id' => '',
149
            'fullPath' => true
150
        ));
151
    }
152
153
    public function testGetUrlForProduct()
154
    {
155
        $expectedUrl = $this->getProductBaseUrl() . '1091';
156
        $this->assertEquals($expectedUrl, $this->getLocalProductQuery()->getUrlForProduct(1091));
157
    }
158
159
    public function testGetUrlForProductWithShopId()
160
    {
161
        $expectedUrl = $this->getProductBaseUrl() . '1091/shId/3';
162
        $this->assertEquals($expectedUrl, $this->getLocalProductQuery()->getUrlForProduct(1091, 3));
163
    }
164
165
    public function testGetConnectProduct()
166
    {
167
        $row = array (
168
            'sku' => 'SW10005',
169
            'sourceId' => '22',
170
            'ean' => NULL,
171
            'title' => 'Glas -Teetasse 0,25l',
172
            'shortDescription' => 'Almus Emitto Bos sicut hae Amplitudo rixa ortus retribuo Vicarius an nam capitagium medius.',
173
            'vendor' =>  array(
174
                'name' => 'Teapavilion',
175
                'description' => 'Teapavilion description',
176
                'logo_url' => 'tea_pavilion.jpg',
177
                'url' => 'http://teapavilion.com',
178
                'page_title' => 'Teapavilion title',
179
            ),
180
            'vat' => '0.190000',
181
            'availability' => 3445,
182
            'price' => 10.924369747899,
183
            'purchasePrice' => 0,
184
            'longDescription' => '<p>Reficio congratulor simplex Ile familia mire hae Prosequor in pro St quae Muto,, St Texo aer Cornu ferox lex inconsiderate propitius, animus ops nos haero vietus Subdo qui Gemo ipse somniculosus. Non Apertio ops, per Repere torpeo penintentiarius Synagoga res mala caelestis praestigiator. Ineo via consectatio Gemitus sui domus ludio is vulgariter, hic ut legens nox Falx nos cui vaco insudo tero, tollo valde emo. deprecativus fio redigo probabiliter pacificus sem Nequequam, suppliciter dis Te summisse Consuesco cur Desolo sis insolesco expeditus pes Curo aut Crocotula Trimodus. Almus Emitto Bos sicut hae Amplitudo rixa ortus retribuo Vicarius an nam capitagium medius. Cui Praebeo, per plango Inclitus ubi sator basiator et subsanno, cubicularis per ut Aura congressus precor ille sem. aro quid ius Praedatio vitupero Tractare nos premo procurator. Ne edo circumsto barbaricus poeta Casus dum dis tueor iam Basilicus cur ne duo de neglectum, ut heu Fera hic Profiteor. Ius Perpetuus stilla co.</p>',
185
            'fixedPrice' => null,
186
            'deliveryWorkDays' => null,
187
            'shipping' => null,
188
            'translations' => [],
189
            'attributes' => [
190
                'unit' => null,
191
                'quantity' => null,
192
                'ref_quantity' => null,
193
            ],
194
        );
195
196
        $productMedia = [];
197 View Code Duplication
        for ($i = 1; $i < 12; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
198
            $media = new Media();
199
            $media->setFile(sprintf('http://myshop/media/image/2e/4f/tea_pavilion_product_image%s.jpg', $i));
200
            $productMedia[] = $media;
201
        }
202
203
        $variantMedia = [];
204 View Code Duplication
        for ($i = 1; $i < 12; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
205
            $media = new Media();
206
            $media->setFile(sprintf('http://myshop/media/image/2e/4f/tea_pavilion_variant_image%s.jpg', $i));
207
            $variantMedia[] = $media;
208
        }
209
210
        $this->localMediaService->expects($this->once())
211
            ->method('getProductMediaList')
212
            ->with($this->anything(), $this->productContext)
213
            ->willReturn([$row['sku'] => $productMedia]);
214
215
        $this->localMediaService->expects($this->once())
216
            ->method('getVariantMediaList')
217
            ->with($this->anything(), $this->productContext)
218
            ->willReturn([$row['sku'] => $variantMedia]);
219
220
        $expectedProduct = new Product($row);
221
        $expectedProduct->vendor['logo_url'] = 'http://myshop/media/image/2e/4f/tea_pavilion.jpg';
222
        $expectedProduct->url = $this->getProductBaseUrl() . '22';
223
        $expectedProduct->attributes = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('quantity' => NULL, 'ref_quantity' => NULL) of type array<string,null,{"quan..."ref_quantity":"null"}> is incompatible with the declared type array<integer,string> of property $attributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
224
            'quantity' => NULL,
225
            'ref_quantity' => NULL,
226
        );
227
        $expectedProduct->translations = $this->translations;
228
        $expectedProduct->priceRanges = [
229
            new PriceRange([
230
                'customerGroupKey' => 'EK',
231
                'from' => 1,
232
                'to' => 5,
233
                'price' => 123.99,
234
            ]),
235
            new PriceRange([
236
                'customerGroupKey' => 'EK',
237
                'from' => 6,
238
                'to' => PriceRange::ANY,
239
                'price' => 113.99,
240
            ]),
241
        ];
242
243
        $expectedProduct->properties = [
244
            new \Shopware\Connect\Struct\Property([
245
                'groupName' => 'Nike',
246
                'groupPosition' => 3,
247
                'comparable' => false,
248
                'sortMode' => 3,
249
                'option' => 'color',
250
                'filterable' => false,
251
                'value' => 'green',
252
                'valuePosition' => 0,
253
            ]),
254
            new \Shopware\Connect\Struct\Property([
255
                'groupName' => 'Nike',
256
                'groupPosition' => 3,
257
                'comparable' => false,
258
                'sortMode' => 3,
259
                'option' => 'size',
260
                'filterable' => false,
261
                'value' => '2xl',
262
                'valuePosition' => 0,
263
            ]),
264
            new \Shopware\Connect\Struct\Property([
265
                'groupName' => 'Nike',
266
                'groupPosition' => 3,
267
                'comparable' => false,
268
                'sortMode' => 3,
269
                'option' => 'size',
270
                'filterable' => false,
271
                'value' => '3xl',
272
                'valuePosition' => 0,
273
            ]),
274
        ];
275
276
		$expectedProduct->images = array(
277
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image1.jpg',
278
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image2.jpg',
279
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image3.jpg',
280
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image4.jpg',
281
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image5.jpg',
282
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image6.jpg',
283
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image7.jpg',
284
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image8.jpg',
285
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image9.jpg',
286
            'http://myshop/media/image/2e/4f/tea_pavilion_product_image10.jpg',
287
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image1.jpg',
288
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image2.jpg',
289
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image3.jpg',
290
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image4.jpg',
291
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image5.jpg',
292
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image6.jpg',
293
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image7.jpg',
294
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image8.jpg',
295
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image9.jpg',
296
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image10.jpg',
297
        );
298
        $expectedProduct->variantImages = array(
299
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image1.jpg',
300
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image2.jpg',
301
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image3.jpg',
302
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image4.jpg',
303
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image5.jpg',
304
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image6.jpg',
305
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image7.jpg',
306
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image8.jpg',
307
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image9.jpg',
308
            'http://myshop/media/image/2e/4f/tea_pavilion_variant_image10.jpg',
309
        );
310
311
        $row['vendorName'] = $row['vendor']['name'];
312
        $row['vendorLink'] = $row['vendor']['url'];
313
        $row['vendorImage'] = $row['vendor']['logo_url'];
314
        $row['vendorDescription'] = $row['vendor']['description'];
315
        $row['vendorMetaTitle'] = $row['vendor']['page_title'];
316
        unset($row['vendor']);
317
        $row['category'] = '';
318
        $row['weight'] = null;
319
        $row['unit'] = null;
320
        $row['localId'] = $this->article->getId();
321
        $row['detailId'] = $this->article->getMainDetail()->getId();
322
323
        $this->assertEquals($expectedProduct, $this->getLocalProductQuery()->getConnectProduct($row));
324
    }
325
326
    private function createArticle()
327
    {
328
        $group = $this->manager->getRepository(Property\Group::class)->findOneBy(
329
            ['name' => 'Nike']
330
        );
331
332
        if (!$group) {
333
            $group = new Property\Group();
334
            $group->setName('Nike');
335
            $group->setPosition(3);
336
            $group->setSortMode(3);
337
            $group->setComparable(0);
338
            $this->manager->persist($group);
339
            $this->manager->flush();
340
        }
341
342
        $minimalTestArticle = array(
343
            'name' => 'Glas -Teetasse 0,25l',
344
            'active' => true,
345
            'tax' => 19,
346
            'supplier' => 'Teapavilion',
347
            'mainDetail' => array(
348
                'number' => '9898' . rand(1, 99999),
349
            ),
350
            'filterGroupId' => $group->getId(),
351
            'propertyValues' => array(
352
                array(
353
                    'option' => array(
354
                        'name' => 'color',
355
                    ),
356
                    'value' => 'green'
357
                ),
358
                array(
359
                    'option' => array(
360
                        'name' => 'size',
361
                    ),
362
                    'value' => '2xl'
363
                ),
364
                array(
365
                    'option' => array(
366
                        'name' => 'size',
367
                    ),
368
                    'value' => '3xl'
369
                )
370
            )
371
        );
372
373
        $articleResource = \Shopware\Components\Api\Manager::getResource('article');
374
        /** @var \Shopware\Models\Article\Article $article */
375
        $this->article = $articleResource->create($minimalTestArticle);
376
377
        $this->db->insert(
378
            's_articles_prices',
379
            array(
380
                'pricegroup' => 'EK',
381
                'from' => 1,
382
                'to' => 5,
383
                'price' => 123.99,
384
                'articleID' => $this->article->getId(),
385
                'articledetailsID' => $this->article->getMainDetail()->getId(),
386
                'pseudoprice' => 0
387
            )
388
        );
389
390
        $this->db->insert(
391
            's_articles_prices',
392
            array(
393
                'pricegroup' => 'EK',
394
                'from' => 6,
395
                'to' => 'beliebig',
396
                'price' => 113.99,
397
                'articleID' => $this->article->getId(),
398
                'articledetailsID' => $this->article->getMainDetail()->getId(),
399
                'pseudoprice' => 0
400
            )
401
        );
402
    }
403
404
    public function testGetLocalProductQueryShouldFetchProductsWithoutArticleDetails()
405
    {
406
        $this->db->exec(file_get_contents(__DIR__ . '/_fixtures.sql'));
407
        $this->localProductQuery = $this->getLocalProductQuery();
408
409
        $builder = $this->localProductQuery->getProductQuery();
410
        $result = $builder->getQuery()->getArrayResult();
411
412
        $this->assertCount(1, $result);
413
    }
414
415
416
    public function tearDown()
417
    {
418
        if (!$this->article) {
419
            return;
420
        }
421
422
        $articleId = $this->article->getId();
423
        $this->db->exec("DELETE FROM s_articles WHERE id = $articleId");
424
        $this->db->exec('DELETE FROM s_articles_details WHERE ordernumber LIKE "9898%"');
425
        $this->db->exec("DELETE FROM s_articles_prices WHERE articleID = $articleId");
426
        $this->db->exec('DELETE FROM s_plugin_connect_items WHERE article_id = 5');
427
    }
428
}
429