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

SspAssetWritePublisherPluginTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 108
dl 0
loc 177
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 32 1
A assertSspAssetSearchData() 0 63 1
A testHandleBulkWritesSspAssetSearchData() 0 58 1
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\SspAssetWritePublisherPlugin;
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 SspAssetWritePublisherPluginTest
38
 */
39
class SspAssetWritePublisherPluginTest 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
    protected SelfServicePortalCommunicationTester $tester;
52
53
    protected function setUp(): void
54
    {
55
        parent::setUp();
56
57
        $storeFacadeMock = $this->createMock(StoreFacadeInterface::class);
58
        $storeFacadeMock->method('getAllStores')->willReturn([
59
            (new StoreTransfer())->setName(static::STORE_NAME_UF),
60
            (new StoreTransfer())->setName(static::STORE_NAME_LO),
61
        ]);
62
63
        $this->tester->setDependency(
64
            SelfServicePortalDependencyProvider::FACADE_STORE,
65
            $storeFacadeMock,
66
        );
67
68
        $this->tester->setDependency(
69
            QueueDependencyProvider::QUEUE_ADAPTERS,
70
            function (Container $container) {
71
                return [
72
                    $container->getLocator()->rabbitMq()->client()->createQueueAdapter(),
73
                ];
74
            },
75
        );
76
77
        $this->tester->setDependency(
78
            StoreDependencyProvider::SERVICE_STORE,
79
            static::STORE_NAME_UF,
80
        );
81
82
        $this->tester->ensureSspAssetToCompanyBusinessUnitTableIsEmpty();
83
        $this->tester->ensureSspAssetToSspModelTableIsEmpty();
84
        $this->tester->ensureSspAssetTableIsEmpty();
85
    }
86
87
    public function testHandleBulkWritesSspAssetSearchData(): void
88
    {
89
        // Arrange
90
        $companyTransfer1 = $this->tester->haveCompany();
91
        $companyTransfer2 = $this->tester->haveCompany();
92
        $businessUnitTransfer1 = $this->tester->haveCompanyBusinessUnit([
93
            CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer1->getIdCompany(),
94
            CompanyBusinessUnitTransfer::COMPANY => $companyTransfer1,
95
        ]);
96
        $businessUnitTransfer2 = $this->tester->haveCompanyBusinessUnit([
97
            CompanyBusinessUnitTransfer::FK_COMPANY => $companyTransfer2->getIdCompany(),
98
            CompanyBusinessUnitTransfer::COMPANY => $companyTransfer2->getIdCompany(),
99
        ]);
100
101
        $sspModelTransfer1 = $this->tester->haveSspModel();
102
        $sspModelTransfer2 = $this->tester->haveSspModel();
103
104
        $sspAssetTransfer1 = $this->tester->haveAsset([
105
            SspAssetTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer1,
106
            SspAssetTransfer::BUSINESS_UNIT_ASSIGNMENTS => [
107
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer1],
108
                [SspAssetBusinessUnitAssignmentTransfer::COMPANY_BUSINESS_UNIT => $businessUnitTransfer2],
109
            ],
110
            SspAssetTransfer::SSP_MODELS => [
111
                $sspModelTransfer1->modifiedToArray(false, true),
112
                $sspModelTransfer2->modifiedToArray(false, true),
113
            ],
114
        ]);
115
        $sspAssetTransfer2 = $this->tester->haveAsset([
116
            SspAssetTransfer::SERIAL_NUMBER => '1234567890',
117
        ]);
118
119
        // Act
120
        $sspAssetWritePublisherPlugin = new SspAssetWritePublisherPlugin();
121
        $sspAssetWritePublisherPlugin->handleBulk(
122
            [
123
                (new EventEntityTransfer())->setId($sspAssetTransfer1->getIdSspAsset()),
124
                (new EventEntityTransfer())->setId($sspAssetTransfer2->getIdSspAsset()),
125
            ],
126
            null,
127
        );
128
129
        // Assert
130
        $sspAssetSearchEntities = SpySspAssetSearchQuery::create()->filterByFkSspAsset_In([
131
            $sspAssetTransfer1->getIdSspAsset(),
132
            $sspAssetTransfer2->getIdSspAsset(),
133
        ])->orderByFkSspAsset()->find();
134
135
        $this->assertCount(2, $sspAssetSearchEntities);
136
137
        $sspAssetSearchEntity1 = $sspAssetSearchEntities[0];
138
        $sspAssetSearchEntity2 = $sspAssetSearchEntities[1];
139
140
        $this->assertSame($sspAssetTransfer1->getIdSspAsset(), $sspAssetSearchEntity1->getFkSspAsset());
141
        $this->assertSame($sspAssetTransfer2->getIdSspAsset(), $sspAssetSearchEntity2->getFkSspAsset());
142
143
        $this->assertSspAssetSearchData($sspAssetSearchEntity1->getData(), $sspAssetTransfer1);
144
        $this->assertSspAssetSearchData($sspAssetSearchEntity2->getData(), $sspAssetTransfer2);
145
    }
146
147
    /**
148
     * @param array<string, mixed> $data
149
     * @param \Generated\Shared\Transfer\SspAssetTransfer $sspAssetTransfer
150
     *
151
     * @return void
152
     */
153
    protected function assertSspAssetSearchData(array $data, SspAssetTransfer $sspAssetTransfer): void
154
    {
155
        $this->assertArrayHasKey(SspAssetIndexMap::TYPE, $data);
156
        $this->assertArrayHasKey(SspAssetIndexMap::SEARCH_RESULT_DATA, $data);
157
        $this->assertArrayHasKey(SspAssetIndexMap::FULL_TEXT_BOOSTED, $data);
158
        $this->assertArrayHasKey(SspAssetIndexMap::SUGGESTION_TERMS, $data);
159
        $this->assertArrayHasKey(SspAssetIndexMap::COMPLETION_TERMS, $data);
160
        $this->assertArrayHasKey(SspAssetIndexMap::STORE, $data);
161
        $this->assertArrayHasKey(SspAssetIndexMap::BUSINESS_UNIT_IDS, $data);
162
        $this->assertArrayHasKey(SspAssetIndexMap::COMPANY_IDS, $data);
163
        $this->assertArrayHasKey(SspAssetIndexMap::ID_OWNER_BUSINESS_UNIT, $data);
164
        $this->assertArrayHasKey(SspAssetIndexMap::ID_OWNER_COMPANY_ID, $data);
165
166
        $this->assertSame(SelfServicePortalConfig::SSP_ASSET_RESOURCE_NAME, $data[SspAssetIndexMap::TYPE]);
167
        $this->assertSame([
168
            'name' => $sspAssetTransfer->getName(),
169
            'serial_number' => $sspAssetTransfer->getSerialNumber(),
170
            'model_ids' => array_map(fn (SspModelTransfer $sspModel) => $sspModel->getIdSspModel(), $sspAssetTransfer->getSspModels()->getArrayCopy()),
171
            'busines_unit_ids' => array_map(fn (SspAssetBusinessUnitAssignmentTransfer $businessUnitAssignment) => $businessUnitAssignment->getCompanyBusinessUnitOrFail()->getIdCompanyBusinessUnitOrFail(), $sspAssetTransfer->getBusinessUnitAssignments()->getArrayCopy()),
172
            'company_ids' => array_map(fn (SspAssetBusinessUnitAssignmentTransfer $businessUnitAssignment) => $businessUnitAssignment->getCompanyBusinessUnitOrFail()->getFkCompanyOrFail(), $sspAssetTransfer->getBusinessUnitAssignments()->getArrayCopy()),
173
            'id_owner_business_unit' => $sspAssetTransfer->getCompanyBusinessUnit()?->getIdCompanyBusinessUnitOrFail(),
174
            'id_owner_company_id' => $sspAssetTransfer->getCompanyBusinessUnit()?->getFkCompany(),
175
        ], $data[SspAssetIndexMap::SEARCH_RESULT_DATA]);
176
177
        $this->assertSame([static::STORE_NAME_UF, static::STORE_NAME_LO], $data[SspAssetIndexMap::STORE]);
178
179
        $assignedBusinessUnitIds = array_map(
180
            fn (SspAssetBusinessUnitAssignmentTransfer $businessUnitAssignment) => $businessUnitAssignment->getCompanyBusinessUnitOrFail()->getIdCompanyBusinessUnitOrFail(),
181
            $sspAssetTransfer->getBusinessUnitAssignments()->getArrayCopy(),
182
        );
183
184
        $assignedCompanyIds = array_map(
185
            fn (SspAssetBusinessUnitAssignmentTransfer $businessUnitAssignment) => $businessUnitAssignment->getCompanyBusinessUnitOrFail()->getFkCompany(),
186
            $sspAssetTransfer->getBusinessUnitAssignments()->getArrayCopy(),
187
        );
188
189
        $this->assertSame($assignedBusinessUnitIds, $data[SspAssetIndexMap::BUSINESS_UNIT_IDS]);
190
        $this->assertSame($assignedCompanyIds, $data[SspAssetIndexMap::COMPANY_IDS]);
191
        $this->assertSame($sspAssetTransfer->getCompanyBusinessUnit()?->getIdCompanyBusinessUnitOrFail(), $data[SspAssetIndexMap::ID_OWNER_BUSINESS_UNIT]);
192
        $this->assertSame($sspAssetTransfer->getCompanyBusinessUnit()?->getFkCompany(), $data[SspAssetIndexMap::ID_OWNER_COMPANY_ID]);
193
194
        $sspModelNames = array_map(fn (SspModelTransfer $sspModel) => $sspModel->getName(), $sspAssetTransfer->getSspModels()->getArrayCopy());
195
196
        $this->assertSame(array_filter([
197
            $sspAssetTransfer->getName(),
198
            ...$sspModelNames,
199
            $sspAssetTransfer->getSerialNumber(),
200
        ]), $data[SspAssetIndexMap::FULL_TEXT_BOOSTED]);
201
202
        $this->assertSame(array_filter([
203
            $sspAssetTransfer->getName(),
204
            ...$sspModelNames,
205
        ]), $data[SspAssetIndexMap::SUGGESTION_TERMS]);
206
207
        $this->assertSame(array_filter([
208
            $sspAssetTransfer->getName(),
209
            ...$sspModelNames,
210
        ]), $data[SspAssetIndexMap::COMPLETION_TERMS]);
211
212
        $this->assertSame($assignedBusinessUnitIds, $data[SspAssetIndexMap::BUSINESS_UNIT_IDS]);
213
        $this->assertSame($assignedCompanyIds, $data[SspAssetIndexMap::COMPANY_IDS]);
214
        $this->assertSame($sspAssetTransfer->getCompanyBusinessUnit()?->getIdCompanyBusinessUnitOrFail(), $data[SspAssetIndexMap::ID_OWNER_BUSINESS_UNIT]);
215
        $this->assertSame($sspAssetTransfer->getCompanyBusinessUnit()?->getFkCompany(), $data[SspAssetIndexMap::ID_OWNER_COMPANY_ID]);
216
    }
217
}
218