Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GalleryManagementTest.php
Go to the documentation of this file.
1 <?php
9 
10 class GalleryManagementTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
21 
26 
30  protected $productMock;
31 
36 
41 
42  protected function setUp()
43  {
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, [
47  'setStoreId',
48  'getData',
49  'getStoreId',
50  'getSku',
51  'getCustomAttribute',
52  'getMediaGalleryEntries',
53  'setMediaGalleryEntries',
54  ]);
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
60  );
61  $this->attributeValueMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64  }
65 
71  {
72  $entryContentMock = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
76 
77  $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
78  ->willReturn(false);
79 
80  $this->model->create("sku", $this->mediaGalleryEntryMock);
81  }
82 
88  {
89  $productSku = 'mediaProduct';
90  $entryContentMock = $this->getMockBuilder(\Magento\Framework\Api\Data\ImageContentInterface::class)
91  ->disableOriginalConstructor()
92  ->getMock();
93  $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
94  $this->productRepositoryMock->expects($this->once())
95  ->method('get')
96  ->with($productSku)
97  ->willReturn($this->productMock);
98 
99  $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
100  ->willReturn(true);
101 
102  $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
103  ->willThrowException(new \Exception());
104  $this->model->create($productSku, $this->mediaGalleryEntryMock);
105  }
106 
107  public function testCreate()
108  {
109  $productSku = 'mediaProduct';
110  $entryContentMock = $this->createMock(
111  \Magento\Framework\Api\Data\ImageContentInterface::class
112  );
113  $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
114 
115  $this->productRepositoryMock->expects($this->once())
116  ->method('get')
117  ->with($productSku)
118  ->willReturn($this->productMock);
119  $this->productRepositoryMock->expects($this->once())
120  ->method('save')
121  ->with($this->productMock)
122  ->willReturn($this->productMock);
123 
124  $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
125  ->willReturn(true);
126 
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]);
133 
134  $this->assertEquals(42, $this->model->create($productSku, $this->mediaGalleryEntryMock));
135  }
136 
142  {
143  $productSku = 'testProduct';
144  $entryMock = $this->createMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
145  $entryId = 42;
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
150  );
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);
156  }
157 
163  {
164  $productSku = 'testProduct';
165  $entryMock = $this->createMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
166  $entryId = 42;
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
171  );
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)
177  ->willThrowException(new \Exception());
178  $this->model->update($productSku, $entryMock);
179  }
180 
181  public function testUpdate()
182  {
183  $productSku = 'testProduct';
184  $entryMock = $this->createMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
185  $entryId = 42;
186  $entrySecondId = 43;
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
191  );
192  $existingSecondEntryMock = $this->createMock(
193  \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
194  );
195 
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]);
204 
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']);
209 
210  $this->productMock->expects($this->once())->method('setMediaGalleryEntries')
211  ->with([$entryMock, $existingSecondEntryMock])
212  ->willReturnSelf();
213  $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
214  $this->assertTrue($this->model->update($productSku, $entryMock));
215  }
216 
222  {
223  $productSku = 'testProduct';
224  $entryId = 42;
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
229  );
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);
234  }
235 
236  public function testRemove()
237  {
238  $productSku = 'testProduct';
239  $entryId = 42;
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
244  );
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')
249  ->with([]);
250  $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
251  $this->assertTrue($this->model->remove($productSku, $entryId));
252  }
253 
259  {
260  $productSku = 'testProduct';
261  $imageId = 42;
262  $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
263  ->willThrowException(new \Exception());
264  $this->model->get($productSku, $imageId);
265  }
266 
271  public function testGetWithNonExistingImage()
272  {
273  $productSku = 'testProduct';
274  $imageId = 43;
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
279  );
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);
284  }
285 
286  public function testGet()
287  {
288  $productSku = 'testProduct';
289  $imageId = 42;
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
294  );
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));
299  }
300 
301  public function testGetList()
302  {
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));
310  }
311 }