44 $this->productRepositoryMock = $this->createMock(\
Magento\Catalog\Api\ProductRepositoryInterface::class);
45 $this->contentValidatorMock = $this->createMock(\
Magento\Framework\Api\ImageContentValidatorInterface::class);
46 $this->productMock = $this->createPartialMock(\
Magento\Catalog\Model\Product::class, [
52 'getMediaGalleryEntries',
53 'setMediaGalleryEntries',
55 $this->mediaGalleryEntryMock =
56 $this->createMock(\
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
57 $this->model = new \Magento\Catalog\Model\Product\Gallery\GalleryManagement(
58 $this->productRepositoryMock,
59 $this->contentValidatorMock
61 $this->attributeValueMock = $this->getMockBuilder(\
Magento\Framework\Api\AttributeValue::class)
62 ->disableOriginalConstructor()
72 $entryContentMock = $this->getMockBuilder(\
Magento\Framework\Api\Data\ImageContentInterface::class)
73 ->disableOriginalConstructor()
75 $this->mediaGalleryEntryMock->expects($this->any())->method(
'getContent')->willReturn($entryContentMock);
77 $this->contentValidatorMock->expects($this->once())->method(
'isValid')->with($entryContentMock)
80 $this->model->create(
"sku", $this->mediaGalleryEntryMock);
89 $productSku =
'mediaProduct';
90 $entryContentMock = $this->getMockBuilder(\
Magento\Framework\Api\Data\ImageContentInterface::class)
91 ->disableOriginalConstructor()
93 $this->mediaGalleryEntryMock->expects($this->any())->method(
'getContent')->willReturn($entryContentMock);
94 $this->productRepositoryMock->expects($this->once())
97 ->willReturn($this->productMock);
99 $this->contentValidatorMock->expects($this->once())->method(
'isValid')->with($entryContentMock)
102 $this->productRepositoryMock->expects($this->once())->method(
'save')->with($this->productMock)
104 $this->model->create($productSku, $this->mediaGalleryEntryMock);
109 $productSku =
'mediaProduct';
110 $entryContentMock = $this->createMock(
111 \
Magento\Framework\Api\Data\ImageContentInterface::class
113 $this->mediaGalleryEntryMock->expects($this->any())->method(
'getContent')->willReturn($entryContentMock);
115 $this->productRepositoryMock->expects($this->once())
118 ->willReturn($this->productMock);
119 $this->productRepositoryMock->expects($this->once())
121 ->with($this->productMock)
122 ->willReturn($this->productMock);
124 $this->contentValidatorMock->expects($this->once())->method(
'isValid')->with($entryContentMock)
127 $newEntryMock = $this->createMock(\
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
128 $newEntryMock->expects($this->exactly(2))->method(
'getId')->willReturn(42);
129 $this->productMock->expects($this->at(2))->method(
'getMediaGalleryEntries')
130 ->willReturn([$newEntryMock]);
131 $this->productMock->expects($this->once())->method(
'setMediaGalleryEntries')
132 ->with([$this->mediaGalleryEntryMock]);
134 $this->assertEquals(42, $this->model->create($productSku, $this->mediaGalleryEntryMock));
143 $productSku =
'testProduct';
144 $entryMock = $this->createMock(\
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
146 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
147 ->willReturn($this->productMock);
148 $existingEntryMock = $this->createMock(
149 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
151 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn(43);
152 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
153 ->willReturn([$existingEntryMock]);
154 $entryMock->expects($this->once())->method(
'getId')->willReturn($entryId);
155 $this->model->update($productSku, $entryMock);
164 $productSku =
'testProduct';
165 $entryMock = $this->createMock(\
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
167 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
168 ->willReturn($this->productMock);
169 $existingEntryMock = $this->createMock(
170 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
172 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn($entryId);
173 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
174 ->willReturn([$existingEntryMock]);
175 $entryMock->expects($this->once())->method(
'getId')->willReturn($entryId);
176 $this->productRepositoryMock->expects($this->once())->method(
'save')->with($this->productMock)
178 $this->model->update($productSku, $entryMock);
183 $productSku =
'testProduct';
184 $entryMock = $this->createMock(\
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
187 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
188 ->willReturn($this->productMock);
189 $existingEntryMock = $this->createMock(
190 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
192 $existingSecondEntryMock = $this->createMock(
193 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
196 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn($entryId);
197 $existingEntryMock->expects($this->once())->method(
'getTypes')->willReturn([
'small_image']);
198 $existingEntryMock->expects($this->once())->method(
'setTypes')->with([
'small_image']);
199 $existingSecondEntryMock->expects($this->once())->method(
'getId')->willReturn($entrySecondId);
200 $existingSecondEntryMock->expects($this->once())->method(
'getTypes')->willReturn([
'image']);
201 $existingSecondEntryMock->expects($this->once())->method(
'setTypes')->with([]);
202 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
203 ->willReturn([$existingEntryMock, $existingSecondEntryMock]);
205 $entryMock->expects($this->exactly(2))->method(
'getId')->willReturn($entryId);
206 $entryMock->expects($this->once())->method(
'getFile')->willReturn(
"base64");
207 $entryMock->expects($this->once())->method(
'setId')->with(
null);
208 $entryMock->expects($this->exactly(2))->method(
'getTypes')->willReturn([
'image']);
210 $this->productMock->expects($this->once())->method(
'setMediaGalleryEntries')
211 ->with([$entryMock, $existingSecondEntryMock])
213 $this->productRepositoryMock->expects($this->once())->method(
'save')->with($this->productMock);
214 $this->assertTrue($this->model->update($productSku, $entryMock));
223 $productSku =
'testProduct';
225 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
226 ->willReturn($this->productMock);
227 $existingEntryMock = $this->createMock(
228 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
230 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn(43);
231 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
232 ->willReturn([$existingEntryMock]);
233 $this->model->remove($productSku, $entryId);
238 $productSku =
'testProduct';
240 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
241 ->willReturn($this->productMock);
242 $existingEntryMock = $this->createMock(
243 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
245 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn(42);
246 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
247 ->willReturn([$existingEntryMock]);
248 $this->productMock->expects($this->once())->method(
'setMediaGalleryEntries')
250 $this->productRepositoryMock->expects($this->once())->method(
'save')->with($this->productMock);
251 $this->assertTrue($this->model->remove($productSku, $entryId));
260 $productSku =
'testProduct';
262 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
264 $this->model->get($productSku, $imageId);
273 $productSku =
'testProduct';
275 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
276 ->willReturn($this->productMock);
277 $existingEntryMock = $this->createMock(
278 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
280 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn(44);
281 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
282 ->willReturn([$existingEntryMock]);
283 $this->model->get($productSku, $imageId);
288 $productSku =
'testProduct';
290 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
291 ->willReturn($this->productMock);
292 $existingEntryMock = $this->createMock(
293 \
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
295 $existingEntryMock->expects($this->once())->method(
'getId')->willReturn(42);
296 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
297 ->willReturn([$existingEntryMock]);
298 $this->assertEquals($existingEntryMock, $this->model->get($productSku, $imageId));
303 $productSku =
'testProductSku';
304 $this->productRepositoryMock->expects($this->once())->method(
'get')->with($productSku)
305 ->willReturn($this->productMock);
306 $entryMock = $this->createMock(\
Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
307 $this->productMock->expects($this->once())->method(
'getMediaGalleryEntries')
308 ->willReturn([$entryMock]);
309 $this->assertEquals([$entryMock], $this->model->getList($productSku));
testRemoveWithNonExistingImage()
testGetWithNonExistingProduct()
testCreateWithInvalidImageException()
testUpdateWithNonExistingImage()
testGetWithNonExistingImage()
testCreateWithCannotSaveException()
testUpdateWithCannotSaveException()