Issues (3641)

ProductLabel/Business/ProductLabelFacadeTest.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Zed\ProductLabel\Business;
9
10
use ArrayObject;
11
use Codeception\Test\Unit;
12
use DateInterval;
13
use DateTime;
14
use Generated\Shared\DataBuilder\ProductLabelBuilder;
15
use Generated\Shared\DataBuilder\ProductLabelCriteriaBuilder;
16
use Generated\Shared\DataBuilder\ProductLabelLocalizedAttributesBuilder;
17
use Generated\Shared\DataBuilder\ProductLabelProductAbstractRelationsBuilder;
18
use Generated\Shared\DataBuilder\StoreRelationBuilder;
19
use Generated\Shared\Transfer\EventEntityTransfer;
20
use Generated\Shared\Transfer\FilterTransfer;
21
use Generated\Shared\Transfer\PaginationTransfer;
22
use Generated\Shared\Transfer\ProductLabelConditionsTransfer;
23
use Generated\Shared\Transfer\ProductLabelCriteriaTransfer;
24
use Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer;
25
use Generated\Shared\Transfer\ProductLabelProductAbstractRelationsTransfer;
26
use Generated\Shared\Transfer\ProductLabelTransfer;
27
use Generated\Shared\Transfer\SortTransfer;
28
use Generated\Shared\Transfer\StoreRelationTransfer;
29
use Generated\Shared\Transfer\StoreTransfer;
30
use Orm\Zed\ProductLabel\Persistence\SpyProductLabelQuery;
31
use Orm\Zed\ProductLabel\Persistence\SpyProductLabelStoreQuery;
32
use Spryker\Shared\Product\ProductConfig;
33
use Spryker\Shared\ProductLabel\ProductLabelConstants;
34
use Spryker\Zed\ProductLabel\Business\ProductLabelFacadeInterface;
35
use Spryker\Zed\ProductLabel\Dependency\Facade\ProductLabelToEventInterface;
36
use Spryker\Zed\ProductLabel\Dependency\Facade\ProductLabelToProductInterface;
37
use Spryker\Zed\ProductLabel\Dependency\Facade\ProductLabelToTouchInterface;
38
use Spryker\Zed\ProductLabel\Dependency\Plugin\ProductLabelRelationUpdaterPluginInterface;
39
use Spryker\Zed\ProductLabel\ProductLabelDependencyProvider;
40
41
/**
42
 * Auto-generated group annotations
43
 *
44
 * @group SprykerTest
45
 * @group Zed
46
 * @group ProductLabel
47
 * @group Business
48
 * @group Facade
49
 * @group ProductLabelFacadeTest
50
 * Add your own group annotations below this line
51
 */
52
class ProductLabelFacadeTest extends Unit
53
{
54
    /**
55
     * @uses \Spryker\Zed\Product\Dependency\ProductEvents::PRODUCT_ABSTRACT_UPDATE
56
     *
57
     * @var string
58
     */
59
    protected const PRODUCT_ABSTRACT_UPDATE = 'Product.product_abstract.update';
60
61
    /**
62
     * @var \SprykerTest\Zed\ProductLabel\ProductLabelBusinessTester
63
     */
64
    protected $tester;
65
66
    /**
67
     * @var string
68
     */
69
    public const STORE_NAME_DE = 'DE';
70
71
    /**
72
     * @var string
73
     */
74
    public const STORE_NAME_AT = 'AT';
75
76
    /**
77
     * @var \Spryker\Zed\ProductLabel\Dependency\Facade\ProductLabelToEventInterface
78
     */
79
    protected $eventFacade;
80
81
    /**
82
     * @return void
83
     */
84
    protected function setUp(): void
85
    {
86
        parent::setUp();
87
88
        $this->eventFacade = $this->createMock(ProductLabelToEventInterface::class);
89
90
        $this->tester->mockFactoryMethod(
91
            'getEventFacade',
92
            $this->eventFacade,
93
        );
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function testFindLabelByIdShouldReturnProductLabelTransfer(): void
100
    {
101
        $productLabelTransfer = $this->tester->haveProductLabel();
102
103
        $productLabelFacade = $this->getProductLabelFacade();
104
        $productLabelTransfer = $productLabelFacade->findLabelById($productLabelTransfer->getIdProductLabel());
105
106
        $this->assertInstanceOf('\Generated\Shared\Transfer\ProductLabelTransfer', $productLabelTransfer);
107
    }
108
109
    /**
110
     * @return void
111
     */
112
    public function testRemoveLabelShouldRemoveProductLabel(): void
113
    {
114
        //Arrange
115
        $productLabelTransfer = $this->tester->haveProductLabel();
116
117
        //Act
118
        $productLabelResponseTransfer = $this->getProductLabelFacade()->removeLabel($productLabelTransfer);
119
120
        //Assert
121
        $this->assertTrue($productLabelResponseTransfer->getIsSuccessful(), 'Response transfer does not match the expected result');
122
123
        $deletedProductLabel = SpyProductLabelQuery::create()
124
            ->filterByIdProductLabel($productLabelTransfer->getIdProductLabel())
125
            ->findOne();
126
127
        $this->assertNull($deletedProductLabel, 'Product label record was not deleted');
128
    }
129
130
    /**
131
     * @return void
132
     */
133
    public function testGetActiveLabelsByCriteriaShouldRetrieveActiveLabels(): void
134
    {
135
        //Arrange
136
        $productTransfer = $this->tester->haveProduct();
137
        $idProductAbstract = $productTransfer->getFkProductAbstract();
138
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
139
        $storeTransferAT = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_AT]);
140
        $storeRelationSeedData = [
141
            StoreRelationTransfer::ID_STORES => [
142
                $storeTransferDE->getIdStore(),
143
                $storeTransferAT->getIdStore(),
144
            ],
145
        ];
146
        $productLabelTransfer = $this->tester->haveProductLabel([
147
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedData,
148
        ]);
149
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
150
151
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
152
153
        $productLabelCriteria = (new ProductLabelCriteriaBuilder())->seed([
154
            ProductLabelCriteriaTransfer::PRODUCT_ABSTRACT_IDS => [$idProductAbstract],
155
            ProductLabelCriteriaTransfer::PRODUCT_LABEL_IDS => [$idProductLabel],
156
            ProductLabelCriteriaTransfer::STORE_NAME => $storeTransferDE->getName(),
157
        ])->build();
158
159
        //Act
160
        $productLabelFacade = $this->getProductLabelFacade();
161
        $activeProductLabelsByTheGivenCriteria = $productLabelFacade->getActiveLabelsByCriteria($productLabelCriteria);
162
163
        //Assert
164
        $this->assertCount(
165
            1,
166
            $activeProductLabelsByTheGivenCriteria,
167
            'Product label not found.',
168
        );
169
170
        $this->assertSame(
171
            $idProductLabel,
172
            $activeProductLabelsByTheGivenCriteria[0]->getIdProductLabel(),
173
            'Wrong product label.',
174
        );
175
176
        $this->assertCount(
177
            2,
178
            $activeProductLabelsByTheGivenCriteria[0]->getStoreRelation()->getStores(),
179
            'Stores relations number is incorrect.',
180
        );
181
182
        $this->assertCount(
183
            $productLabelTransfer->getLocalizedAttributesCollection()->count(),
184
            $activeProductLabelsByTheGivenCriteria[0]->getLocalizedAttributesCollection(),
185
            'Localized attributes number is incorrect.',
186
        );
187
    }
188
189
    /**
190
     * @return void
191
     */
192
    public function testFindLabelByIdShouldReturnNullIfLabelDoesNotExist(): void
193
    {
194
        $productLabelFacade = $this->getProductLabelFacade();
195
        $productLabelTransfer = $productLabelFacade->findLabelById(666);
196
197
        $this->assertNull($productLabelTransfer);
198
    }
199
200
    /**
201
     * @return void
202
     */
203
    public function testFindLabelByIdShouldReturnCollectionOfLocalizedAttributes(): void
204
    {
205
        $localeTransfer = $this->tester->haveLocale();
206
207
        $productLabelTransfer = (new ProductLabelBuilder())->except(['idProductLabel'])->build();
208
        $productLabelTransfer->addLocalizedAttributes(
209
            $this->generateLocalizedAttributesTransfer($localeTransfer->getIdLocale()),
210
        );
211
        $productLabelFacade = $this->getProductLabelFacade();
212
        $productLabelFacade->createLabel($productLabelTransfer);
213
214
        $persistedProductLabelTransfer = $productLabelFacade->findLabelById($productLabelTransfer->getIdProductLabel());
215
216
        $this->assertSame(1, $persistedProductLabelTransfer->getLocalizedAttributesCollection()->count());
217
    }
218
219
    /**
220
     * @return void
221
     */
222
    public function testFindAllLabelsShouldReturnCollectionSortedByPosition(): void
223
    {
224
        $this->tester->haveProductLabel([ProductLabelTransfer::POSITION => 100]);
225
        $this->tester->haveProductLabel([ProductLabelTransfer::POSITION => 101]);
226
        $this->tester->haveProductLabel([ProductLabelTransfer::POSITION => 102]);
227
        $this->tester->haveProductLabel([ProductLabelTransfer::POSITION => 103]);
228
229
        $productLabelFacade = $this->getProductLabelFacade();
230
        $productLabelTransferCollection = $productLabelFacade->findAllLabels();
231
232
        $this->assertSame(103, array_pop($productLabelTransferCollection)->getPosition());
233
        $this->assertSame(102, array_pop($productLabelTransferCollection)->getPosition());
234
        $this->assertSame(101, array_pop($productLabelTransferCollection)->getPosition());
235
        $this->assertSame(100, array_pop($productLabelTransferCollection)->getPosition());
236
    }
237
238
    /**
239
     * @return void
240
     */
241
    public function testCreateLabelShouldPersistDataAndUpdatesTransferIdField(): void
242
    {
243
        $productLabelFacade = $this->getProductLabelFacade();
244
        $productLabelTransfer = (new ProductLabelBuilder())->except(['idProductLabel'])->build();
245
        $productLabelFacade->createLabel($productLabelTransfer);
246
247
        $persistedProductLabelTransfer = $productLabelFacade->findLabelById($productLabelTransfer->getIdProductLabel());
248
249
        $this->assertNotNull($productLabelTransfer->getIdProductLabel());
250
        $this->assertSame($productLabelTransfer->getIdProductLabel(), $persistedProductLabelTransfer->getIdProductLabel());
251
    }
252
253
    /**
254
     * @return void
255
     */
256
    public function testUpdateLabelShouldPersistChanges(): void
257
    {
258
        $productLabelTransfer = $this->tester->haveProductLabel([
259
            'idActive' => true,
260
            'isExclusive' => false,
261
        ]);
262
263
        $productLabelTransfer->setIsActive(false);
264
        $productLabelTransfer->setIsExclusive(true);
265
        $productLabelTransfer->setName('FooBarBaz');
266
267
        $productLabelFacade = $this->getProductLabelFacade();
268
        $productLabelFacade->updateLabel($productLabelTransfer);
269
270
        $updatedProductLabelTransfer = $productLabelFacade->findLabelById($productLabelTransfer->getIdProductLabel());
271
272
        $this->assertFalse($updatedProductLabelTransfer->getIsActive());
273
        $this->assertTrue($updatedProductLabelTransfer->getIsExclusive());
274
        $this->assertSame('FooBarBaz', $updatedProductLabelTransfer->getName());
275
    }
276
277
    /**
278
     * @return void
279
     */
280
    public function testCreateLabelShouldPersistLocalizedAttributes(): void
281
    {
282
        $localeTransfer = $this->tester->haveLocale();
283
284
        $productLabelTransfer = (new ProductLabelBuilder())->except(['idProductLabel'])->build();
285
        $productLabelTransfer->addLocalizedAttributes(
286
            $this->generateLocalizedAttributesTransfer($localeTransfer->getIdLocale()),
287
        );
288
        $productLabelFacade = $this->getProductLabelFacade();
289
        $productLabelFacade->createLabel($productLabelTransfer);
290
291
        $persistedProductLabelTransfer = $productLabelFacade->findLabelById($productLabelTransfer->getIdProductLabel());
292
293
        $this->assertSame(1, $persistedProductLabelTransfer->getLocalizedAttributesCollection()->count());
294
295
        /** @var array<\Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer> $localizedAttributesList */
296
        $localizedAttributesList = $persistedProductLabelTransfer->getLocalizedAttributesCollection()->getArrayCopy();
297
        $this->assertSame($productLabelTransfer->getIdProductLabel(), $localizedAttributesList[0]->getFkProductLabel());
298
        $this->assertSame($localeTransfer->getIdLocale(), $localizedAttributesList[0]->getFkLocale());
299
    }
300
301
    /**
302
     * @return void
303
     */
304
    public function testUpdateLabelShouldTouchDictionaryWhenLabelIsUpdated(): void
305
    {
306
        // Arrange
307
        $productLabelTransfer = $this->tester->haveProductLabel([
308
            ProductLabelTransfer::IS_EXCLUSIVE => true,
309
        ]);
310
311
        $touchFacadeMock = $this->createTouchFacadeMock();
312
        $this->tester->setDependency(ProductLabelDependencyProvider::FACADE_TOUCH, $touchFacadeMock);
313
314
        $productLabelTransfer->setIsExclusive(false);
315
316
        // Assert
317
        $touchFacadeMock->expects($this->once())
318
            ->method('touchActive')
319
            ->with(
320
                ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
321
                ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
322
            );
323
324
        // Act
325
        $this->getProductLabelFacade()->updateLabel($productLabelTransfer);
326
    }
327
328
    /**
329
     * @return void
330
     */
331
    public function testUpdateLabelShouldNotTouchDictionaryWhenLabelIsNotUpdated(): void
332
    {
333
        // Arrange
334
        $productLabelTransfer = $this->tester->haveProductLabel([
335
            ProductLabelTransfer::IS_EXCLUSIVE => true,
336
        ]);
337
338
        $touchFacadeMock = $this->createTouchFacadeMock();
339
        $this->tester->setDependency(ProductLabelDependencyProvider::FACADE_TOUCH, $touchFacadeMock);
340
341
        // Assert
342
        $touchFacadeMock->expects($this->never())->method('touchActive');
343
344
        // Act
345
        $this->getProductLabelFacade()->updateLabel($productLabelTransfer);
346
    }
347
348
    /**
349
     * @return void
350
     */
351
    public function testUpdateLabelShouldTouchProductRelationWhenIsActiveUpdated(): void
352
    {
353
        // Arrange
354
        $productLabelTransfer = $this->tester->haveProductLabel([
355
            ProductLabelTransfer::IS_ACTIVE => true,
356
        ]);
357
        $idProductAbstract = $this->tester->haveProduct()->getFkProductAbstract();
358
359
        $this->tester->haveProductLabelToAbstractProductRelation($productLabelTransfer->getIdProductLabel(), $idProductAbstract);
360
361
        $touchFacadeMock = $this->createTouchFacadeMock();
362
        $this->tester->setDependency(ProductLabelDependencyProvider::FACADE_TOUCH, $touchFacadeMock);
363
364
        $productFacadeMock = $this->createProductFacadeMock();
365
        $this->tester->setDependency(ProductLabelDependencyProvider::FACADE_PRODUCT, $productFacadeMock);
366
367
        $productLabelTransfer->setIsActive(false);
368
369
        // Assert
370
        $touchFacadeMock->expects($this->exactly(2))
371
            ->method('touchActive')
372
            ->withConsecutive(
373
                [
374
                    ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
375
                    ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
376
                ],
377
                [
378
                    ProductLabelConstants::RESOURCE_TYPE_PRODUCT_ABSTRACT_PRODUCT_LABEL_RELATIONS,
379
                    $idProductAbstract,
380
                ],
381
            );
382
383
        $productFacadeMock->expects($this->once())
384
            ->method('touchProductAbstract')
385
            ->with($idProductAbstract);
386
387
        // Act
388
        $this->getProductLabelFacade()->updateLabel($productLabelTransfer);
389
    }
390
391
    /**
392
     * @return void
393
     */
394
    public function testCreateLabelShouldPersistStoreRelations(): void
395
    {
396
        //Arrange
397
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
398
        $storeTransferAT = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_AT]);
399
        $storeRelationSeedData = [
400
            StoreRelationTransfer::ID_STORES => [
401
                $storeTransferDE->getIdStore(),
402
                $storeTransferAT->getIdStore(),
403
            ],
404
        ];
405
        $productLabelTransfer = (new ProductLabelBuilder([
406
            ProductLabelTransfer::ID_PRODUCT_LABEL => null,
407
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedData,
408
        ]))->build();
409
410
        //Act
411
        $this->getProductLabelFacade()->createLabel($productLabelTransfer);
412
413
        //Assert
414
        $productLabelEntity = SpyProductLabelQuery::create()
415
            ->filterByName($productLabelTransfer->getName())
416
            ->findOne();
417
418
        $productLabelStoreRelationExists = SpyProductLabelStoreQuery::create()
419
            ->filterByFkProductLabel($productLabelEntity->getIdProductLabel())
420
            ->exists();
421
        $this->assertTrue($productLabelStoreRelationExists, 'Relation between store and product label should exists');
422
    }
423
424
    /**
425
     * @return void
426
     */
427
    public function testUpdateLabelShouldUpdateStoreRelations(): void
428
    {
429
        //Arrange
430
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
431
        $storeTransferAT = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_AT]);
432
        $storeRelationSeedDataForDE = [
433
            StoreRelationTransfer::ID_STORES => [
434
                $storeTransferDE->getIdStore(),
435
            ],
436
        ];
437
        $storeRelationSeedDataForAT = [
438
            StoreRelationTransfer::ID_STORES => [
439
                $storeTransferAT->getIdStore(),
440
            ],
441
        ];
442
443
        $productLabelTransfer = $this->tester->haveProductLabel([
444
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedDataForDE,
445
        ]);
446
447
        $storeRelationTransfer = (new StoreRelationBuilder())->seed($storeRelationSeedDataForAT)->build();
448
449
        //Act
450
        $productLabelTransfer->setStoreRelation($storeRelationTransfer);
451
        $this->getProductLabelFacade()->updateLabel($productLabelTransfer);
452
453
        //Assert
454
        $productLabelStoreRelationAfterUpdate = SpyProductLabelStoreQuery::create()
455
            ->filterByFkProductLabel($productLabelTransfer->getIdProductLabel())
456
            ->find();
457
458
        $this->assertCount(
459
            1,
460
            $productLabelStoreRelationAfterUpdate,
461
            'Product label store relation data should be relevant',
462
        );
463
        $this->assertSame(
464
            $storeTransferAT->getIdStore(),
465
            $productLabelStoreRelationAfterUpdate->offsetGet(0)->getFkStore(),
466
            'Product label store relation',
467
        );
468
    }
469
470
    /**
471
     * @return void
472
     */
473
    public function testFindLabelByIdShouldReturnProductLabelTransferForSpecificStore(): void
474
    {
475
        //Arrange
476
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
477
        $productLabelTransfer = $this->tester->haveProductLabel([
478
            ProductLabelTransfer::NAME => 'test1',
479
            ProductLabelTransfer::STORE_RELATION => [
480
                StoreRelationTransfer::ID_STORES => [
481
                    $storeTransferDE->getIdStore(),
482
                ],
483
            ],
484
        ]);
485
486
        //Act
487
        $productLabelTransfer = $this->getProductLabelFacade()->findLabelById($productLabelTransfer->getIdProductLabel());
488
489
        //Assert
490
        $this->tester->assertEquals(
491
            $storeTransferDE->getName(),
492
            $productLabelTransfer->getStoreRelation()->getStores()->offsetGet(0)->getName(),
493
            'Given store is invalid for this relation',
494
        );
495
    }
496
497
    /**
498
     * @return void
499
     */
500
    public function testFindLabelByLabelNameShouldReturnProductLabelTransferForSpecificStore(): void
501
    {
502
        //Arrange
503
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
504
        $productLabelTransfer = $this->tester->haveProductLabel([
505
            ProductLabelTransfer::NAME => 'test1',
506
            ProductLabelTransfer::STORE_RELATION => [
507
                StoreRelationTransfer::ID_STORES => [
508
                    $storeTransferDE->getIdStore(),
509
                ],
510
            ],
511
        ]);
512
513
        $productLabelFacade = $this->getProductLabelFacade();
514
        //Act
515
        $productLabelTransfer = $productLabelFacade->findLabelByLabelName($productLabelTransfer->getName());
516
517
        //Assert
518
        $this->tester->assertEquals(
519
            $storeTransferDE->getName(),
520
            $productLabelTransfer->getStoreRelation()->getStores()->offsetGet(0)->getName(),
521
            'Given store is invalid for this relation',
522
        );
523
    }
524
525
    /**
526
     * @return void
527
     */
528
    public function testCreateLabelShouldTouchDictionaryActive(): void
529
    {
530
        $productLabelTransfer = (new ProductLabelBuilder())->except(['idProductLabel'])->build();
531
        $productLabelFacade = $this->getProductLabelFacade();
532
        $productLabelFacade->createLabel($productLabelTransfer);
533
534
        $this->tester->assertTouchActive(
535
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
536
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
537
        );
538
    }
539
540
    /**
541
     * @return void
542
     */
543
    public function testFindProductAbstractRelationsByIdProductLabelShouldReturnListOfIdsProductAbstract(): void
544
    {
545
        $productTransfer = $this->tester->haveProduct();
546
        $idProductAbstract = $productTransfer->getFkProductAbstract();
547
        $productLabelTransfer = $this->tester->haveProductLabel();
548
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
549
550
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
551
552
        $productLabelFacade = $this->getProductLabelFacade();
553
        $idsProductAbstract = $productLabelFacade->findProductAbstractRelationsByIdProductLabel($idProductLabel);
554
555
        $this->assertCount(1, $idsProductAbstract);
556
        $this->assertSame($idProductAbstract, $idsProductAbstract[0]);
557
    }
558
559
    /**
560
     * @return void
561
     */
562
    public function testAddProductAbstractRelationsShouldPersistRelations(): void
563
    {
564
        $productTransfer = $this->tester->haveProduct();
565
        $idProductAbstract = $productTransfer->getFkProductAbstract();
566
        $productLabelTransfer = $this->tester->haveProductLabel();
567
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
568
569
        $productLabelFacade = $this->getProductLabelFacade();
570
        $productLabelFacade->addAbstractProductRelationsForLabel($idProductLabel, [$idProductAbstract]);
571
572
        $productLabelTransferCollection = $productLabelFacade->findLabelsByIdProductAbstract($idProductAbstract);
573
574
        $this->assertCount(1, $productLabelTransferCollection);
575
    }
576
577
    /**
578
     * @return void
579
     */
580
    public function testFindProductLabelIdsByIdProductAbstractShouldReturnListOfIds(): void
581
    {
582
        $productTransfer = $this->tester->haveProduct();
583
        $idProductAbstract = $productTransfer->getFkProductAbstract();
584
        $productLabelTransfer = $this->tester->haveProductLabel();
585
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
586
587
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
588
589
        $productLabelFacade = $this->getProductLabelFacade();
590
        $productLabelIds = $productLabelFacade->findLabelIdsByIdProductAbstract($idProductAbstract);
591
592
        $this->assertEquals([$idProductLabel], $productLabelIds);
593
    }
594
595
    /**
596
     * @return void
597
     */
598
    public function testRemoveProductAbstractRelationsShouldRemoveExistingRelations(): void
599
    {
600
        $productTransfer = $this->tester->haveProduct();
601
        $idProductAbstract = $productTransfer->getFkProductAbstract();
602
        $productLabelTransfer = $this->tester->haveProductLabel();
603
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
604
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
605
606
        $productLabelFacade = $this->getProductLabelFacade();
607
        $productLabelFacade->removeProductAbstractRelationsForLabel($idProductLabel, [$idProductAbstract]);
608
609
        $idsProductAbstract = $productLabelFacade->findProductAbstractRelationsByIdProductLabel($idProductLabel);
610
611
        $this->assertCount(0, $idsProductAbstract);
612
    }
613
614
    /**
615
     * @return void
616
     */
617
    public function testAddProductAbstractRelationsShouldTouchRelationsActive(): void
618
    {
619
        $productTransfer = $this->tester->haveProduct();
620
        $idProductAbstract = $productTransfer->getFkProductAbstract();
621
        $productLabelTransfer = $this->tester->haveProductLabel();
622
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
623
624
        $productLabelFacade = $this->getProductLabelFacade();
625
        $productLabelFacade->addAbstractProductRelationsForLabel($idProductLabel, [$idProductAbstract]);
626
627
        $this->tester->assertTouchActive(
628
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_ABSTRACT_PRODUCT_LABEL_RELATIONS,
629
            $idProductAbstract,
630
        );
631
632
        $this->tester->assertTouchActive(
633
            ProductConfig::RESOURCE_TYPE_PRODUCT_ABSTRACT,
634
            $idProductAbstract,
635
        );
636
    }
637
638
    /**
639
     * @return void
640
     */
641
    public function testCheckLabelValidityDateRangeAndTouchTouchesUnpublishedLabelsActiveWhenEnteringValidityDateRange(): void
642
    {
643
        $this->tester->haveProductLabel([
644
            'validFrom' => (new DateTime())->setTimestamp(strtotime('-1 day'))->format('Y-m-d'),
645
            'validTo' => (new DateTime())->setTimestamp(strtotime('+1 day'))->format('Y-m-d'),
646
            'isPublished' => false,
647
        ]);
648
649
        $referenceTime = new DateTime('now');
650
        $productLabelFacade = $this->getProductLabelFacade();
651
        $productLabelFacade->checkLabelValidityDateRangeAndTouch();
652
653
        $this->tester->assertTouchActiveAfter(
654
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
655
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
656
            $referenceTime,
657
        );
658
    }
659
660
    /**
661
     * @return void
662
     */
663
    public function testCheckLabelValidityDateRangeAndTouchTouchesPublishedLabelsDeletedWhenLeavingValidityDateRange(): void
664
    {
665
        $this->tester->haveProductLabel([
666
            'validFrom' => (new DateTime())->setTimestamp(strtotime('-3 day'))->format('Y-m-d'),
667
            'validTo' => (new DateTime())->setTimestamp(strtotime('-1 day'))->format('Y-m-d'),
668
            'isPublished' => true,
669
        ]);
670
671
        $referenceTime = new DateTime('now');
672
        $productLabelFacade = $this->getProductLabelFacade();
673
        $productLabelFacade->checkLabelValidityDateRangeAndTouch();
674
675
        $this->tester->assertTouchActiveAfter(
676
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
677
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
678
            $referenceTime,
679
        );
680
    }
681
682
    /**
683
     * @return void
684
     */
685
    public function testCheckLabelValidityDateRangeAndTouchDoesNotTouchActiveWhenAlreadyPublished(): void
686
    {
687
        $this->tester->haveProductLabel([
688
            'validFrom' => (new DateTime())->setTimestamp(strtotime('-2 day'))->format('Y-m-d'),
689
            'validTo' => (new DateTime())->setTimestamp(strtotime('+1 day'))->format('Y-m-d'),
690
            'isPublished' => true,
691
        ]);
692
        $referenceTime = new DateTime();
693
        $referenceTime->add(new DateInterval('PT1S'));
694
695
        $productLabelFacade = $this->getProductLabelFacade();
696
        $productLabelFacade->checkLabelValidityDateRangeAndTouch();
697
698
        $this->tester->assertNoTouchAfter(
699
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
700
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
701
            $referenceTime,
702
        );
703
    }
704
705
    /**
706
     * @return void
707
     */
708
    public function testCheckLabelValidityDateRangeAndTouchDoesNotTouchDeletedWhenAlreadyUnpublished(): void
709
    {
710
        $this->tester->haveProductLabel([
711
            'validFrom' => (new DateTime())->setTimestamp(strtotime('-3 day'))->format('Y-m-d'),
712
            'validTo' => (new DateTime())->setTimestamp(strtotime('-1 day'))->format('Y-m-d'),
713
            'isPublished' => false,
714
        ]);
715
        $referenceTime = new DateTime();
716
        $referenceTime->add(new DateInterval('PT1S'));
717
718
        $productLabelFacade = $this->getProductLabelFacade();
719
        $productLabelFacade->checkLabelValidityDateRangeAndTouch();
720
721
        $this->tester->assertNoTouchAfter(
722
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY,
723
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_LABEL_DICTIONARY_IDENTIFIER,
724
            $referenceTime,
725
        );
726
    }
727
728
    /**
729
     * @return void
730
     */
731
    public function testUpdateDynamicProductLabelRelationsPersistRelationChanges(): void
732
    {
733
        // Arrange
734
        $productTransfer1 = $this->tester->haveProduct();
735
        $productTransfer2 = $this->tester->haveProduct();
736
        $productTransfer3 = $this->tester->haveProduct();
737
        $productLabelTransfer = $this->tester->haveProductLabel();
738
        $this->tester->haveProductLabelToAbstractProductRelation(
739
            $productLabelTransfer->getIdProductLabel(),
740
            $productTransfer2->getFkProductAbstract(),
741
        );
742
        $this->tester->haveProductLabelToAbstractProductRelation(
743
            $productLabelTransfer->getIdProductLabel(),
744
            $productTransfer3->getFkProductAbstract(),
745
        );
746
747
        $productLabelRelationUpdaterPluginMock = $this->getMockBuilder(ProductLabelRelationUpdaterPluginInterface::class)
748
            ->setMethods(['findProductLabelProductAbstractRelationChanges'])
749
            ->getMock();
750
751
        $productLabelRelationUpdaterPluginMock->method('findProductLabelProductAbstractRelationChanges')->willReturn([
752
            (new ProductLabelProductAbstractRelationsBuilder([
753
                ProductLabelProductAbstractRelationsTransfer::ID_PRODUCT_LABEL => $productLabelTransfer->getIdProductLabel(),
754
                ProductLabelProductAbstractRelationsTransfer::IDS_PRODUCT_ABSTRACT_TO_ASSIGN => [
755
                    $productTransfer1->getFkProductAbstract(),
756
                ],
757
                ProductLabelProductAbstractRelationsTransfer::IDS_PRODUCT_ABSTRACT_TO_DE_ASSIGN => [
758
                    $productTransfer2->getFkProductAbstract(),
759
                    $productTransfer3->getFkProductAbstract(),
760
                ],
761
            ]))->build(),
762
        ]);
763
764
        $this->tester->setDependency(ProductLabelDependencyProvider::PLUGIN_PRODUCT_LABEL_RELATION_UPDATERS, [
765
            $productLabelRelationUpdaterPluginMock,
766
        ]);
767
768
        $this->tester->setConfig(ProductLabelConstants::PRODUCT_LABEL_TO_DE_ASSIGN_CHUNK_SIZE, 1);
769
770
        // Act
771
        $this->getProductLabelFacade()->updateDynamicProductLabelRelations();
772
773
        // Assert
774
        $actualIdProductAbstracts = $this
775
            ->getProductLabelFacade()
776
            ->findProductAbstractRelationsByIdProductLabel($productLabelTransfer->getIdProductLabel());
777
778
        $this->assertEquals([$productTransfer1->getFkProductAbstract()], $actualIdProductAbstracts);
779
780
        $this->tester->assertTouchActive(
781
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_ABSTRACT_PRODUCT_LABEL_RELATIONS,
782
            $productTransfer1->getFkProductAbstract(),
783
        );
784
        $this->tester->assertTouchActive(
785
            ProductConfig::RESOURCE_TYPE_PRODUCT_ABSTRACT,
786
            $productTransfer1->getFkProductAbstract(),
787
        );
788
789
        $this->tester->assertTouchDeleted(
790
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_ABSTRACT_PRODUCT_LABEL_RELATIONS,
791
            $productTransfer2->getFkProductAbstract(),
792
        );
793
        $this->tester->assertTouchDeleted(
794
            ProductLabelConstants::RESOURCE_TYPE_PRODUCT_ABSTRACT_PRODUCT_LABEL_RELATIONS,
795
            $productTransfer3->getFkProductAbstract(),
796
        );
797
        $this->tester->assertTouchActive(
798
            ProductConfig::RESOURCE_TYPE_PRODUCT_ABSTRACT,
799
            $productTransfer1->getFkProductAbstract(),
800
        );
801
    }
802
803
    /**
804
     * @return void
805
     */
806
    public function testGetProductLabelProductAbstractsByProductAbstractIdsReturnsCorrectData(): void
807
    {
808
        // Arrange
809
        $productTransfer1 = $this->tester->haveProduct();
810
        $productLabelTransfer1 = $this->tester->haveProductLabel();
811
        $this->tester->haveProductLabelToAbstractProductRelation(
812
            $productLabelTransfer1->getIdProductLabel(),
813
            $productTransfer1->getFkProductAbstract(),
814
        );
815
816
        $productTransfer2 = $this->tester->haveProduct();
817
        $productLabelTransfer2 = $this->tester->haveProductLabel();
818
        $this->tester->haveProductLabelToAbstractProductRelation(
819
            $productLabelTransfer2->getIdProductLabel(),
820
            $productTransfer2->getFkProductAbstract(),
821
        );
822
823
        $productAbstractIds = [
824
            $productTransfer1->getFkProductAbstract(),
825
            $productTransfer2->getFkProductAbstract(),
826
        ];
827
828
        // Act
829
        $productLabelProductAbstracts = $this->getProductLabelFacade()
830
            ->getProductLabelProductAbstractsByProductAbstractIds($productAbstractIds);
831
832
        // Assert
833
        $this->assertCount(count($productAbstractIds), $productLabelProductAbstracts);
834
    }
835
836
    /**
837
     * @return void
838
     */
839
    public function testGetProductLabelProductAbstractsByProductAbstractIdsReturnsEmptyCollectionIfDataIsNotCorrect(): void
840
    {
841
        // Arrange
842
        $productTransfer = $this->tester->haveProduct();
843
        $productLabelTransfer = $this->tester->haveProductLabel();
844
        $this->tester->haveProductLabelToAbstractProductRelation(
845
            $productLabelTransfer->getIdProductLabel(),
846
            $productTransfer->getFkProductAbstract(),
847
        );
848
849
        $productAbstractIds = [
850
            $productTransfer->getFkProductAbstract() + 1,
851
        ];
852
853
        // Act
854
        $productLabelProductAbstracts = $this->getProductLabelFacade()
855
            ->getProductLabelProductAbstractsByProductAbstractIds($productAbstractIds);
856
857
        // Assert
858
        $this->assertCount(0, $productLabelProductAbstracts);
859
    }
860
861
    /**
862
     * @return void
863
     */
864
    public function testGetProductLabelProductAbstractsByFilterReturnsCorrectData(): void
865
    {
866
        // Arrange
867
        $productTransfer1 = $this->tester->haveProduct();
868
        $productLabelTransfer1 = $this->tester->haveProductLabel();
869
        $this->tester->haveProductLabelToAbstractProductRelation(
870
            $productLabelTransfer1->getIdProductLabel(),
871
            $productTransfer1->getFkProductAbstract(),
872
        );
873
874
        $productTransfer2 = $this->tester->haveProduct();
875
        $productLabelTransfer2 = $this->tester->haveProductLabel();
876
        $this->tester->haveProductLabelToAbstractProductRelation(
877
            $productLabelTransfer2->getIdProductLabel(),
878
            $productTransfer2->getFkProductAbstract(),
879
        );
880
881
        $filterTransfer = (new FilterTransfer())
882
            ->setLimit(1)
883
            ->setOffset(0);
884
885
        // Act
886
        $productLabelProductAbstracts = $this->getProductLabelFacade()
887
            ->getProductLabelProductAbstractsByFilter($filterTransfer);
888
889
        // Assert
890
        $this->assertCount(1, $productLabelProductAbstracts);
891
    }
892
893
    /**
894
     * @return void
895
     */
896
    public function testGetProductLabelCollectionReturnsCorrectProductLabelsWithoutPaginationAndRelations(): void
897
    {
898
        // Arrange
899
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
900
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
901
        $productTransfer = $this->tester->haveProduct();
902
        $storeRelationSeedData = [
903
            StoreRelationTransfer::ID_STORES => [
904
                $storeTransferDE->getIdStore(),
905
            ],
906
        ];
907
908
        $productLabelTransfer1 = $this->tester->haveProductLabel([
909
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedData,
910
        ]);
911
        $this->tester->haveProductLabelToAbstractProductRelation(
912
            $productLabelTransfer1->getIdProductLabel(),
913
            $productTransfer->getFkProductAbstract(),
914
        );
915
        $productLabelTransfer2 = $this->tester->haveProductLabel([
916
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedData,
917
        ]);
918
        $this->tester->haveProductLabelToAbstractProductRelation(
919
            $productLabelTransfer2->getIdProductLabel(),
920
            $productTransfer->getFkProductAbstract(),
921
        );
922
        $productLabelTransfer3 = $this->tester->haveProductLabel([
923
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedData,
924
        ]);
925
        $this->tester->haveProductLabelToAbstractProductRelation(
926
            $productLabelTransfer3->getIdProductLabel(),
927
            $productTransfer->getFkProductAbstract(),
928
        );
929
930
        $productLabelCriteriaTransfer = new ProductLabelCriteriaTransfer();
931
932
        // Act
933
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
934
935
        // Assert
936
        $this->assertCount(3, $productLabelCollectionTransfer->getProductLabels());
937
        $this->assertSame(
938
            $productLabelTransfer1->getIdProductLabel(),
939
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getIdProductLabel(),
940
        );
941
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getLocalizedAttributesCollection());
942
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getProductLabelProductAbstracts());
943
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getStoreRelation()->getStores());
944
        $this->assertSame(
945
            $productLabelTransfer2->getIdProductLabel(),
946
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(1)->getIdProductLabel(),
947
        );
948
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(1)->getLocalizedAttributesCollection());
949
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(1)->getProductLabelProductAbstracts());
950
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(1)->getStoreRelation()->getStores());
951
        $this->assertSame(
952
            $productLabelTransfer3->getIdProductLabel(),
953
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(2)->getIdProductLabel(),
954
        );
955
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(2)->getLocalizedAttributesCollection());
956
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(2)->getProductLabelProductAbstracts());
957
        $this->assertCount(0, $productLabelCollectionTransfer->getProductLabels()->offsetGet(2)->getStoreRelation()->getStores());
958
    }
959
960
    /**
961
     * @return void
962
     */
963
    public function testGetProductLabelCollectionShouldReturnAllRelations(): void
964
    {
965
        // Arrange
966
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
967
        $productTransfer = $this->tester->haveProduct();
968
        $storeTransferDE = $this->tester->haveStore([StoreTransfer::NAME => static::STORE_NAME_DE]);
969
        $storeRelationSeedData = [
970
            StoreRelationTransfer::ID_STORES => [
971
                $storeTransferDE->getIdStore(),
972
            ],
973
        ];
974
        $productLabelTransfer = $this->tester->haveProductLabel([
975
            ProductLabelTransfer::STORE_RELATION => $storeRelationSeedData,
976
        ]);
977
        $this->tester->haveProductLabelToAbstractProductRelation(
978
            $productLabelTransfer->getIdProductLabel(),
979
            $productTransfer->getFkProductAbstract(),
980
        );
981
982
        $productLabelCriteriaTransfer = (new ProductLabelCriteriaTransfer())
983
            ->setWithProductLabelLocalizedAttributes(true)
984
            ->setWithProductLabelProductAbstracts(true)
985
            ->setWithProductLabelStores(true);
986
        $localeTransfers = $this->tester->getLocator()->locale()->facade()->getLocaleCollection();
987
988
        // Act
989
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
990
991
        // Assert
992
        $this->assertCount(1, $productLabelCollectionTransfer->getProductLabels());
993
        $this->assertSame(
994
            $productLabelTransfer->getIdProductLabel(),
995
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getIdProductLabel(),
996
        );
997
998
        $this->assertCount(count($localeTransfers), $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getLocalizedAttributesCollection());
999
1000
        $this->assertCount(1, $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getProductLabelProductAbstracts());
1001
        $this->assertSame(
1002
            $productTransfer->getFkProductAbstract(),
1003
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getProductLabelProductAbstracts()->offsetGet(0)->getFkProductAbstract(),
1004
        );
1005
1006
        $this->assertCount(1, $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getStoreRelation()->getStores());
1007
        $this->assertSame(
1008
            $storeTransferDE->getIdStore(),
1009
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getStoreRelation()->getStores()->offsetGet(0)->getIdStore(),
1010
        );
1011
    }
1012
1013
    /**
1014
     * @return void
1015
     */
1016
    public function testGetProductLabelCollectionReturnsPaginatedProductLabelsWithLimitAndOffset(): void
1017
    {
1018
        // Arrange
1019
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
1020
        $this->tester->haveProductLabel();
1021
        $productLabelTransfer1 = $this->tester->haveProductLabel();
1022
        $productLabelTransfer2 = $this->tester->haveProductLabel();
1023
        $this->tester->haveProductLabel();
1024
        $productLabelCriteriaTransfer = (new ProductLabelCriteriaTransfer())
1025
            ->setPagination(
1026
                (new PaginationTransfer())->setOffset(1)->setLimit(2),
1027
            );
1028
1029
        // Act
1030
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
1031
1032
        // Assert
1033
        $this->assertCount(2, $productLabelCollectionTransfer->getProductLabels());
1034
        $this->assertSame(4, $productLabelCollectionTransfer->getPagination()->getNbResults());
1035
        $this->assertSame(
1036
            $productLabelTransfer1->getIdProductLabel(),
1037
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getIdProductLabel(),
1038
        );
1039
        $this->assertSame(
1040
            $productLabelTransfer2->getIdProductLabel(),
1041
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(1)->getIdProductLabel(),
1042
        );
1043
    }
1044
1045
    /**
1046
     * @return void
1047
     */
1048
    public function testGetProductLabelCollectionShouldReturnCollectionFilteredByIdProductAbstract(): void
1049
    {
1050
        // Arrange
1051
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
1052
1053
        $idProductAbstract = $this->tester->haveProduct()->getFkProductAbstract();
1054
1055
        $productLabelTransfer = $this->tester->haveProductLabel();
1056
        $this->tester->haveProductLabel();
1057
1058
        $this->tester->haveProductLabelToAbstractProductRelation(
1059
            $productLabelTransfer->getIdProductLabel(),
1060
            $idProductAbstract,
1061
        );
1062
1063
        $productLabelCriteriaTransfer = (new ProductLabelCriteriaTransfer())->setProductLabelConditions(
1064
            (new ProductLabelConditionsTransfer())->addProductAbstractId($idProductAbstract),
1065
        );
1066
1067
        // Act
1068
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
1069
1070
        // Assert
1071
        $this->assertCount(1, $productLabelCollectionTransfer->getProductLabels());
1072
        $this->assertSame(
1073
            $productLabelTransfer->getIdProductLabel(),
1074
            $productLabelCollectionTransfer->getProductLabels()->getIterator()->current()->getIdProductLabel(),
1075
        );
1076
    }
1077
1078
    /**
1079
     * @return void
1080
     */
1081
    public function testProductConcretesSuccessfullyExpandedWithLabels(): void
1082
    {
1083
        // Arrange
1084
        $productConcreteTransfer = $this->tester->haveProduct();
1085
        $productLabelTransfer = $this->tester->haveProductLabel();
1086
        $this->tester->haveProductLabelToAbstractProductRelation(
1087
            $productLabelTransfer->getIdProductLabel(),
1088
            $productConcreteTransfer->getFkProductAbstract(),
1089
        );
1090
1091
        $productConcreteTransfers = [$productConcreteTransfer];
1092
1093
        // Act
1094
        $expandedProductConcreteTransfers = $this->tester->getFacade()->expandProductConcretesWithLabels($productConcreteTransfers);
1095
1096
        // Assert
1097
        $this->assertSame(
1098
            $expandedProductConcreteTransfers[0]->getProductLabels()->offsetGet(0)->getIdProductLabel(),
1099
            $productLabelTransfer->getIdProductLabel(),
1100
        );
1101
    }
1102
1103
    /**
1104
     * Tests if the repository do not set the wrong ProductLabelTransfer to a ProductLabelProductAbstractTransfer when
1105
     * the product has more then one label assigned to it.
1106
     *
1107
     * This bug was reported on https://spryker.atlassian.net/browse/PBC-845.
1108
     *
1109
     * @return void
1110
     */
1111
    public function testExpandProductConcretesWithLabelsAssignCorrectLabelsToProductWhenProductHasMoreThanOneLabels(): void
1112
    {
1113
        // Arrange
1114
        $productTransfer = $this->tester->haveProduct();
1115
        $idProductAbstract = $productTransfer->getFkProductAbstract();
1116
1117
        $productLabelTransfer1 = $this->tester->haveProductLabel();
1118
        $idProductLabel1 = $productLabelTransfer1->getIdProductLabel();
1119
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel1, $idProductAbstract);
1120
1121
        $productLabelTransfer2 = $this->tester->haveProductLabel();
1122
        $idProductLabel2 = $productLabelTransfer2->getIdProductLabel();
1123
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel2, $idProductAbstract);
1124
1125
        $productConcreteTransfers = [$productTransfer];
1126
1127
        // Act
1128
        $expandedProductConcreteTransfers = $this->tester->getFacade()->expandProductConcretesWithLabels(
1129
            $productConcreteTransfers,
1130
        );
1131
1132
        $expandedLabelsids = [
1133
            $expandedProductConcreteTransfers[0]->getProductLabels()->offsetGet(0)->getIdProductLabel(),
1134
            $expandedProductConcreteTransfers[0]->getProductLabels()->offsetGet(1)->getIdProductLabel(),
1135
        ];
1136
1137
        // Assert
1138
        $this->assertContains($productLabelTransfer1->getIdProductLabel(), $expandedLabelsids);
1139
        $this->assertContains($productLabelTransfer2->getIdProductLabel(), $expandedLabelsids);
1140
    }
1141
1142
    /**
1143
     * @return void
1144
     */
1145
    public function testGetProductLabelCollectionShouldReturnCollectionFilteredByIsActive(): void
1146
    {
1147
        // Arrange
1148
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
1149
1150
        $productLabelTransfer = $this->tester->haveProductLabel([
1151
            ProductLabelTransfer::IS_ACTIVE => true,
1152
        ]);
1153
        $this->tester->haveProductLabel([
1154
            ProductLabelTransfer::IS_ACTIVE => false,
1155
        ]);
1156
        $this->tester->haveProductLabel([
1157
            ProductLabelTransfer::IS_ACTIVE => true,
1158
            ProductLabelTransfer::VALID_TO => date('Y-m-d H:i:s', strtotime('-1 day')),
1159
        ]);
1160
        $this->tester->haveProductLabel([
1161
            ProductLabelTransfer::IS_ACTIVE => true,
1162
            ProductLabelTransfer::VALID_FROM => date('Y-m-d H:i:s', strtotime('+1 day')),
1163
        ]);
1164
1165
        $productLabelCriteriaTransfer = (new ProductLabelCriteriaTransfer())->setProductLabelConditions(
1166
            (new ProductLabelConditionsTransfer())->setIsActive(true),
1167
        );
1168
1169
        // Act
1170
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
1171
1172
        // Assert
1173
        $this->assertCount(1, $productLabelCollectionTransfer->getProductLabels());
1174
        $this->assertSame(
1175
            $productLabelTransfer->getIdProductLabel(),
1176
            $productLabelCollectionTransfer->getProductLabels()->getIterator()->current()->getIdProductLabel(),
1177
        );
1178
    }
1179
1180
    /**
1181
     * @return void
1182
     */
1183
    public function testGetProductLabelCollectionShouldReturnCollectionSortedBySortCollection(): void
1184
    {
1185
        // Arrange
1186
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
1187
1188
        $productLabelTransfer = $this->tester->haveProductLabel([
1189
            ProductLabelTransfer::IS_EXCLUSIVE => true,
1190
            ProductLabelTransfer::POSITION => 3,
1191
        ]);
1192
        $productLabelTransfer2 = $this->tester->haveProductLabel([
1193
            ProductLabelTransfer::IS_EXCLUSIVE => false,
1194
            ProductLabelTransfer::POSITION => 2,
1195
        ]);
1196
        $productLabelTransfer3 = $this->tester->haveProductLabel([
1197
            ProductLabelTransfer::IS_EXCLUSIVE => false,
1198
            ProductLabelTransfer::POSITION => 1,
1199
        ]);
1200
1201
        $productLabelCriteriaTransfer = (new ProductLabelCriteriaTransfer())->setSortCollection(new ArrayObject([
1202
            (new SortTransfer())->setField(ProductLabelTransfer::IS_EXCLUSIVE)->setIsAscending(false),
1203
            (new SortTransfer())->setField(ProductLabelTransfer::POSITION)->setIsAscending(true),
1204
        ]));
1205
1206
        // Act
1207
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
1208
1209
        // Assert
1210
        $this->assertCount(3, $productLabelCollectionTransfer->getProductLabels());
1211
        $this->assertSame(
1212
            $productLabelTransfer->getIdProductLabel(),
1213
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(0)->getIdProductLabel(),
1214
        );
1215
        $this->assertSame(
1216
            $productLabelTransfer3->getIdProductLabel(),
1217
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(1)->getIdProductLabel(),
1218
        );
1219
        $this->assertSame(
1220
            $productLabelTransfer2->getIdProductLabel(),
1221
            $productLabelCollectionTransfer->getProductLabels()->offsetGet(2)->getIdProductLabel(),
1222
        );
1223
    }
1224
1225
    /**
1226
     * @return void
1227
     */
1228
    public function testGetProductLabelCollectionShouldReturnUniqueProductLabelsFilteredByProductAbstractIds(): void
1229
    {
1230
        // Arrange
1231
        $this->tester->ensureDatabaseTableIsEmpty(SpyProductLabelQuery::create());
1232
1233
        $firstProductTransfer = $this->tester->haveProduct();
1234
        $secondProductTransfer = $this->tester->haveProduct();
1235
1236
        $firstProductLabelTransfer = $this->tester->haveProductLabel();
1237
        $secondProductLabelTransfer = $this->tester->haveProductLabel();
1238
1239
        $this->tester->haveProductLabelToAbstractProductRelation(
1240
            $firstProductLabelTransfer->getIdProductLabel(),
1241
            $firstProductTransfer->getFkProductAbstract(),
1242
        );
1243
        $this->tester->haveProductLabelToAbstractProductRelation(
1244
            $secondProductLabelTransfer->getIdProductLabel(),
1245
            $firstProductTransfer->getFkProductAbstract(),
1246
        );
1247
        $this->tester->haveProductLabelToAbstractProductRelation(
1248
            $firstProductLabelTransfer->getIdProductLabel(),
1249
            $secondProductTransfer->getFkProductAbstract(),
1250
        );
1251
        $this->tester->haveProductLabelToAbstractProductRelation(
1252
            $secondProductLabelTransfer->getIdProductLabel(),
1253
            $secondProductTransfer->getFkProductAbstract(),
1254
        );
1255
1256
        $productLabelCriteriaTransfer = (new ProductLabelCriteriaTransfer())
1257
            ->setProductLabelConditions(
1258
                (new ProductLabelConditionsTransfer())
1259
                    ->setIsActive(true)
1260
                    ->addProductAbstractId($firstProductTransfer->getFkProductAbstract())
1261
                    ->addProductAbstractId($secondProductTransfer->getFkProductAbstract()),
1262
            );
1263
1264
        // Act
1265
        $productLabelCollectionTransfer = $this->tester->getFacade()->getProductLabelCollection($productLabelCriteriaTransfer);
1266
1267
        // Assert
1268
        $this->assertCount(2, $productLabelCollectionTransfer->getProductLabels());
1269
    }
1270
1271
    /**
1272
     * @return void
1273
     */
1274
    public function testExpandProductConcretesWithLabelsReturnsEmptyArrayWhenProductAreEmpty(): void
1275
    {
1276
        // Arrange
1277
        $productConcreteTransfers = [];
1278
1279
        // Act
1280
        $expandedProductConcreteTransfers = $this->tester->getFacade()->expandProductConcretesWithLabels($productConcreteTransfers);
1281
1282
        // Assert
1283
        $this->assertEmpty($expandedProductConcreteTransfers);
1284
    }
1285
1286
    /**
1287
     * @return \Spryker\Zed\ProductLabel\Business\ProductLabelFacadeInterface
1288
     */
1289
    protected function getProductLabelFacade(): ProductLabelFacadeInterface
1290
    {
1291
        return $this->tester->getLocator()->productLabel()->facade();
1292
    }
1293
1294
    /**
1295
     * @param int|null $fkLocale
1296
     * @param int|null $fkProductLabel
1297
     *
1298
     * @return \Generated\Shared\Transfer\ProductLabelLocalizedAttributesTransfer
1299
     */
1300
    protected function generateLocalizedAttributesTransfer(?int $fkLocale = null, ?int $fkProductLabel = null): ProductLabelLocalizedAttributesTransfer
1301
    {
1302
        $builder = new ProductLabelLocalizedAttributesBuilder([
1303
            'fkProductLabel' => $fkProductLabel,
1304
            'fkLocale' => $fkLocale,
1305
        ]);
1306
1307
        return $builder->build();
1308
    }
1309
1310
    /**
1311
     * @return void
1312
     */
1313
    public function testAddProductAbstractRelationsEmitsProductEvents(): void
1314
    {
1315
        // Arrange
1316
        $productTransfer = $this->tester->haveProduct();
1317
        $idProductAbstract = $productTransfer->getFkProductAbstract();
1318
        $productLabelTransfer = $this->tester->haveProductLabel();
1319
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
1320
1321
        // Assert
1322
        $this->assertProductEventWithProductAbstractIdIsEmitted($idProductAbstract);
1323
1324
        // Act
1325
        $this->tester->getFacade()->addAbstractProductRelationsForLabel($idProductLabel, [$idProductAbstract]);
1326
    }
1327
1328
    /**
1329
     * @return void
1330
     */
1331
    public function testUpdatingLabelEmitsProductEventsForRelatedProducts(): void
1332
    {
1333
        // Arrange
1334
        $productTransfer = $this->tester->haveProduct();
1335
        $idProductAbstract = $productTransfer->getFkProductAbstract();
1336
        $productLabelTransfer = $this->tester->haveProductLabel();
1337
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
1338
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
1339
1340
        // Assert
1341
        $this->assertProductEventWithProductAbstractIdIsEmitted($idProductAbstract);
1342
1343
        // Act
1344
        $this->tester->getFacade()->updateLabel($productLabelTransfer);
1345
    }
1346
1347
    /**
1348
     * @return void
1349
     */
1350
    public function testRemoveAbstractRelationsEmitsProductEvent(): void
1351
    {
1352
        // Arrange
1353
        $productTransfer = $this->tester->haveProduct();
1354
        $idProductAbstract = $productTransfer->getFkProductAbstract();
1355
        $productLabelTransfer = $this->tester->haveProductLabel();
1356
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
1357
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
1358
1359
        // Assert
1360
        $this->assertProductEventWithProductAbstractIdIsEmitted($idProductAbstract);
1361
1362
        // Act
1363
        $this->tester->getFacade()->removeProductAbstractRelationsForLabel($idProductLabel, [$idProductAbstract]);
1364
    }
1365
1366
    /**
1367
     * @return void
1368
     */
1369
    public function testLabelRemovalEmitsProductEventForRelatedProducts(): void
1370
    {
1371
        // Arrange
1372
        $productTransfer = $this->tester->haveProduct();
1373
        $idProductAbstract = $productTransfer->getFkProductAbstract();
1374
        $productLabelTransfer = $this->tester->haveProductLabel();
1375
        $idProductLabel = $productLabelTransfer->getIdProductLabel();
1376
        $this->tester->haveProductLabelToAbstractProductRelation($idProductLabel, $idProductAbstract);
1377
1378
        // Assert
1379
        $this->assertProductEventWithProductAbstractIdIsEmitted($idProductAbstract);
1380
1381
        // Act
1382
        $this->tester->getFacade()->removeLabel($productLabelTransfer);
1383
    }
1384
1385
    /**
1386
     * @return void
1387
     */
1388
    public function testTriggerProductAbstractUpdateEventsByProductLabelEventsTriggerEventsWithCorrectProductAbstracts(): void
1389
    {
1390
        // Arrange
1391
        $productTransfer = $this->tester->haveProduct();
1392
        $productLabelTransfer = $this->tester->haveProductLabel();
1393
        $this->tester->haveProductLabelToAbstractProductRelation($productLabelTransfer->getIdProductLabelOrFail(), $productTransfer->getFkProductAbstractOrFail());
1394
1395
        // Assert
1396
        $this->assertProductEventWithProductAbstractIdIsEmitted($productTransfer->getFkProductAbstractOrFail());
1397
1398
        // Act
1399
        $this->tester->getFacade()->triggerProductAbstractUpdateEventsByProductLabelEvents([
1400
                (new EventEntityTransfer())->setId($productLabelTransfer->getIdProductLabelOrFail()),
1401
           ]);
1402
    }
1403
1404
    /**
1405
     * @return void
1406
     */
1407
    public function testTriggerProductAbstractUpdateEventsByProductLabelLocalizedAttributeEventsTriggerEventsWithCorrectProductAbstracts(): void
1408
    {
1409
        // Arrange
1410
        $productTransfer = $this->tester->haveProduct();
1411
        $productLabelTransfer = $this->tester->haveProductLabel();
1412
        $this->tester->haveProductLabelToAbstractProductRelation($productLabelTransfer->getIdProductLabelOrFail(), $productTransfer->getFkProductAbstractOrFail());
1413
1414
        // Assert
1415
        $this->assertProductEventWithProductAbstractIdIsEmitted($productTransfer->getFkProductAbstractOrFail());
1416
1417
        // Act
1418
        $this->tester->getFacade()->triggerProductAbstractUpdateEventsByProductLabelEvents([
1419
                (new EventEntityTransfer())
1420
                    ->setName('spy_product_label_localized_attributes')
1421
                    ->setForeignKeys(['spy_product_label_localized_attributes.fk_product_label' => $productLabelTransfer->getIdProductLabelOrFail()]),
1422
            ]);
1423
    }
1424
1425
    /**
1426
     * @param int $idProductAbstract
1427
     *
1428
     * @return void
1429
     */
1430
    protected function assertProductEventWithProductAbstractIdIsEmitted(int $idProductAbstract): void
1431
    {
1432
        $this->eventFacade
1433
            ->expects($this->atLeastOnce())
0 ignored issues
show
The method expects() does not exist on Spryker\Zed\ProductLabel...ctLabelToEventInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1433
            ->/** @scrutinizer ignore-call */ 
1434
              expects($this->atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1434
            ->method('triggerBulk')
1435
            ->with(static::PRODUCT_ABSTRACT_UPDATE, $this->callback(
1436
                function ($transfers) use ($idProductAbstract) {
1437
                    $this->assertNotEmpty($transfers);
1438
                    $this->assertInstanceOf(EventEntityTransfer::class, $transfers[0]);
1439
                    $this->assertEquals($transfers[0]->getId(), $idProductAbstract);
1440
1441
                    return true;
1442
                },
1443
            ));
1444
    }
1445
1446
    /**
1447
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\ProductLabel\Dependency\Facade\ProductLabelToTouchInterface
1448
     */
1449
    protected function createTouchFacadeMock(): ProductLabelToTouchInterface
1450
    {
1451
        return $this->createMock(ProductLabelToTouchInterface::class);
1452
    }
1453
1454
    /**
1455
     * @return \PHPUnit\Framework\MockObject\MockObject|\Spryker\Zed\ProductLabel\Dependency\Facade\ProductLabelToProductInterface
1456
     */
1457
    protected function createProductFacadeMock(): ProductLabelToProductInterface
1458
    {
1459
        return $this->createMock(ProductLabelToProductInterface::class);
1460
    }
1461
}
1462