Passed
Push — master ( cacbf1...74b6c9 )
by
unknown
34:14
created

testHandleBulkWithMultipleEventEntityTransfers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 38
nc 1
nop 0
dl 0
loc 62
rs 9.312
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
declare(strict_types=1);
9
10
namespace SprykerFeatureTest\Zed\SelfServicePortal\Communication\Plugin\Publisher\SspAsset\Search;
11
12
use Codeception\Test\Unit;
13
use Generated\Shared\Search\SspAssetIndexMap;
14
use Generated\Shared\Transfer\CompanyBusinessUnitTransfer;
15
use Generated\Shared\Transfer\EventEntityTransfer;
16
use Generated\Shared\Transfer\SspAssetBusinessUnitAssignmentTransfer;
17
use Generated\Shared\Transfer\SspAssetTransfer;
18
use Generated\Shared\Transfer\SspModelTransfer;
19
use Generated\Shared\Transfer\StoreTransfer;
20
use Orm\Zed\SelfServicePortal\Persistence\SpySspAssetSearchQuery;
21
use Spryker\Client\Kernel\Container;
22
use Spryker\Client\Queue\QueueDependencyProvider;
23
use Spryker\Zed\Store\Business\StoreFacadeInterface;
24
use Spryker\Zed\Store\StoreDependencyProvider;
25
use SprykerFeature\Shared\SelfServicePortal\SelfServicePortalConfig;
26
use SprykerFeature\Zed\SelfServicePortal\Communication\Plugin\Publisher\SspAsset\Search\SspAssetToModelWritePublisherPlugin;
27
use SprykerFeature\Zed\SelfServicePortal\SelfServicePortalDependencyProvider;
28
use SprykerFeatureTest\Zed\SelfServicePortal\SelfServicePortalCommunicationTester;
29
30
/**
31
 * @group SprykerFeatureTest
32
 * @group Zed
33
 * @group SelfServicePortal
34
 * @group Communication
35
 * @group Plugin
36
 * @group Publisher
37
 * @group SspAssetToModelWritePublisherPluginTest
38
 */
39
class SspAssetToModelWritePublisherPluginTest extends Unit
40
{
41
    /**
42
     * @var string
43
     */
44
    protected const STORE_NAME_UF = 'UF';
45
46
    /**
47
     * @var string
48
     */
49
    protected const STORE_NAME_LO = 'LO';
50
51
    /**
52
     * @uses \Orm\Zed\SelfServicePortal\Persistence\Map\SpySspAssetToSspModelTableMap::COL_FK_SSP_ASSET
53
     *
54
     * @var string
55
     */
56
    protected const COL_FK_SSP_ASSET = 'spy_ssp_asset_to_ssp_model.fk_ssp_asset';
57
58
    /**
59
     * @uses \Orm\Zed\SelfServicePortal\Persistence\Map\SpySspAssetToSspModelTableMap::COL_FK_SSP_MODEL
60
     *
61
     * @var string
62
     */
63
    protected const COL_FK_SSP_MODEL = 'spy_ssp_asset_to_ssp_model.fk_ssp_model';
64
65
    protected SelfServicePortalCommunicationTester $tester;
66
67
    protected function setUp(): void
68
    {
69
        parent::setUp();
70
71
        $storeFacadeMock = $this->createMock(StoreFacadeInterface::class);
72
        $storeFacadeMock->method('getAllStores')->willReturn([
73
            (new StoreTransfer())->setName(static::STORE_NAME_UF),
74
            (new StoreTransfer())->setName(static::STORE_NAME_LO),
75
        ]);
76
77
        $this->tester->setDependency(
78
            SelfServicePortalDependencyProvider::FACADE_STORE,
79
            $storeFacadeMock,
80
        );
81
82
        $this->tester->setDependency(
83
            QueueDependencyProvider::QUEUE_ADAPTERS,
84
            function (Container $container) {
85
                return [
86
                    $container->getLocator()->rabbitMq()->client()->createQueueAdapter(),
87
                ];
88
            },
89
        );
90
91
        $this->tester->setDependency(
92
            StoreDependencyProvider::SERVICE_STORE,
93
            static::STORE_NAME_UF,
94
        );
95
96
        $this->tester->ensureSspAssetToCompanyBusinessUnitTableIsEmpty();
97
        $this->tester->ensureSspAssetToSspModelTableIsEmpty();
98
        $this->tester->ensureSspAssetTableIsEmpty();
99
    }
100
101
    public function testHandleBulkWritesSspAssetSearchDataOnModelAssignmentCreate(): void
102
    {
103
        // Arrange
104
        $companyTransfer = $this->tester->haveCompany();
105
        $businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([
106
            CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(),
107
            CompanyBusinessUnitTransfer::COMPANY => $companyTransfer,
108
        ]);
109
110
        $sspModelTransfer1 = $this->tester->haveSspModel();
111
        $sspModelTransfer2 = $this->tester->haveSspModel();
112
113
        $sspAssetTransfer = $this->tester->haveAsset([
114
            SspAssetTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer,
115
            SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [
116
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer],
117
            ],
118
            SspAssetTransfer::SSP_MODELS => [
119
                $sspModelTransfer1->modifiedToArray(false, true),
120
            ],
121
        ]);
122
123
        // Act
124
        $sspAssetToModelWritePublisherPlugin = new SspAssetToModelWritePublisherPlugin();
125
        $sspAssetToModelWritePublisherPlugin->handleBulk(
126
            [
127
                (new EventEntityTransfer())
128
                    ->setForeignKeys([
129
                        static::COL_FK_SSP_ASSET => $sspAssetTransfer->getIdSspAsset(),
130
                        static::COL_FK_SSP_MODEL => $sspModelTransfer2->getIdSspModel(),
131
                    ]),
132
            ],
133
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_CREATE,
134
        );
135
136
        // Assert
137
        $sspAssetSearchEntity = SpySspAssetSearchQuery::create()
138
            ->filterByFkSspAsset($sspAssetTransfer->getIdSspAsset())
139
            ->findOne();
140
141
        $this->assertNotNull($sspAssetSearchEntity);
142
        $this->assertSame($sspAssetTransfer->getIdSspAsset(), $sspAssetSearchEntity->getFkSspAsset());
143
144
        $searchData = $sspAssetSearchEntity->getData();
145
        $this->assertArrayHasKey(SspAssetIndexMap::SEARCH_RESULT_DATA, $searchData);
146
147
        $resultData = $searchData[SspAssetIndexMap::SEARCH_RESULT_DATA];
148
        $this->assertArrayHasKey('model_ids', $resultData);
149
        $this->assertContains($sspModelTransfer1->getIdSspModel(), $resultData['model_ids']);
150
    }
151
152
    public function testHandleBulkWritesSspAssetSearchDataOnModelAssignmentDelete(): void
153
    {
154
        // Arrange
155
        $companyTransfer = $this->tester->haveCompany();
156
        $businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([
157
            CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(),
158
            CompanyBusinessUnitTransfer::COMPANY => $companyTransfer,
159
        ]);
160
161
        $sspModelTransfer1 = $this->tester->haveSspModel();
162
        $sspModelTransfer2 = $this->tester->haveSspModel();
163
164
        $sspAssetTransfer = $this->tester->haveAsset([
165
            SspAssetTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer,
166
            SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [
167
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer],
168
            ],
169
            SspAssetTransfer::SSP_MODELS => [
170
                $sspModelTransfer1->modifiedToArray(false, true),
171
                $sspModelTransfer2->modifiedToArray(false, true),
172
            ],
173
        ]);
174
175
        $this->tester->ensureSspAssetToCompanyBusinessUnitTableIsEmpty();
176
        $this->tester->ensureSspAssetToSspModelTableIsEmpty();
177
        $this->tester->ensureSspAssetTableIsEmpty();
178
179
        // Act
180
        $sspAssetToModelWritePublisherPlugin = new SspAssetToModelWritePublisherPlugin();
181
        $sspAssetToModelWritePublisherPlugin->handleBulk(
182
            [
183
                (new EventEntityTransfer())
184
                    ->setForeignKeys([
185
                        static::COL_FK_SSP_ASSET => $sspAssetTransfer->getIdSspAsset(),
186
                        static::COL_FK_SSP_MODEL => $sspModelTransfer2->getIdSspModel(),
187
                    ]),
188
            ],
189
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_DELETE,
190
        );
191
192
        // Assert
193
        $sspAssetSearchEntity = SpySspAssetSearchQuery::create()
194
            ->filterByFkSspAsset($sspAssetTransfer->getIdSspAsset())
195
            ->findOne();
196
197
        $this->assertNull($sspAssetSearchEntity);
198
    }
199
200
    public function testHandleBulkUpdatesFullTextBoostedAndSuggestionTermsWithModelNames(): void
201
    {
202
        // Arrange
203
        $companyTransfer = $this->tester->haveCompany();
204
        $businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([
205
            CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(),
206
            CompanyBusinessUnitTransfer::COMPANY => $companyTransfer,
207
        ]);
208
209
        $sspModelTransfer = $this->tester->haveSspModel([
210
            SspModelTransfer::NAME => 'Test Model Name',
211
        ]);
212
213
        $sspAssetTransfer = $this->tester->haveAsset([
214
            SspAssetTransfer::NAME => 'Test Asset Name',
215
            SspAssetTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer,
216
            SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [
217
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer],
218
            ],
219
             SspAssetTransfer::SSP_MODELS => [
220
                $sspModelTransfer->modifiedToArray(false, true),
221
            ],
222
        ]);
223
224
        // Act
225
        $sspAssetToModelWritePublisherPlugin = new SspAssetToModelWritePublisherPlugin();
226
        $sspAssetToModelWritePublisherPlugin->handleBulk(
227
            [
228
                (new EventEntityTransfer())
229
                    ->setForeignKeys([
230
                        static::COL_FK_SSP_ASSET => $sspAssetTransfer->getIdSspAsset(),
231
                        static::COL_FK_SSP_MODEL => $sspModelTransfer->getIdSspModel(),
232
                    ]),
233
            ],
234
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_CREATE,
235
        );
236
237
        // Assert
238
        $sspAssetSearchEntity = SpySspAssetSearchQuery::create()
239
            ->filterByFkSspAsset($sspAssetTransfer->getIdSspAsset())
240
            ->findOne();
241
242
        $this->assertNotNull($sspAssetSearchEntity);
243
244
        $searchData = $sspAssetSearchEntity->getData();
245
        $this->assertArrayHasKey(SspAssetIndexMap::FULL_TEXT_BOOSTED, $searchData);
246
        $this->assertArrayHasKey(SspAssetIndexMap::SUGGESTION_TERMS, $searchData);
247
        $this->assertArrayHasKey(SspAssetIndexMap::COMPLETION_TERMS, $searchData);
248
249
        $this->assertContains('Test Asset Name', $searchData[SspAssetIndexMap::FULL_TEXT_BOOSTED]);
250
        $this->assertContains('Test Model Name', $searchData[SspAssetIndexMap::FULL_TEXT_BOOSTED]);
251
252
        $this->assertContains('Test Asset Name', $searchData[SspAssetIndexMap::SUGGESTION_TERMS]);
253
        $this->assertContains('Test Model Name', $searchData[SspAssetIndexMap::SUGGESTION_TERMS]);
254
255
        $this->assertContains('Test Asset Name', $searchData[SspAssetIndexMap::COMPLETION_TERMS]);
256
        $this->assertContains('Test Model Name', $searchData[SspAssetIndexMap::COMPLETION_TERMS]);
257
    }
258
259
    public function testHandleBulkWithMultipleEventEntityTransfers(): void
260
    {
261
        // Arrange
262
        $companyTransfer = $this->tester->haveCompany();
263
        $businessUnitTransfer = $this->tester->haveCompanyBusinessUnit([
264
            CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer->getIdCompany(),
265
            CompanyBusinessUnitTransfer::COMPANY => $companyTransfer,
266
        ]);
267
268
        $sspModelTransfer1 = $this->tester->haveSspModel();
269
        $sspModelTransfer2 = $this->tester->haveSspModel();
270
271
        $sspAssetTransfer1 = $this->tester->haveAsset([
272
            SspAssetTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer,
273
            SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [
274
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer],
275
            ],
276
        ]);
277
        $sspAssetTransfer2 = $this->tester->haveAsset([
278
            SspAssetTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer,
279
            SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [
280
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer],
281
            ],
282
        ]);
283
284
        // Act
285
        $sspAssetToModelWritePublisherPlugin = new SspAssetToModelWritePublisherPlugin();
286
        $sspAssetToModelWritePublisherPlugin->handleBulk(
287
            [
288
                (new EventEntityTransfer())
289
                    ->setForeignKeys([
290
                        static::COL_FK_SSP_ASSET => $sspAssetTransfer1->getIdSspAsset(),
291
                        static::COL_FK_SSP_MODEL => $sspModelTransfer1->getIdSspModel(),
292
                    ]),
293
                (new EventEntityTransfer())
294
                    ->setForeignKeys([
295
                        static::COL_FK_SSP_ASSET => $sspAssetTransfer2->getIdSspAsset(),
296
                        static::COL_FK_SSP_MODEL => $sspModelTransfer2->getIdSspModel(),
297
                    ]),
298
            ],
299
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_CREATE,
300
        );
301
302
        // Assert
303
        $sspAssetSearchEntities = SpySspAssetSearchQuery::create()
304
            ->filterByFkSspAsset_In([
305
                $sspAssetTransfer1->getIdSspAsset(),
306
                $sspAssetTransfer2->getIdSspAsset(),
307
            ])
308
            ->orderByFkSspAsset()
309
            ->find();
310
311
        $this->assertCount(2, $sspAssetSearchEntities);
312
313
        $sspAssetSearchEntity1 = $sspAssetSearchEntities[0];
314
        $sspAssetSearchEntity2 = $sspAssetSearchEntities[1];
315
316
        $this->assertSame($sspAssetTransfer1->getIdSspAsset(), $sspAssetSearchEntity1->getFkSspAsset());
317
        $this->assertSame($sspAssetTransfer2->getIdSspAsset(), $sspAssetSearchEntity2->getFkSspAsset());
318
319
        $this->assertSearchDataStructure($sspAssetSearchEntity1->getData());
320
        $this->assertSearchDataStructure($sspAssetSearchEntity2->getData());
321
    }
322
323
    public function testGetSubscribedEventsReturnsCorrectEvents(): void
324
    {
325
        // Arrange
326
        $sspAssetToModelWritePublisherPlugin = new SspAssetToModelWritePublisherPlugin();
327
328
        // Act
329
        $subscribedEvents = $sspAssetToModelWritePublisherPlugin->getSubscribedEvents();
330
331
        // Assert
332
        $expectedEvents = [
333
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_CREATE,
334
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_UPDATE,
335
            SelfServicePortalConfig::ENTITY_SPY_SSP_ASSET_TO_MODEL_DELETE,
336
        ];
337
338
        $this->assertSame($expectedEvents, $subscribedEvents);
339
    }
340
341
    /**
342
     * @param array<string, mixed> $data
343
     *
344
     * @return void
345
     */
346
    protected function assertSearchDataStructure(array $data): void
347
    {
348
        $this->assertArrayHasKey(SspAssetIndexMap::TYPE, $data);
349
        $this->assertArrayHasKey(SspAssetIndexMap::SEARCH_RESULT_DATA, $data);
350
        $this->assertArrayHasKey(SspAssetIndexMap::FULL_TEXT_BOOSTED, $data);
351
        $this->assertArrayHasKey(SspAssetIndexMap::SUGGESTION_TERMS, $data);
352
        $this->assertArrayHasKey(SspAssetIndexMap::COMPLETION_TERMS, $data);
353
        $this->assertArrayHasKey(SspAssetIndexMap::STORE, $data);
354
        $this->assertArrayHasKey(SspAssetIndexMap::BUSINESS_UNIT_IDS, $data);
355
        $this->assertArrayHasKey(SspAssetIndexMap::COMPANY_IDS, $data);
356
        $this->assertArrayHasKey(SspAssetIndexMap::ID_OWNER_BUSINESS_UNIT, $data);
357
        $this->assertArrayHasKey(SspAssetIndexMap::ID_OWNER_COMPANY_ID, $data);
358
359
        $this->assertSame(SelfServicePortalConfig::SSP_ASSET_RESOURCE_NAME, $data[SspAssetIndexMap::TYPE]);
360
        $this->assertSame([static::STORE_NAME_UF, static::STORE_NAME_LO], $data[SspAssetIndexMap::STORE]);
361
    }
362
}
363