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\CategoryImage\Business; |
9
|
|
|
|
10
|
|
|
use ArrayObject; |
11
|
|
|
use Codeception\Test\Unit; |
12
|
|
|
use Generated\Shared\Transfer\CategoryImageSetTransfer; |
13
|
|
|
use Generated\Shared\Transfer\LocaleTransfer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Auto-generated group annotations |
17
|
|
|
* |
18
|
|
|
* @group SprykerTest |
19
|
|
|
* @group Zed |
20
|
|
|
* @group CategoryImage |
21
|
|
|
* @group Business |
22
|
|
|
* @group Facade |
23
|
|
|
* @group CategoryImageFacadeTest |
24
|
|
|
* Add your own group annotations below this line |
25
|
|
|
* |
26
|
|
|
* @property \SprykerTest\Zed\CategoryImage\CategoryImageBusinessTester $tester |
27
|
|
|
*/ |
28
|
|
|
class CategoryImageFacadeTest extends Unit |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
public const DEFAULT_CATEGORY_IMAGE_SET_COUNT = 1; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
public const MAX_CATEGORY_IMAGE_SET_COUNT = 5; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function testFindCategoryImagesSetCollectionByCategoryId(): void |
44
|
|
|
{ |
45
|
|
|
// Assign |
46
|
|
|
$categoryImageSetTransferCollection = $this->buildCategoryImageSetTransferCollection( |
47
|
|
|
rand(1, static::MAX_CATEGORY_IMAGE_SET_COUNT), |
48
|
|
|
); |
49
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
50
|
|
|
$categoryTransfer->setImageSets(new ArrayObject($categoryImageSetTransferCollection)); |
51
|
|
|
|
52
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
|
|
|
|
53
|
|
|
|
54
|
|
|
// Act |
55
|
|
|
$dbCategoryImageSetCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
|
|
|
|
56
|
|
|
$categoryTransfer->getIdCategory(), |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
// Assert |
60
|
|
|
$this->assertSame(count($categoryImageSetTransferCollection), count($dbCategoryImageSetCollection)); |
61
|
|
|
$this->assertEmpty(array_diff( |
62
|
|
|
$this->getIdCategoryImageSetCollection($dbCategoryImageSetCollection), |
63
|
|
|
$this->getIdCategoryImageSetCollection($categoryImageSetTransferCollection), |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function testCreateCategoryWithOneImageSet(): void |
71
|
|
|
{ |
72
|
|
|
// Assign |
73
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
74
|
|
|
$categoryImageSetCollection = $this->buildCategoryImageSetTransferCollection(static::DEFAULT_CATEGORY_IMAGE_SET_COUNT); |
75
|
|
|
$categoryTransfer->setImageSets(new ArrayObject($categoryImageSetCollection)); |
76
|
|
|
|
77
|
|
|
// Act |
78
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
79
|
|
|
$dbCategoryImageSetCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
80
|
|
|
$categoryTransfer->getIdCategory(), |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
// Assert |
84
|
|
|
$this->assertNotEmpty($dbCategoryImageSetCollection); |
85
|
|
|
$this->assertSame(static::DEFAULT_CATEGORY_IMAGE_SET_COUNT, count($dbCategoryImageSetCollection)); |
86
|
|
|
$this->assertEquals( |
87
|
|
|
reset($categoryImageSetCollection)->getIdCategoryImageSet(), |
88
|
|
|
reset($dbCategoryImageSetCollection)->getIdCategoryImageSet(), |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function testUpdateCategoryImageSetCollection(): void |
96
|
|
|
{ |
97
|
|
|
// Assign |
98
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
99
|
|
|
$categoryImage = $this->tester->buildCategoryImageTransfer(); |
100
|
|
|
$categoryImageSet = $this->tester->buildCategoryImageSetTransfer(); |
101
|
|
|
$categoryImageSet->setCategoryImages(new ArrayObject([$categoryImage])); |
102
|
|
|
$categoryTransfer->addImageSet($categoryImageSet); |
103
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
104
|
|
|
|
105
|
|
|
// Act |
106
|
|
|
$this->getFacade()->updateCategoryImageSetsForCategory($categoryTransfer); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$dbCategoryImageSetCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
109
|
|
|
$categoryTransfer->getIdCategory(), |
110
|
|
|
); |
111
|
|
|
$dbCategoryImageSet = $dbCategoryImageSetCollection[0]; |
112
|
|
|
|
113
|
|
|
// Assert |
114
|
|
|
$this->assertEquals($categoryImageSet->getName(), $dbCategoryImageSet->getName()); |
115
|
|
|
$this->assertEquals($categoryImage->getExternalUrlSmall(), $dbCategoryImageSet->getCategoryImages()[0]->getExternalUrlSmall()); |
116
|
|
|
$this->assertEquals($categoryImage->getExternalUrlLarge(), $dbCategoryImageSet->getCategoryImages()[0]->getExternalUrlLarge()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function testDeleteRandomImageSetForCategory(): void |
123
|
|
|
{ |
124
|
|
|
// Assign |
125
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
126
|
|
|
$categoryImageSetCollection = $this->buildCategoryImageSetTransferCollection(rand(2, static::MAX_CATEGORY_IMAGE_SET_COUNT)); |
127
|
|
|
$categoryTransfer->setImageSets(new ArrayObject($categoryImageSetCollection)); |
128
|
|
|
|
129
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
130
|
|
|
|
131
|
|
|
// Act |
132
|
|
|
$dbCategoryImageSetCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
133
|
|
|
$categoryTransfer->getIdCategory(), |
134
|
|
|
); |
135
|
|
|
$randomImageSetKeyToBeDeleted = array_rand($categoryImageSetCollection); |
136
|
|
|
$idCategoryImageSetToBeDeleted = $categoryImageSetCollection[$randomImageSetKeyToBeDeleted]->getIdCategoryImageSet(); |
137
|
|
|
$this->assertTrue(in_array( |
138
|
|
|
$idCategoryImageSetToBeDeleted, |
139
|
|
|
$this->getIdCategoryImageSetCollection($dbCategoryImageSetCollection), |
140
|
|
|
)); |
141
|
|
|
unset($categoryImageSetCollection[$randomImageSetKeyToBeDeleted]); |
142
|
|
|
$categoryTransfer->setImageSets(new ArrayObject($categoryImageSetCollection)); |
143
|
|
|
$this->getFacade()->updateCategoryImageSetsForCategory($categoryTransfer); |
144
|
|
|
$dbCategoryImageSetCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
145
|
|
|
$categoryTransfer->getIdCategory(), |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
// Assert |
149
|
|
|
$this->assertFalse(in_array( |
150
|
|
|
$idCategoryImageSetToBeDeleted, |
151
|
|
|
$this->getIdCategoryImageSetCollection($dbCategoryImageSetCollection), |
152
|
|
|
)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function testDeleteImageSetsByCategoryId(): void |
159
|
|
|
{ |
160
|
|
|
// Assign |
161
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
162
|
|
|
$categoryImageSetCollection = $this->buildCategoryImageSetTransferCollection(rand(2, static::MAX_CATEGORY_IMAGE_SET_COUNT)); |
163
|
|
|
$categoryTransfer->setImageSets(new ArrayObject($categoryImageSetCollection)); |
164
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
165
|
|
|
|
166
|
|
|
// Act |
167
|
|
|
$this->getFacade()->deleteCategoryImageSetsByIdCategory($categoryTransfer->getIdCategory()); |
|
|
|
|
168
|
|
|
|
169
|
|
|
// Assert |
170
|
|
|
$dbCategoryImageSetCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
171
|
|
|
$categoryTransfer->getIdCategory(), |
172
|
|
|
); |
173
|
|
|
$this->assertEmpty($dbCategoryImageSetCollection); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function testExpandCategoryWithImageSets(): void |
180
|
|
|
{ |
181
|
|
|
// Assign |
182
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
183
|
|
|
$categoryImageSetCollection = $this->buildCategoryImageSetTransferCollection( |
184
|
|
|
rand(1, static::MAX_CATEGORY_IMAGE_SET_COUNT), |
185
|
|
|
); |
186
|
|
|
$categoryTransfer->setImageSets(new ArrayObject($categoryImageSetCollection)); |
187
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
188
|
|
|
$categoryTransfer->setImageSets(new ArrayObject()); |
189
|
|
|
|
190
|
|
|
// Act |
191
|
|
|
$this->getFacade()->expandCategoryWithImageSets($categoryTransfer); |
|
|
|
|
192
|
|
|
$dbCategoryImageSetCollection = $categoryTransfer->getImageSets()->getArrayCopy(); |
193
|
|
|
|
194
|
|
|
// Assert |
195
|
|
|
$this->assertSame(count($categoryImageSetCollection), count($dbCategoryImageSetCollection)); |
196
|
|
|
$this->assertEmpty(array_diff( |
197
|
|
|
$this->getIdCategoryImageSetCollection($dbCategoryImageSetCollection), |
198
|
|
|
$this->getIdCategoryImageSetCollection($categoryImageSetCollection), |
199
|
|
|
)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function testGetCategoryImageSetsByIdCategorySortsImagesBySortOrderAsc(): void |
206
|
|
|
{ |
207
|
|
|
// Assign |
208
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
209
|
|
|
$categoryImageSetTransfer = $this->tester->createCategoryImageSetWithOrderedImages([3, 1, 0, 2]); |
210
|
|
|
$categoryTransfer->setImageSets(new ArrayObject([$categoryImageSetTransfer])); |
211
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
212
|
|
|
|
213
|
|
|
// Act |
214
|
|
|
$categoryImageCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
215
|
|
|
$categoryTransfer->getIdCategory(), |
216
|
|
|
)[0]->getCategoryImages(); |
217
|
|
|
|
218
|
|
|
$sortOrder = 0; |
219
|
|
|
foreach ($categoryImageCollection as $categoryImageTransfer) { |
220
|
|
|
$this->assertTrue($categoryImageTransfer->getSortOrder() >= $sortOrder); |
221
|
|
|
$sortOrder = $categoryImageTransfer->getSortOrder(); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
public function testGetCategoryImageSetsByIdCategorySortsImagesByIdCategoryImageSetToCategoryImageAsc(): void |
229
|
|
|
{ |
230
|
|
|
// Assign |
231
|
|
|
$categoryTransfer = $this->tester->haveCategory(); |
232
|
|
|
$categoryImageSetTransfer = $this->tester->createCategoryImageSetWithOrderedImages([0, 0, 0]); |
233
|
|
|
$categoryTransfer->setImageSets(new ArrayObject([$categoryImageSetTransfer])); |
234
|
|
|
$this->getFacade()->createCategoryImageSetsForCategory($categoryTransfer); |
235
|
|
|
|
236
|
|
|
// Act |
237
|
|
|
$categoryImageCollection = $this->getFacade()->getCategoryImageSetsByIdCategory( |
238
|
|
|
$categoryTransfer->getIdCategory(), |
239
|
|
|
)[0]->getCategoryImages(); |
240
|
|
|
|
241
|
|
|
$idCategoryImageSetToCategoryImage = 0; |
242
|
|
|
foreach ($categoryImageCollection as $categoryImageTransfer) { |
243
|
|
|
$this->assertTrue( |
244
|
|
|
$categoryImageTransfer->getIdCategoryImageSetToCategoryImage() > $idCategoryImageSetToCategoryImage, |
245
|
|
|
); |
246
|
|
|
$idCategoryImageSetToCategoryImage = $categoryImageTransfer->getIdCategoryImageSetToCategoryImage(); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param int $size |
252
|
|
|
* |
253
|
|
|
* @return array<\Generated\Shared\Transfer\CategoryImageSetTransfer|\Spryker\Shared\Kernel\Transfer\AbstractTransfer> |
254
|
|
|
*/ |
255
|
|
|
protected function buildCategoryImageSetTransferCollection(int $size): array |
256
|
|
|
{ |
257
|
|
|
$categoryImageTransferCollection = []; |
258
|
|
|
for ($i = 1; $i <= $size; ++$i) { |
259
|
|
|
$categoryImageTransferCollection[] = $this->tester->buildCategoryImageSetTransfer(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $categoryImageTransferCollection; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param array $seedData |
267
|
|
|
* |
268
|
|
|
* @return \Generated\Shared\Transfer\LocaleTransfer |
269
|
|
|
*/ |
270
|
|
|
protected function haveLocale(array $seedData = []): LocaleTransfer |
271
|
|
|
{ |
272
|
|
|
return $this->tester->haveLocale($seedData); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return \Spryker\Zed\CategoryImage\Business\CategoryImageFacadeInterface|\Spryker\Zed\Kernel\Business\AbstractFacade |
277
|
|
|
*/ |
278
|
|
|
protected function getFacade() |
279
|
|
|
{ |
280
|
|
|
return $this->tester->getFacade(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param array<\Generated\Shared\Transfer\CategoryImageSetTransfer> $categoryImageSetCollection |
285
|
|
|
* |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
protected function getIdCategoryImageSetCollection(array $categoryImageSetCollection): array |
289
|
|
|
{ |
290
|
|
|
return array_map(function (CategoryImageSetTransfer $categoryImageSetTransfer) { |
291
|
|
|
return $categoryImageSetTransfer->getIdCategoryImageSet(); |
292
|
|
|
}, $categoryImageSetCollection); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|