PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 290 lines | 208 code | 30 blank | 52 comment | 0 complexity | 989a24d05e681c653de36f5572b3c4ee MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Copyright © 2016 Magento. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. // @codingStandardsIgnoreFile
  8. namespace Magento\Catalog\Test\Unit\Model\Product\Gallery;
  9. class GalleryManagementTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var \Magento\Catalog\Model\Product\Gallery\GalleryManagement
  13. */
  14. protected $model;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $productRepositoryMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $contentValidatorMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $productMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $mediaGalleryEntryMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\AttributeValue
  33. */
  34. protected $attributeValueMock;
  35. protected function setUp()
  36. {
  37. $this->productRepositoryMock = $this->getMock('\Magento\Catalog\Api\ProductRepositoryInterface');
  38. $this->contentValidatorMock = $this->getMock('\Magento\Framework\Api\ImageContentValidatorInterface');
  39. $this->productMock = $this->getMock(
  40. '\Magento\Catalog\Model\Product',
  41. [
  42. 'setStoreId',
  43. 'getData',
  44. 'getStoreId',
  45. 'getSku',
  46. 'getCustomAttribute',
  47. 'getMediaGalleryEntries',
  48. 'setMediaGalleryEntries',
  49. ],
  50. [],
  51. '',
  52. false
  53. );
  54. $this->mediaGalleryEntryMock =
  55. $this->getMock('Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  56. $this->model = new \Magento\Catalog\Model\Product\Gallery\GalleryManagement(
  57. $this->productRepositoryMock,
  58. $this->contentValidatorMock
  59. );
  60. $this->attributeValueMock = $this->getMockBuilder('\Magento\Framework\Api\AttributeValue')
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. }
  64. /**
  65. * @expectedException \Magento\Framework\Exception\InputException
  66. * @expectedExceptionMessage The image content is not valid.
  67. */
  68. public function testCreateWithInvalidImageException()
  69. {
  70. $entryContentMock = $this->getMockBuilder('\Magento\Framework\Api\Data\ImageContentInterface')
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
  74. $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
  75. ->willReturn(false);
  76. $this->model->create("sku", $this->mediaGalleryEntryMock);
  77. }
  78. /**
  79. * @expectedException \Magento\Framework\Exception\StateException
  80. * @expectedExceptionMessage Cannot save product.
  81. */
  82. public function testCreateWithCannotSaveException()
  83. {
  84. $productSku = 'mediaProduct';
  85. $entryContentMock = $this->getMockBuilder('\Magento\Framework\Api\Data\ImageContentInterface')
  86. ->disableOriginalConstructor()
  87. ->getMock();;
  88. $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
  89. $this->productRepositoryMock->expects($this->once())
  90. ->method('get')
  91. ->with($productSku)
  92. ->willReturn($this->productMock);
  93. $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
  94. ->willReturn(true);
  95. $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
  96. ->willThrowException(new \Exception());
  97. $this->model->create($productSku, $this->mediaGalleryEntryMock);
  98. }
  99. public function testCreate()
  100. {
  101. $productSku = 'mediaProduct';
  102. $entryContentMock = $this->getMock(
  103. 'Magento\Framework\Api\Data\ImageContentInterface'
  104. );
  105. $this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);
  106. $this->productRepositoryMock->expects($this->once())
  107. ->method('get')
  108. ->with($productSku)
  109. ->willReturn($this->productMock);
  110. $this->productRepositoryMock->expects($this->once())
  111. ->method('save')
  112. ->with($this->productMock)
  113. ->willReturn($this->productMock);
  114. $this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
  115. ->willReturn(true);
  116. $newEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  117. $newEntryMock->expects($this->exactly(2))->method('getId')->willReturn(42);
  118. $this->productMock->expects($this->at(2))->method('getMediaGalleryEntries')
  119. ->willReturn([$newEntryMock]);
  120. $this->productMock->expects($this->once())->method('setMediaGalleryEntries')
  121. ->with([$this->mediaGalleryEntryMock]);
  122. $this->assertEquals(42, $this->model->create($productSku, $this->mediaGalleryEntryMock));
  123. }
  124. /**
  125. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  126. * @expectedExceptionMessage There is no image with provided ID.
  127. */
  128. public function testUpdateWithNonExistingImage()
  129. {
  130. $productSku = 'testProduct';
  131. $entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  132. $entryId = 42;
  133. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  134. ->willReturn($this->productMock);
  135. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  136. $existingEntryMock->expects($this->once())->method('getId')->willReturn(43);
  137. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  138. ->willReturn([$existingEntryMock]);
  139. $entryMock->expects($this->once())->method('getId')->willReturn($entryId);
  140. $this->model->update($productSku, $entryMock);
  141. }
  142. /**
  143. * @expectedException \Magento\Framework\Exception\StateException
  144. * @expectedExceptionMessage Cannot save product.
  145. */
  146. public function testUpdateWithCannotSaveException()
  147. {
  148. $productSku = 'testProduct';
  149. $entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  150. $entryId = 42;
  151. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  152. ->willReturn($this->productMock);
  153. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  154. $existingEntryMock->expects($this->once())->method('getId')->willReturn($entryId);
  155. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  156. ->willReturn([$existingEntryMock]);
  157. $entryMock->expects($this->once())->method('getId')->willReturn($entryId);
  158. $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
  159. ->willThrowException(new \Exception());
  160. $this->model->update($productSku, $entryMock);
  161. }
  162. public function testUpdate()
  163. {
  164. $productSku = 'testProduct';
  165. $entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  166. $entryId = 42;
  167. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  168. ->willReturn($this->productMock);
  169. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  170. $existingEntryMock->expects($this->once())->method('getId')->willReturn($entryId);
  171. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  172. ->willReturn([$existingEntryMock]);
  173. $entryMock->expects($this->once())->method('getId')->willReturn($entryId);
  174. $this->productMock->expects($this->once())->method('setMediaGalleryEntries')
  175. ->willReturn([$entryMock]);
  176. $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
  177. $this->assertTrue($this->model->update($productSku, $entryMock));
  178. }
  179. /**
  180. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  181. * @expectedExceptionMessage There is no image with provided ID.
  182. */
  183. public function testRemoveWithNonExistingImage()
  184. {
  185. $productSku = 'testProduct';
  186. $entryId = 42;
  187. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  188. ->willReturn($this->productMock);
  189. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  190. $existingEntryMock->expects($this->once())->method('getId')->willReturn(43);
  191. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  192. ->willReturn([$existingEntryMock]);
  193. $this->model->remove($productSku, $entryId);
  194. }
  195. public function testRemove()
  196. {
  197. $productSku = 'testProduct';
  198. $entryId = 42;
  199. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  200. ->willReturn($this->productMock);
  201. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  202. $existingEntryMock->expects($this->once())->method('getId')->willReturn(42);
  203. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  204. ->willReturn([$existingEntryMock]);
  205. $this->productMock->expects($this->once())->method('setMediaGalleryEntries')
  206. ->with([]);
  207. $this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
  208. $this->assertTrue($this->model->remove($productSku, $entryId));
  209. }
  210. /**
  211. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  212. * @expectedExceptionMessage Such product doesn't exist
  213. */
  214. public function testGetWithNonExistingProduct()
  215. {
  216. $productSku = 'testProduct';
  217. $imageId = 42;
  218. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  219. ->willThrowException(new \Exception());
  220. $this->model->get($productSku, $imageId);
  221. }
  222. /**
  223. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  224. * @expectedExceptionText Such image doesn't exist
  225. */
  226. public function testGetWithNonExistingImage()
  227. {
  228. $productSku = 'testProduct';
  229. $imageId = 43;
  230. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  231. ->willReturn($this->productMock);
  232. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  233. $existingEntryMock->expects($this->once())->method('getId')->willReturn(44);
  234. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  235. ->willReturn([$existingEntryMock]);
  236. $this->model->get($productSku, $imageId);
  237. }
  238. public function testGet()
  239. {
  240. $productSku = 'testProduct';
  241. $imageId = 42;
  242. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  243. ->willReturn($this->productMock);
  244. $existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  245. $existingEntryMock->expects($this->once())->method('getId')->willReturn(42);
  246. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  247. ->willReturn([$existingEntryMock]);
  248. $this->assertEquals($existingEntryMock, $this->model->get($productSku, $imageId));
  249. }
  250. public function testGetList()
  251. {
  252. $productSku = 'testProductSku';
  253. $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
  254. ->willReturn($this->productMock);
  255. $entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
  256. $this->productMock->expects($this->once())->method('getMediaGalleryEntries')
  257. ->willReturn([$entryMock]);
  258. $this->assertEquals([$entryMock], $this->model->getList($productSku));
  259. }
  260. }