Issues (3412)

ProductOfferShipmentTypeDataImportPluginTest.php (1 issue)

1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerTest\Zed\ProductOfferShipmentTypeDataImport\Communication\Plugin\DataImport;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\Transfer\DataImporterConfigurationTransfer;
12
use Generated\Shared\Transfer\DataImporterReaderConfigurationTransfer;
13
use Generated\Shared\Transfer\ProductConcreteTransfer;
14
use Generated\Shared\Transfer\ProductOfferTransfer;
15
use Generated\Shared\Transfer\ShipmentTypeTransfer;
16
use SprykerTest\Zed\ProductOfferShipmentTypeDataImport\ProductOfferShipmentTypeDataImportCommunicationTester;
17
18
/**
19
 * Auto-generated group annotations
20
 *
21
 * @group SprykerTest
22
 * @group Zed
23
 * @group ProductOfferShipmentTypeDataImport
24
 * @group Communication
25
 * @group Plugin
26
 * @group DataImport
27
 * @group ProductOfferShipmentTypeDataImportPluginTest
28
 * Add your own group annotations below this line
29
 */
30
class ProductOfferShipmentTypeDataImportPluginTest extends Unit
31
{
32
    /**
33
     * @var string
34
     */
35
    protected const SHIPMENT_TYPE_KEY_1 = 'shipment_type_key_1';
36
37
    /**
38
     * @var string
39
     */
40
    protected const SHIPMENT_TYPE_KEY_2 = 'shipment_type_key_2';
41
42
    /**
43
     * @var string
44
     */
45
    protected const NOT_EXISTING_SHIPMENT_TYPE_KEY = 'not_existing_shipment_type_key';
46
47
    /**
48
     * @var string
49
     */
50
    protected const PRODUCT_OFFER_REFERENCE_1 = 'product_offer_reference_1';
51
52
    /**
53
     * @var string
54
     */
55
    protected const PRODUCT_OFFER_REFERENCE_2 = 'product_offer_reference_2';
56
57
    /**
58
     * @var string
59
     */
60
    protected const NOT_EXISTING_PRODUCT_OFFER_REFERENCE = 'not_existing_product_offer_reference';
61
62
    /**
63
     * @uses \Spryker\Zed\ProductOfferShipmentTypeDataImport\ProductOfferShipmentTypeDataImportConfig::IMPORT_TYPE_PRODUCT_OFFER_SHIPMENT_TYPE
64
     *
65
     * @var string
66
     */
67
    protected const IMPORT_TYPE_PRODUCT_OFFER_SHIPMENT_TYPE = 'product-offer-shipment-type';
68
69
    /**
70
     * @var \SprykerTest\Zed\ProductOfferShipmentTypeDataImport\ProductOfferShipmentTypeDataImportCommunicationTester
71
     */
72
    protected ProductOfferShipmentTypeDataImportCommunicationTester $tester;
73
74
    /**
75
     * @var \Generated\Shared\Transfer\ProductConcreteTransfer
76
     */
77
    protected ProductConcreteTransfer $productConcreteTransfer;
78
79
    /**
80
     * @return void
81
     */
82
    protected function setUp(): void
83
    {
84
        parent::setUp();
85
86
        $this->tester->cleanUpData();
87
        $this->productConcreteTransfer = $this->tester->haveProduct();
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    protected function tearDown(): void
94
    {
95
        parent::tearDown();
96
97
        $this->tester->cleanUpStaticCacheData();
98
        $this->tester->cleanUpData();
99
    }
100
101
    /**
102
     * @return void
103
     */
104
    public function testImportImportsValidProductOfferShipmentTypeDataSet(): void
105
    {
106
        // Arrange
107
        $shipmentTypeTransfer1 = $this->tester->haveShipmentType([
108
                ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_1,
109
            ]);
110
        $shipmentTypeTransfer2 = $this->tester->haveShipmentType([
111
                ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_2,
112
            ]);
113
        $this->tester->haveProductOffer([
114
                ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_1,
115
                ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
116
            ]);
117
118
        $this->tester->haveProductOffer([
119
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_2,
120
        ]);
121
122
        $dataImportConfigurationTransfer = $this->createDataImporterConfigurationTransfer(
123
            'valid_data_set.csv',
124
        );
125
126
        // Act
127
        $dataImporterReportTransfer = $this->tester->createProductOfferShipmentTypeDataImportPlugin()
128
            ->import($dataImportConfigurationTransfer);
129
130
        $productOfferShipmentTypeEntitiesCount = $this->tester->getProductOfferShipmentTypeEntitiesByShipmentTypeKeysCount([
131
            $shipmentTypeTransfer1->getKey(),
132
            $shipmentTypeTransfer2->getKey(),
133
        ]);
134
135
        // Assert
136
        $this->assertTrue($dataImporterReportTransfer->getIsSuccess());
137
        $this->assertSame(4, $dataImporterReportTransfer->getImportedDataSetCount());
138
        $this->assertSame(4, $productOfferShipmentTypeEntitiesCount);
139
    }
140
141
    /**
142
     * @group test
143
     *
144
     * @return void
145
     */
146
    public function testImportDoesntImportDataWithAnEmptyProductOfferReference(): void
147
    {
148
        // Arrange
149
        $shipmentTypeTransfer1 = $this->tester->haveShipmentType([
150
            ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_1,
151
        ]);
152
        $shipmentTypeTransfer2 = $this->tester->haveShipmentType([
153
            ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_2,
154
        ]);
155
        $this->tester->haveProductOffer([
156
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_1,
157
            ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
158
        ]);
159
        $dataImportConfigurationTransfer = $this->createDataImporterConfigurationTransfer(
160
            'empty_product_offer_reference.csv',
161
        );
162
163
        // Act
164
        $dataImporterReportTransfer = $this->tester->createProductOfferShipmentTypeDataImportPlugin()
165
            ->import($dataImportConfigurationTransfer);
166
167
        $productOfferShipmentTypeEntitiesCount = $this->tester->getProductOfferShipmentTypeEntitiesByShipmentTypeKeysCount([
168
            $shipmentTypeTransfer1->getKey(),
169
            $shipmentTypeTransfer2->getKey(),
170
        ]);
171
172
        // Assert
173
        $this->assertFalse($dataImporterReportTransfer->getIsSuccess());
174
        $this->assertSame(2, $dataImporterReportTransfer->getExpectedImportableDataSetCount());
175
        $this->assertSame(1, $dataImporterReportTransfer->getImportedDataSetCount());
176
        $this->assertSame(1, $productOfferShipmentTypeEntitiesCount);
177
        $this->assertCount(1, $dataImporterReportTransfer->getMessages());
178
        $this->assertStringContainsString(
179
            'Missing required "product_offer_reference" field.',
180
            $dataImporterReportTransfer->getMessages()->getIterator()->current()->getMessage(),
181
        );
182
    }
183
184
    /**
185
     * @return void
186
     */
187
    public function testImportDoesntImportDataWithAnEmptyShipmentTypeKey(): void
188
    {
189
        // Arrange
190
        $shipmentTypeTransfer = $this->tester->haveShipmentType([
191
            ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_1,
192
        ]);
193
        $this->tester->haveProductOffer([
194
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_1,
195
            ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
196
        ]);
197
        $this->tester->haveProductOffer([
198
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_2,
199
            ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
200
        ]);
201
        $dataImportConfigurationTransfer = $this->createDataImporterConfigurationTransfer(
202
            'empty_shipment_type_key.csv',
203
        );
204
205
        // Act
206
        $dataImporterReportTransfer = $this->tester->createProductOfferShipmentTypeDataImportPlugin()
207
            ->import($dataImportConfigurationTransfer);
208
209
        $productOfferShipmentTypeEntitiesCount = $this->tester->getProductOfferShipmentTypeEntitiesByShipmentTypeKeysCount([
210
            $shipmentTypeTransfer->getKey(),
211
        ]);
212
213
        // Assert
214
        $this->assertFalse($dataImporterReportTransfer->getIsSuccess());
215
        $this->assertSame(2, $dataImporterReportTransfer->getExpectedImportableDataSetCount());
216
        $this->assertSame(1, $dataImporterReportTransfer->getImportedDataSetCount());
217
        $this->assertSame(1, $productOfferShipmentTypeEntitiesCount);
218
        $this->assertCount(1, $dataImporterReportTransfer->getMessages());
219
        $this->assertStringContainsString(
220
            'Missing required "shipment_type_key" field.',
221
            $dataImporterReportTransfer->getMessages()->getIterator()->current()->getMessage(),
222
        );
223
    }
224
225
    /**
226
     * @return void
227
     */
228
    public function testImportDoesntImportDataWithNotExistingProductOfferReference(): void
229
    {
230
        // Arrange
231
        $shipmentTypeTransfer1 = $this->tester->haveShipmentType([
232
            ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_1,
233
        ]);
234
        $shipmentTypeTransfer2 = $this->tester->haveShipmentType([
235
            ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_2,
236
        ]);
237
238
        $this->tester->haveProductOffer([
239
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_1,
240
            ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
241
        ]);
242
        $dataImportConfigurationTransfer = $this->createDataImporterConfigurationTransfer(
243
            'not_existing_product_offer_reference.csv',
244
        );
245
246
        // Act
247
        $dataImporterReportTransfer = $this->tester->createProductOfferShipmentTypeDataImportPlugin()
248
            ->import($dataImportConfigurationTransfer);
249
250
        $productOfferShipmentTypeEntitiesCount = $this->tester->getProductOfferShipmentTypeEntitiesByShipmentTypeKeysCount([
251
            $shipmentTypeTransfer1->getKey(),
252
            $shipmentTypeTransfer2->getKey(),
253
        ]);
254
255
        // Assert
256
        $this->assertFalse($dataImporterReportTransfer->getIsSuccess());
257
        $this->assertSame(2, $dataImporterReportTransfer->getExpectedImportableDataSetCount());
258
        $this->assertSame(1, $dataImporterReportTransfer->getImportedDataSetCount());
259
        $this->assertSame(1, $productOfferShipmentTypeEntitiesCount);
260
        $this->assertCount(1, $dataImporterReportTransfer->getMessages());
261
        $this->assertStringContainsString(
262
            'Could not find product offer by product offer reference "' . static::NOT_EXISTING_PRODUCT_OFFER_REFERENCE . '"',
263
            $dataImporterReportTransfer->getMessages()->getIterator()->current()->getMessage(),
264
        );
265
    }
266
267
    /**
268
     * @return void
269
     */
270
    public function testImportDoesntImportDataWithNotExistingShipmentTypeKey(): void
271
    {
272
        // Arrange
273
        $shipmentTypeTransfer1 = $this->tester->haveShipmentType([
274
            ShipmentTypeTransfer::KEY => static::SHIPMENT_TYPE_KEY_1,
275
        ]);
276
        $this->tester->haveProductOffer([
277
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_1,
278
            ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
279
        ]);
280
        $this->tester->haveProductOffer([
281
            ProductOfferTransfer::PRODUCT_OFFER_REFERENCE => static::PRODUCT_OFFER_REFERENCE_2,
282
            ProductOfferTransfer::CONCRETE_SKU => $this->productConcreteTransfer->getSku(),
283
        ]);
284
        $dataImportConfigurationTransfer = $this->createDataImporterConfigurationTransfer(
285
            'not_existing_shipment_type_key.csv',
286
        );
287
288
        // Act
289
        $dataImporterReportTransfer = $this->tester->createProductOfferShipmentTypeDataImportPlugin()
290
            ->import($dataImportConfigurationTransfer);
291
292
        $productOfferShipmentTypeEntitiesCount = $this->tester->getProductOfferShipmentTypeEntitiesByShipmentTypeKeysCount([
293
            static::NOT_EXISTING_SHIPMENT_TYPE_KEY,
294
            $shipmentTypeTransfer1->getKey(),
295
        ]);
296
297
        // Assert
298
        $this->assertFalse($dataImporterReportTransfer->getIsSuccess());
299
        $this->assertSame(2, $dataImporterReportTransfer->getExpectedImportableDataSetCount());
300
        $this->assertSame(1, $dataImporterReportTransfer->getImportedDataSetCount());
301
        $this->assertSame(1, $productOfferShipmentTypeEntitiesCount);
302
        $this->assertCount(1, $dataImporterReportTransfer->getMessages());
303
        $this->assertStringContainsString(
304
            'Could not find shipment type by key "' . static::NOT_EXISTING_SHIPMENT_TYPE_KEY . '"',
305
            $dataImporterReportTransfer->getMessages()->getIterator()->current()->getMessage(),
306
        );
307
    }
308
309
    /**
310
     * @return void
311
     */
312
    public function testGetImportTypeReturnsExpectedType(): void
313
    {
314
        // Act
315
        $importType = $this->tester->createProductOfferShipmentTypeDataImportPlugin()->getImportType();
316
317
        // Assert
318
        $this->assertSame(static::IMPORT_TYPE_PRODUCT_OFFER_SHIPMENT_TYPE, $importType);
319
    }
320
321
    /**
322
     * @param string $importFileName
323
     *
324
     * @return \Generated\Shared\Transfer\DataImporterConfigurationTransfer
325
     */
326
    protected function createDataImporterConfigurationTransfer(string $importFileName): DataImporterConfigurationTransfer
327
    {
328
        return (new DataImporterConfigurationTransfer())
329
            ->setReaderConfiguration(
330
                (new DataImporterReaderConfigurationTransfer())
331
                    ->setFileName(codecept_data_dir() . 'import/product-offer-shipment-type/' . $importFileName),
0 ignored issues
show
The function codecept_data_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

331
                    ->setFileName(/** @scrutinizer ignore-call */ codecept_data_dir() . 'import/product-offer-shipment-type/' . $importFileName),
Loading history...
332
            );
333
    }
334
}
335