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

ProductTranslatorTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 817
Duplicated Lines 67.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 550
loc 817
rs 9.7391
c 1
b 0
f 0

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace Tests\ShopwarePlugins\Connect\Component\Translations;
4
5
use Shopware\Connect\Struct\Translation;
6
use Shopware\Models\Shop\Locale;
7
use Tests\ShopwarePlugins\Connect\ConnectTestHelper;
8
9
class ProductTranslatorTest extends ConnectTestHelper
10
{
11
    /**
12
     * @var \ShopwarePlugins\Connect\Components\Translations\ProductTranslatorInterface
13
     */
14
    private $productTranslator;
15
16
    private $configComponent;
17
18
    private $translationGateway;
19
20
    private $modelManager;
21
22
    private $shopRepository;
23
24
    private $localeRepository;
25
26
    public function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->configComponent = $this->getMockBuilder('\\ShopwarePlugins\\Connect\\Components\\Config')
31
            ->disableOriginalConstructor()
32
            ->getMock();
33
34
        $this->translationGateway = $this->getMockBuilder('\\ShopwarePlugins\\Connect\\Components\\Gateway\\ProductTranslationsGateway\\PdoProductTranslationsGateway')
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
38
        $this->modelManager = $this->getMockBuilder('\\Shopware\\Components\\Model\\ModelManager')
39
            ->disableOriginalConstructor()
40
            ->getMock();
41
42
        $this->localeRepository = $this->getMockBuilder('\\Shopware\\Components\\Model\\ModelRepository')
43
            ->disableOriginalConstructor()
44
            ->getMock();
45
46
        $this->shopRepository = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Repository')
47
            ->disableOriginalConstructor()
48
            ->getMock();
49
50
        $this->productTranslator = new \ShopwarePlugins\Connect\Components\Translations\ProductTranslator(
51
            $this->configComponent,
52
            $this->translationGateway,
53
            $this->modelManager,
54
            $this->getProductBaseUrl()
55
        );
56
    }
57
58
    public function testTranslate()
59
    {
60
        $translations = array(
61
            2 => array(
62
                'title' => 'shopware Connect Local Product EN',
63
                'shortDescription' => 'shopware Connect Local Product short description EN',
64
                'longDescription' => 'shopware Connect Local Product long description EN',
65
                'url' => $this->getProductBaseUrl() . '35/shId/2',
66
            ),
67
            176 => array(
68
                'title' => 'shopware Connect Local Product NL',
69
                'shortDescription' => 'shopware Connect Local Product short description NL',
70
                'longDescription' => 'shopware Connect Local Product long description NL',
71
                'url' => $this->getProductBaseUrl() . '35/shId/3',
72
            ),
73
        );
74
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 176));
75
        $this->translationGateway->expects($this->any())->method('getTranslations')->with(108, array(2, 176))->willReturn($translations);
76
77
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
78
79
        $enLocale = new Locale();
80
        $enLocale->setLocale('en_GB');
81
        $nlLocale = new Locale();
82
        $nlLocale->setLocale('nl_NL');
83
84
85
        $shop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
86
            ->disableOriginalConstructor()
87
            ->getMock();
88
89
        $shop->method('getId')
90
            ->will($this->onConsecutiveCalls(2, 3));
91
92
        $shop->method('getLocale')
93
            ->will($this->onConsecutiveCalls($enLocale, $nlLocale));
94
95
        $this->shopRepository->expects($this->any())->method('find')->willReturn($shop);
96
97
        $expected = array(
98
            'en' => new Translation(
99
                array(
100
                    'title' => 'shopware Connect Local Product EN',
101
                    'shortDescription' => 'shopware Connect Local Product short description EN',
102
                    'longDescription' => 'shopware Connect Local Product long description EN',
103
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
104
                )
105
            ),
106
            'nl' => new Translation(
107
                array(
108
                    'title' => 'shopware Connect Local Product NL',
109
                    'shortDescription' => 'shopware Connect Local Product short description NL',
110
                    'longDescription' => 'shopware Connect Local Product long description NL',
111
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
112
                )
113
            )
114
        );
115
116
        $this->assertEquals($expected, $this->productTranslator->translate(108, 35));
117
    }
118
119
    public function testTranslateWhenLocaleNotFound()
120
    {
121
        $translations = array(
122
            2 => array(
123
                'title' => 'shopware Connect Local Product EN',
124
                'shortDescription' => 'shopware Connect Local Product short description EN',
125
                'longDescription' => 'shopware Connect Local Product long description EN',
126
                'url' => $this->getProductBaseUrl() . '35/shId/2',
127
            ),
128
            176 => array(
129
                'title' => 'shopware Connect Local Product NL',
130
                'shortDescription' => 'shopware Connect Local Product short description NL',
131
                'longDescription' => 'shopware Connect Local Product long description NL',
132
                'url' => $this->getProductBaseUrl() . '35/shId/3',
133
            ),
134
        );
135
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 176));
136
        $this->translationGateway->expects($this->any())->method('getTranslations')->with(108, array(2, 176))->willReturn($translations);
137
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
138
139
        $locale = new Locale();
140
        $locale->setLocale(null);
141
        $shop = new \Shopware\Models\Shop\Shop();
142
        $shop->setLocale($locale);
143
144
        $this->shopRepository->expects($this->any())->method('find')->willReturn($shop);
145
146
        $this->assertEmpty($this->productTranslator->translate(108, 35));
147
    }
148
149
    public function testTranslateWhenShopNotFound()
150
    {
151
        $translations = array(
152
            2 => array(
153
                'title' => 'shopware Connect Local Product EN',
154
                'shortDescription' => 'shopware Connect Local Product short description EN',
155
                'longDescription' => 'shopware Connect Local Product long description EN',
156
                'url' => $this->getProductBaseUrl() . '35/shId/2',
157
            ),
158
            176 => array(
159
                'title' => 'shopware Connect Local Product NL',
160
                'shortDescription' => 'shopware Connect Local Product short description NL',
161
                'longDescription' => 'shopware Connect Local Product long description NL',
162
                'url' => $this->getProductBaseUrl() . '35/shId/3',
163
            ),
164
        );
165
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 176));
166
        $this->translationGateway->expects($this->any())->method('getTranslations')->with(108, array(2, 176))->willReturn($translations);
167
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
168
        $this->shopRepository->expects($this->any())->method('find')->willReturn(null);
169
170
        $this->assertEmpty($this->productTranslator->translate(108, 35));
171
    }
172
173
    public function testTranslateConfiguratorGroup()
174
    {
175
        $groupTranslations = array(
176
            2 => 'color',
177
            3 => 'kleur',
178
        );
179
180
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
181
        $this->translationGateway->expects($this->any())->method('getConfiguratorGroupTranslations')->with(15, array(2, 3))->willReturn($groupTranslations);
182
183
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
184
185
        $enLocale = new Locale();
186
        $enLocale->setLocale('en_GB');
187
188
189
        $nlLocale = new Locale();
190
        $nlLocale->setLocale('nl_NL');
191
192
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
193
            ->disableOriginalConstructor()
194
            ->getMock();
195
        $enShop->expects($this->any())->method('getLocale')->willReturn($enLocale);
196
197
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
198
            ->disableOriginalConstructor()
199
            ->getMock();
200
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
201
202
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
203
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
204
205
        $expected = array(
206
            'en' => new Translation(
207
                array(
208
                    'title' => 'shopware Connect Local Product EN',
209
                    'shortDescription' => 'shopware Connect Local Product short description EN',
210
                    'longDescription' => 'shopware Connect Local Product long description EN',
211
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
212
                    'variantLabels' => array(
213
                        'farbe' => 'color'
214
                    ),
215
                )
216
            ),
217
            'nl' => new Translation(
218
                array(
219
                    'title' => 'shopware Connect Local Product NL',
220
                    'shortDescription' => 'shopware Connect Local Product short description NL',
221
                    'longDescription' => 'shopware Connect Local Product long description NL',
222
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
223
                    'variantLabels' => array(
224
                        'farbe' => 'kleur'
225
                    ),
226
                )
227
            )
228
        );
229
230
        $translations = array(
231
            'en' => new Translation(
232
                array(
233
                    'title' => 'shopware Connect Local Product EN',
234
                    'shortDescription' => 'shopware Connect Local Product short description EN',
235
                    'longDescription' => 'shopware Connect Local Product long description EN',
236
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
237
                )
238
            ),
239
            'nl' => new Translation(
240
                array(
241
                    'title' => 'shopware Connect Local Product NL',
242
                    'shortDescription' => 'shopware Connect Local Product short description NL',
243
                    'longDescription' => 'shopware Connect Local Product long description NL',
244
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
245
                )
246
            )
247
        );
248
249
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorGroup(15, 'farbe', $translations));
250
    }
251
252
    public function testTranslateConfiguratorGroupWithDefaultShopOnly()
253
    {
254
        // translate should be the same after group translation
255
        // when exportLanguages contains only default shop language
256
        $translations = array(
257
            'en' => new Translation(
258
                array(
259
                    'title' => 'shopware Connect Local Product EN',
260
                    'shortDescription' => 'shopware Connect Local Product short description EN',
261
                    'longDescription' => 'shopware Connect Local Product long description EN',
262
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
263
                )
264
            ),
265
            'nl' => new Translation(
266
                array(
267
                    'title' => 'shopware Connect Local Product NL',
268
                    'shortDescription' => 'shopware Connect Local Product short description NL',
269
                    'longDescription' => 'shopware Connect Local Product long description NL',
270
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
271
                )
272
            )
273
        );
274
275
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(1));
276
        $this->assertEquals($translations, $this->productTranslator->translateConfiguratorGroup(15, 'farbe', $translations));
277
    }
278
279
    public function testTranslateConfiguratorGroupWhenLocaleNotFound()
280
    {
281
        $groupTranslations = array(
282
            2 => 'color',
283
            3 => 'kleur',
284
        );
285
286
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
287
        $this->translationGateway->expects($this->any())->method('getConfiguratorGroupTranslations')->with(15, array(2, 3))->willReturn($groupTranslations);
288
289
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
290
291
        $nlLocale = new Locale();
292
        $nlLocale->setLocale('nl_NL');
293
294
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
295
            ->disableOriginalConstructor()
296
            ->getMock();
297
        $enShop->expects($this->any())->method('getLocale')->willReturn(null);
298
299
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
300
            ->disableOriginalConstructor()
301
            ->getMock();
302
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
303
304
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
305
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
306
307
        $expected = array(
308
            'en' => new Translation(
309
                array(
310
                    'title' => 'shopware Connect Local Product EN',
311
                    'shortDescription' => 'shopware Connect Local Product short description EN',
312
                    'longDescription' => 'shopware Connect Local Product long description EN',
313
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
314
                )
315
            ),
316
            'nl' => new Translation(
317
                array(
318
                    'title' => 'shopware Connect Local Product NL',
319
                    'shortDescription' => 'shopware Connect Local Product short description NL',
320
                    'longDescription' => 'shopware Connect Local Product long description NL',
321
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
322
                    'variantLabels' => array(
323
                        'farbe' => 'kleur'
324
                    ),
325
                )
326
            )
327
        );
328
329
        $translations = array(
330
            'en' => new Translation(
331
                array(
332
                    'title' => 'shopware Connect Local Product EN',
333
                    'shortDescription' => 'shopware Connect Local Product short description EN',
334
                    'longDescription' => 'shopware Connect Local Product long description EN',
335
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
336
                )
337
            ),
338
            'nl' => new Translation(
339
                array(
340
                    'title' => 'shopware Connect Local Product NL',
341
                    'shortDescription' => 'shopware Connect Local Product short description NL',
342
                    'longDescription' => 'shopware Connect Local Product long description NL',
343
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
344
                )
345
            )
346
        );
347
348
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorGroup(15, 'farbe', $translations));
349
    }
350
351
    public function testTranslateConfiguratorGroupWithoutStruct()
352
    {
353
        $groupTranslations = array(
354
            2 => 'color',
355
            3 => 'kleur',
356
        );
357
358
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
359
        $this->translationGateway->expects($this->any())->method('getConfiguratorGroupTranslations')->with(15, array(2, 3))->willReturn($groupTranslations);
360
361
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
362
363
        $nlLocale = new Locale();
364
        $nlLocale->setLocale('nl_NL');
365
366
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
367
            ->disableOriginalConstructor()
368
            ->getMock();
369
        $enShop->expects($this->any())->method('getLocale')->willReturn(null);
370
371
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
372
            ->disableOriginalConstructor()
373
            ->getMock();
374
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
375
376
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
377
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
378
379
        $expected = array(
380
            'en' => array(),
381
            'nl' => new Translation(
382
                array(
383
                    'title' => 'shopware Connect Local Product NL',
384
                    'shortDescription' => 'shopware Connect Local Product short description NL',
385
                    'longDescription' => 'shopware Connect Local Product long description NL',
386
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
387
                    'variantLabels' => array(
388
                        'farbe' => 'kleur'
389
                    ),
390
                )
391
            )
392
        );
393
394
        $translations = array(
395
            'en' => array(),
396
            'nl' => new Translation(
397
                array(
398
                    'title' => 'shopware Connect Local Product NL',
399
                    'shortDescription' => 'shopware Connect Local Product short description NL',
400
                    'longDescription' => 'shopware Connect Local Product long description NL',
401
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
402
                )
403
            )
404
        );
405
406
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorGroup(15, 'farbe', $translations));
407
    }
408
409
    public function testTranslateConfiguratorGroupWithInvalidLocaleCode()
410
    {
411
        $groupTranslations = array(
412
            2 => 'color',
413
            3 => 'kleur',
414
        );
415
416
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
417
        $this->translationGateway->expects($this->any())->method('getConfiguratorGroupTranslations')->with(15, array(2, 3))->willReturn($groupTranslations);
418
419
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
420
421
        $enLocale = new Locale();
422
        $enLocale->setLocale('en-EN');
423
424
        $nlLocale = new Locale();
425
        $nlLocale->setLocale('nl_NL');
426
427
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
428
            ->disableOriginalConstructor()
429
            ->getMock();
430
        $enShop->expects($this->any())->method('getLocale')->willReturn($enLocale);
431
432
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
433
            ->disableOriginalConstructor()
434
            ->getMock();
435
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
436
437
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
438
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
439
440
        $expected = array(
441
            'en' => new Translation(
442
                array(
443
                    'title' => 'shopware Connect Local Product EN',
444
                    'shortDescription' => 'shopware Connect Local Product short description EN',
445
                    'longDescription' => 'shopware Connect Local Product long description EN',
446
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
447
                )
448
            ),
449
            'nl' => new Translation(
450
                array(
451
                    'title' => 'shopware Connect Local Product NL',
452
                    'shortDescription' => 'shopware Connect Local Product short description NL',
453
                    'longDescription' => 'shopware Connect Local Product long description NL',
454
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
455
                    'variantLabels' => array(
456
                        'farbe' => 'kleur'
457
                    ),
458
                )
459
            )
460
        );
461
462
        $translations = array(
463
            'en' => new Translation(
464
                array(
465
                    'title' => 'shopware Connect Local Product EN',
466
                    'shortDescription' => 'shopware Connect Local Product short description EN',
467
                    'longDescription' => 'shopware Connect Local Product long description EN',
468
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
469
                )
470
            ),
471
            'nl' => new Translation(
472
                array(
473
                    'title' => 'shopware Connect Local Product NL',
474
                    'shortDescription' => 'shopware Connect Local Product short description NL',
475
                    'longDescription' => 'shopware Connect Local Product long description NL',
476
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
477
                )
478
            )
479
        );
480
481
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorGroup(15, 'farbe', $translations));
482
    }
483
484
    public function testTranslateConfiguratorOption()
485
    {
486
        $optionTranslations = array(
487
            2 => 'red',
488
            3 => 'rood',
489
        );
490
491
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
492
        $this->translationGateway->expects($this->any())->method('getConfiguratorOptionTranslations')->with(15, array(2, 3))->willReturn($optionTranslations);
493
494
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
495
496
        $enLocale = new Locale();
497
        $enLocale->setLocale('en_GB');
498
499
        $nlLocale = new Locale();
500
        $nlLocale->setLocale('nl_NL');
501
502
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
503
            ->disableOriginalConstructor()
504
            ->getMock();
505
        $enShop->expects($this->any())->method('getLocale')->willReturn($enLocale);
506
507
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
508
            ->disableOriginalConstructor()
509
            ->getMock();
510
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
511
512
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
513
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
514
515
        $expected = array(
516
            'en' => new Translation(
517
                array(
518
                    'title' => 'shopware Connect Local Product EN',
519
                    'shortDescription' => 'shopware Connect Local Product short description EN',
520
                    'longDescription' => 'shopware Connect Local Product long description EN',
521
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
522
                    'variantValues' => array(
523
                        'rot' => 'red'
524
                    ),
525
                )
526
            ),
527
            'nl' => new Translation(
528
                array(
529
                    'title' => 'shopware Connect Local Product NL',
530
                    'shortDescription' => 'shopware Connect Local Product short description NL',
531
                    'longDescription' => 'shopware Connect Local Product long description NL',
532
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
533
                    'variantValues' => array(
534
                        'rot' => 'rood'
535
                    ),
536
                )
537
            )
538
        );
539
540
        $translations = array(
541
            'en' => new Translation(
542
                array(
543
                    'title' => 'shopware Connect Local Product EN',
544
                    'shortDescription' => 'shopware Connect Local Product short description EN',
545
                    'longDescription' => 'shopware Connect Local Product long description EN',
546
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
547
                )
548
            ),
549
            'nl' => new Translation(
550
                array(
551
                    'title' => 'shopware Connect Local Product NL',
552
                    'shortDescription' => 'shopware Connect Local Product short description NL',
553
                    'longDescription' => 'shopware Connect Local Product long description NL',
554
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
555
                )
556
            )
557
        );
558
559
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorOption(15, 'rot', $translations));
560
    }
561
562
    public function testTranslateConfiguratorOptionWithDefaultShopOnly()
563
    {
564
        // translate should be the same after group translation
565
        // when exportLanguages contains only default shop language
566
        $translations = array(
567
            'en' => new Translation(
568
                array(
569
                    'title' => 'shopware Connect Local Product EN',
570
                    'shortDescription' => 'shopware Connect Local Product short description EN',
571
                    'longDescription' => 'shopware Connect Local Product long description EN',
572
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
573
                )
574
            ),
575
            'nl' => new Translation(
576
                array(
577
                    'title' => 'shopware Connect Local Product NL',
578
                    'shortDescription' => 'shopware Connect Local Product short description NL',
579
                    'longDescription' => 'shopware Connect Local Product long description NL',
580
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
581
                )
582
            )
583
        );
584
585
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(1));
586
        $this->assertEquals($translations, $this->productTranslator->translateConfiguratorGroup(15, 'red', $translations));
587
    }
588
589
    public function testTranslateConfiguratorOptionWithInvalidShop()
590
    {
591
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
592
        $this->shopRepository->expects($this->at(0))->method('find')->with(6)->willReturn(null);
593
594
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn([1, 6]);
595
        $this->assertEmpty($this->productTranslator->translateConfiguratorGroup(15, 'red', []));
596
    }
597
598
    public function testTranslateConfiguratorOptionWhenLocaleNotFound()
599
    {
600
        $optionTranslations = array(
601
            2 => 'red',
602
            3 => 'rood',
603
        );
604
605
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
606
        $this->translationGateway->expects($this->any())->method('getConfiguratorOptionTranslations')->with(15, array(2, 3))->willReturn($optionTranslations);
607
608
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
609
610
        $nlLocale = new Locale();
611
        $nlLocale->setLocale('nl_NL');
612
613
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
614
            ->disableOriginalConstructor()
615
            ->getMock();
616
        $enShop->expects($this->any())->method('getLocale')->willReturn(null);
617
618
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
619
            ->disableOriginalConstructor()
620
            ->getMock();
621
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
622
623
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
624
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
625
626
        $expected = array(
627
            'en' => new Translation(
628
                array(
629
                    'title' => 'shopware Connect Local Product EN',
630
                    'shortDescription' => 'shopware Connect Local Product short description EN',
631
                    'longDescription' => 'shopware Connect Local Product long description EN',
632
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
633
                )
634
            ),
635
            'nl' => new Translation(
636
                array(
637
                    'title' => 'shopware Connect Local Product NL',
638
                    'shortDescription' => 'shopware Connect Local Product short description NL',
639
                    'longDescription' => 'shopware Connect Local Product long description NL',
640
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
641
                    'variantValues' => array(
642
                        'rot' => 'rood'
643
                    ),
644
                )
645
            )
646
        );
647
648
        $translations = array(
649
            'en' => new Translation(
650
                array(
651
                    'title' => 'shopware Connect Local Product EN',
652
                    'shortDescription' => 'shopware Connect Local Product short description EN',
653
                    'longDescription' => 'shopware Connect Local Product long description EN',
654
                    'url' => $this->getProductBaseUrl() . '35/shId/2',
655
                )
656
            ),
657
            'nl' => new Translation(
658
                array(
659
                    'title' => 'shopware Connect Local Product NL',
660
                    'shortDescription' => 'shopware Connect Local Product short description NL',
661
                    'longDescription' => 'shopware Connect Local Product long description NL',
662
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
663
                )
664
            )
665
        );
666
667
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorOption(15, 'rot', $translations));
668
    }
669
670
    public function testTranslateConfiguratorOptionWithoutStruct()
671
    {
672
        $optionTranslations = array(
673
            2 => 'red',
674
            3 => 'rood',
675
        );
676
677
        $this->configComponent->expects($this->any())->method('getConfig')->with('exportLanguages')->willReturn(array(2, 3));
678
        $this->translationGateway->expects($this->any())->method('getConfiguratorOptionTranslations')->with(15, array(2, 3))->willReturn($optionTranslations);
679
680
        $this->modelManager->expects($this->at(0))->method('getRepository')->with('Shopware\Models\Shop\Shop')->willReturn($this->shopRepository);
681
682
        $nlLocale = new Locale();
683
        $nlLocale->setLocale('nl_NL');
684
685
        $enShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
686
            ->disableOriginalConstructor()
687
            ->getMock();
688
        $enShop->expects($this->any())->method('getLocale')->willReturn(null);
689
690
        $nlShop = $this->getMockBuilder('\\Shopware\\Models\\Shop\\Shop')
691
            ->disableOriginalConstructor()
692
            ->getMock();
693
        $nlShop->expects($this->any())->method('getLocale')->willReturn($nlLocale);
694
695
        $this->shopRepository->expects($this->at(0))->method('find')->with(2)->willReturn($enShop);
696
        $this->shopRepository->expects($this->at(1))->method('find')->with(3)->willReturn($nlShop);
697
698
        $expected = array(
699
            'en' => array(),
700
            'nl' => new Translation(
701
                array(
702
                    'title' => 'shopware Connect Local Product NL',
703
                    'shortDescription' => 'shopware Connect Local Product short description NL',
704
                    'longDescription' => 'shopware Connect Local Product long description NL',
705
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
706
                    'variantValues' => array(
707
                        'rot' => 'rood'
708
                    ),
709
                )
710
            )
711
        );
712
713
        $translations = array(
714
            'en' => array(),
715
            'nl' => new Translation(
716
                array(
717
                    'title' => 'shopware Connect Local Product NL',
718
                    'shortDescription' => 'shopware Connect Local Product short description NL',
719
                    'longDescription' => 'shopware Connect Local Product long description NL',
720
                    'url' => $this->getProductBaseUrl() . '35/shId/3',
721
                )
722
            )
723
        );
724
725
        $this->assertEquals($expected, $this->productTranslator->translateConfiguratorOption(15, 'rot', $translations));
726
    }
727
728
    public function testValidate()
729
    {
730
        $translation = new Translation(array(
731
            'title' => 'shopware Connect Local Product EN',
732
            'shortDescription' => 'shopware Connect Local Product short description EN',
733
            'longDescription' => 'shopware Connect Local Product long description EN',
734
            'url' => $this->getProductBaseUrl() . '35/shId/2',
735
            'variantLabels' => array(
736
                'größe' => 'size',
737
                'farbe' => 'color',
738
            ),
739
            'variantValues' => array(
740
                '52' => 'XL',
741
                'blau' => 'blue',
742
            ),
743
        ));
744
745
        $this->assertTrue($this->productTranslator->validate($translation, 2));
746
    }
747
748
    public function testValidateMissingTitle()
749
    {
750
        $translation = new Translation(array(
751
            'shortDescription' => 'shopware Connect Local Product short description EN',
752
            'longDescription' => 'shopware Connect Local Product long description EN',
753
            'url' => $this->getProductBaseUrl() . '35/shId/2',
754
            'variantLabels' => array(
755
                'größe' => 'size',
756
                'farbe' => 'color',
757
            ),
758
            'variantValues' => array(
759
                '52' => 'XL',
760
                'blau' => 'blue',
761
            ),
762
        ));
763
764
        $this->assertTrue($this->productTranslator->validate($translation, 2));
765
    }
766
767
    /**
768
     * @expectedException \Exception
769
     */
770
    public function testValidateWrongVariantLabels()
771
    {
772
        $translation = new Translation(array(
773
            'title' => 'shopware Connect Local Product EN',
774
            'shortDescription' => 'shopware Connect Local Product short description EN',
775
            'longDescription' => 'shopware Connect Local Product long description EN',
776
            'url' => $this->getProductBaseUrl() . '35/shId/2',
777
            'variantLabels' => array(
778
                'farbe' => 'color',
779
            ),
780
            'variantValues' => array(
781
                '52' => 'XL',
782
                'blau' => 'blue',
783
            ),
784
        ));
785
786
        $this->assertTrue($this->productTranslator->validate($translation, 2));
787
    }
788
789
    /**
790
     * @expectedException \Exception
791
     */
792
    public function testValidateWrongVariantValues()
793
    {
794
        $translation = new Translation(array(
795
            'title' => 'shopware Connect Local Product EN',
796
            'shortDescription' => 'shopware Connect Local Product short description EN',
797
            'longDescription' => 'shopware Connect Local Product long description EN',
798
            'url' => $this->getProductBaseUrl() . '35/shId/2',
799
            'variantLabels' => array(
800
                'größe' => 'size',
801
                'farbe' => 'color',
802
            ),
803
            'variantValues' => array(
804
                'blau' => 'blue',
805
            ),
806
        ));
807
808
        $this->assertTrue($this->productTranslator->validate($translation, 2));
809
    }
810
811
    public function getProductBaseUrl()
812
    {
813
        if (!Shopware()->Front()->Router()) {
814
            return null;
815
        }
816
817
        return Shopware()->Front()->Router()->assemble(array(
818
            'module' => 'frontend',
819
            'controller' => 'connect_product_gateway',
820
            'action' => 'product',
821
            'id' => '',
822
            'fullPath' => true
823
        ));
824
    }
825
}
826