Issues (3641)

Plugin/ContentProductSetDataImportPluginTest.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\ContentProductSetDataImport\Communication\Plugin;
9
10
use Codeception\Test\Unit;
11
use Generated\Shared\Transfer\DataImporterConfigurationTransfer;
12
use Generated\Shared\Transfer\DataImporterReaderConfigurationTransfer;
13
use Generated\Shared\Transfer\DataImporterReportTransfer;
14
use Orm\Zed\ProductSet\Persistence\SpyProductSet;
15
use Orm\Zed\ProductSet\Persistence\SpyProductSetQuery;
16
use Spryker\Zed\ContentProductSetDataImport\Communication\Plugin\ContentProductSetDataImportPlugin;
17
use Spryker\Zed\ContentProductSetDataImport\ContentProductSetDataImportConfig;
18
use Spryker\Zed\ContentProductSetDataImport\ContentProductSetDataImportDependencyProvider;
19
use Spryker\Zed\DataImport\Business\Exception\DataImportException;
20
21
/**
22
 * Auto-generated group annotations
23
 *
24
 * @group SprykerTest
25
 * @group Zed
26
 * @group ContentProductSetDataImport
27
 * @group Communication
28
 * @group Plugin
29
 * @group ContentProductSetDataImportPluginTest
30
 * Add your own group annotations below this line
31
 */
32
class ContentProductSetDataImportPluginTest extends Unit
33
{
34
    /**
35
     * @var string
36
     */
37
    protected const ERROR_MESSAGE_PRODUCT_SET_WRONG_KEY = 'Please check "product_set_key.default" in the row with key: "ps-1". The wrong product set key passed.';
38
39
    /**
40
     * @var string
41
     */
42
    protected const KEY_ID_PRODUCT_SET = 'id_product_set';
43
44
    /**
45
     * @var \SprykerTest\Zed\ContentProductSetDataImport\ContentProductSetDataImportCommunicationTester
46
     */
47
    protected $tester;
48
49
    /**
50
     * @return void
51
     */
52
    protected function _before(): void
53
    {
54
        parent::_before();
55
        $this->tester->ensureDatabaseTableIsEmpty();
56
    }
57
58
    /**
59
     * @return void
60
     */
61
    public function testImportProductSetData(): void
62
    {
63
        // Arrange
64
        $dataImportConfigurationTransfer = $this->createConfigurationTransfer(
65
            'import/content_product_set.csv',
66
        );
67
68
        // Act
69
        $dataImporterReportTransfer = (new ContentProductSetDataImportPlugin())->import($dataImportConfigurationTransfer);
70
71
        // Assert
72
        $this->assertInstanceOf(DataImporterReportTransfer::class, $dataImporterReportTransfer);
73
74
        $this->tester->assertDatabaseTableContainsData('ps-1');
75
        $this->tester->assertDatabaseTableContainsData('ps-2');
76
        $this->tester->assertDatabaseTableContainsData('ps-3');
77
    }
78
79
    /**
80
     * @return void
81
     */
82
    public function testImportProductSetDataWithWrongProductSetKeyFails(): void
83
    {
84
        // Arrange
85
        $dataImportConfigurationTransfer = $this->createConfigurationTransfer(
86
            'import/content_product_set_wrong_key.csv',
87
        )->setThrowException(true);
88
89
        // Assert
90
        $this->expectExceptionObject(new DataImportException(static::ERROR_MESSAGE_PRODUCT_SET_WRONG_KEY));
91
92
        // Act
93
        (new ContentProductSetDataImportPlugin())->import($dataImportConfigurationTransfer);
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function testGetImportTypeReturnsTypeOfImporter(): void
100
    {
101
        // Arrange
102
        $contentProductSetDataImportPlugin = new ContentProductSetDataImportPlugin();
103
104
        // Assert
105
        $this->assertSame(ContentProductSetDataImportConfig::IMPORT_TYPE_CONTENT_PRODUCT_SET, $contentProductSetDataImportPlugin->getImportType());
106
    }
107
108
    /**
109
     * @return void
110
     */
111
    public function testUpdateLocale(): void
112
    {
113
        // Arrange
114
        $this->setProductSetQueryReturn(2);
115
        $dataImportConfigurationTransfer = $this->createConfigurationTransfer(
116
            'import/content_product_set(update).csv',
117
        );
118
119
        // Act
120
        $dataImporterReportTransfer = (new ContentProductSetDataImportPlugin())->import($dataImportConfigurationTransfer);
121
122
        // Assert
123
        $this->assertInstanceOf(DataImporterReportTransfer::class, $dataImporterReportTransfer);
124
        $this->assertTrue($dataImporterReportTransfer->getIsSuccess());
125
126
        $this->tester->assertContentLocalizedHasSetId(66, [static::KEY_ID_PRODUCT_SET => 2]);
127
        $this->tester->assertContentLocalizedHasSetId(46, [static::KEY_ID_PRODUCT_SET => 2]);
128
    }
129
130
    /**
131
     * @return void
132
     */
133
    public function testUpdateLocaleFromDefault(): void
134
    {
135
        // Arrange
136
        $this->setProductSetQueryReturn(1);
137
        $dataImportConfigurationTransfer = $this->createConfigurationTransfer(
138
            'import/content_product_set(update_locale_from_default).csv',
139
        );
140
141
        // Act
142
        $dataImporterReportTransfer = (new ContentProductSetDataImportPlugin())->import($dataImportConfigurationTransfer);
143
144
        // Assert
145
        $this->assertInstanceOf(DataImporterReportTransfer::class, $dataImporterReportTransfer);
146
        $this->assertTrue($dataImporterReportTransfer->getIsSuccess());
147
148
        $this->tester->assertContentLocalizedHasSetId(66, [static::KEY_ID_PRODUCT_SET => 1]);
149
        $this->tester->assertContentLocalizedHasSetId(46, [static::KEY_ID_PRODUCT_SET => 1]);
150
    }
151
152
    /**
153
     * @return void
154
     */
155
    public function testUpdateLocaleToDefault(): void
156
    {
157
        // Arrange
158
        $this->setProductSetQueryReturn(3);
159
        $dataImportConfigurationTransfer = $this->createConfigurationTransfer(
160
            'import/content_product_set(update_locale_to_default).csv',
161
        );
162
163
        // Act
164
        $dataImporterReportTransfer = (new ContentProductSetDataImportPlugin())->import($dataImportConfigurationTransfer);
165
166
        // Assert
167
        $this->assertInstanceOf(DataImporterReportTransfer::class, $dataImporterReportTransfer);
168
        $this->assertTrue($dataImporterReportTransfer->getIsSuccess());
169
170
        $this->tester->assertContentLocalizedHasSetId(66, [static::KEY_ID_PRODUCT_SET => 3]);
171
        $this->tester->assertContentLocalizedDoesNotExist(46);
172
    }
173
174
    /**
175
     * @param string $importFilePath
176
     *
177
     * @return \Generated\Shared\Transfer\DataImporterConfigurationTransfer
178
     */
179
    protected function createConfigurationTransfer(string $importFilePath): DataImporterConfigurationTransfer
180
    {
181
        // Arrange
182
        $dataImporterReaderConfigurationTransfer = new DataImporterReaderConfigurationTransfer();
183
        $dataImporterReaderConfigurationTransfer->setFileName(codecept_data_dir() . $importFilePath);
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

183
        $dataImporterReaderConfigurationTransfer->setFileName(/** @scrutinizer ignore-call */ codecept_data_dir() . $importFilePath);
Loading history...
184
185
        $dataImportConfigurationTransfer = new DataImporterConfigurationTransfer();
186
187
        return $dataImportConfigurationTransfer->setReaderConfiguration($dataImporterReaderConfigurationTransfer);
188
    }
189
190
    /**
191
     * @param int $idProductSet
192
     *
193
     * @return void
194
     */
195
    protected function setProductSetQueryReturn(int $idProductSet): void
196
    {
197
        $productSetQueryMock = $this->getMockBuilder(SpyProductSetQuery::class)
198
            ->setMethods(['findOneByProductSetKey'])
199
            ->getMock();
200
201
        $productSetQueryMock->method('findOneByProductSetKey')
202
            ->willReturn((new SpyProductSet())->setIdProductSet($idProductSet));
203
204
        $this->tester->setDependency(ContentProductSetDataImportDependencyProvider::PROPEL_QUERY_PRODUCT_SET, $productSetQueryMock);
205
    }
206
}
207