DataTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Test\Unit\Helper;
  7. use Magento\Catalog\Model\Product\Image\UrlBuilder;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. class DataTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\ConfigurableProduct\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. protected $_model;
  15. /**
  16. * @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $_imageHelperMock;
  19. /**
  20. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $_productMock;
  23. /**
  24. * @var UrlBuilder|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $imageUrlBuilder;
  27. protected function setUp()
  28. {
  29. $objectManager = new ObjectManager($this);
  30. $this->imageUrlBuilder = $this->getMockBuilder(UrlBuilder::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->_imageHelperMock = $this->createMock(\Magento\Catalog\Helper\Image::class);
  34. $this->_productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  35. $this->_model = $objectManager->getObject(
  36. \Magento\ConfigurableProduct\Helper\Data::class,
  37. [
  38. '_imageHelper' => $this->_imageHelperMock
  39. ]
  40. );
  41. $objectManager->setBackwardCompatibleProperty($this->_model, 'imageUrlBuilder', $this->imageUrlBuilder);
  42. }
  43. public function testGetAllowAttributes()
  44. {
  45. $typeInstanceMock = $this->createMock(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class);
  46. $typeInstanceMock->expects($this->once())
  47. ->method('getConfigurableAttributes')
  48. ->with($this->_productMock);
  49. $this->_productMock->expects($this->once())
  50. ->method('getTypeInstance')
  51. ->will($this->returnValue($typeInstanceMock));
  52. $this->_model->getAllowAttributes($this->_productMock);
  53. }
  54. /**
  55. * @param array $expected
  56. * @param array $data
  57. * @dataProvider getOptionsDataProvider
  58. */
  59. public function testGetOptions(array $expected, array $data)
  60. {
  61. if (count($data['allowed_products'])) {
  62. $imageHelper1 = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $imageHelper1->expects($this->any())
  66. ->method('getUrl')
  67. ->willReturn('http://example.com/base_img_url');
  68. $imageHelper2 = $this->getMockBuilder(\Magento\Catalog\Helper\Image::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $imageHelper2->expects($this->any())
  72. ->method('getUrl')
  73. ->willReturn('http://example.com/base_img_url_2');
  74. $this->_imageHelperMock->expects($this->any())
  75. ->method('init')
  76. ->willReturnMap([
  77. [$data['current_product_mock'], 'product_page_image_large', [], $imageHelper1],
  78. [$data['allowed_products'][0], 'product_page_image_large', [], $imageHelper1],
  79. [$data['allowed_products'][1], 'product_page_image_large', [], $imageHelper2],
  80. ]);
  81. }
  82. $this->assertEquals(
  83. $expected,
  84. $this->_model->getOptions($data['current_product_mock'], $data['allowed_products'])
  85. );
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function getOptionsDataProvider()
  91. {
  92. $currentProductMock = $this->createPartialMock(
  93. \Magento\Catalog\Model\Product::class,
  94. ['getTypeInstance', '__wakeup']
  95. );
  96. $provider = [];
  97. $provider[] = [
  98. [],
  99. [
  100. 'allowed_products' => [],
  101. 'current_product_mock' => $currentProductMock,
  102. ],
  103. ];
  104. $attributesCount = 3;
  105. $attributes = [];
  106. for ($i = 1; $i < $attributesCount; $i++) {
  107. $attribute = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getProductAttribute']);
  108. $productAttribute = $this->createPartialMock(
  109. \Magento\Framework\DataObject::class,
  110. ['getId', 'getAttributeCode']
  111. );
  112. $productAttribute->expects($this->any())
  113. ->method('getId')
  114. ->will($this->returnValue('attribute_id_' . $i));
  115. $productAttribute->expects($this->any())
  116. ->method('getAttributeCode')
  117. ->will($this->returnValue('attribute_code_' . $i));
  118. $attribute->expects($this->any())
  119. ->method('getProductAttribute')
  120. ->will($this->returnValue($productAttribute));
  121. $attributes[] = $attribute;
  122. }
  123. $typeInstanceMock = $this->createMock(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class);
  124. $typeInstanceMock->expects($this->any())
  125. ->method('getConfigurableAttributes')
  126. ->will($this->returnValue($attributes));
  127. $currentProductMock->expects($this->any())
  128. ->method('getTypeInstance')
  129. ->will($this->returnValue($typeInstanceMock));
  130. $allowedProducts = [];
  131. for ($i = 1; $i <= 2; $i++) {
  132. $productMock = $this->createPartialMock(
  133. \Magento\Catalog\Model\Product::class,
  134. ['getData', 'getImage', 'getId', '__wakeup', 'getMediaGalleryImages']
  135. );
  136. $productMock->expects($this->any())
  137. ->method('getData')
  138. ->will($this->returnCallback([$this, 'getDataCallback']));
  139. $productMock->expects($this->any())
  140. ->method('getId')
  141. ->will($this->returnValue('product_id_' . $i));
  142. if ($i == 2) {
  143. $productMock->expects($this->any())
  144. ->method('getImage')
  145. ->will($this->returnValue(true));
  146. }
  147. $allowedProducts[] = $productMock;
  148. }
  149. $provider[] = [
  150. [
  151. 'attribute_id_1' => [
  152. 'attribute_code_value_1' => ['product_id_1', 'product_id_2'],
  153. ],
  154. 'index' => [
  155. 'product_id_1' => [
  156. 'attribute_id_1' => 'attribute_code_value_1',
  157. 'attribute_id_2' => 'attribute_code_value_2'
  158. ],
  159. 'product_id_2' => [
  160. 'attribute_id_1' => 'attribute_code_value_1',
  161. 'attribute_id_2' => 'attribute_code_value_2'
  162. ]
  163. ],
  164. 'attribute_id_2' => [
  165. 'attribute_code_value_2' => ['product_id_1', 'product_id_2'],
  166. ],
  167. ],
  168. [
  169. 'allowed_products' => $allowedProducts,
  170. 'current_product_mock' => $currentProductMock,
  171. ],
  172. ];
  173. return $provider;
  174. }
  175. /**
  176. * @param string $key
  177. * @return string
  178. */
  179. public function getDataCallback($key)
  180. {
  181. $map = [];
  182. for ($k = 1; $k < 3; $k++) {
  183. $map['attribute_code_' . $k] = 'attribute_code_value_' . $k;
  184. }
  185. return $map[$key];
  186. }
  187. public function testGetGalleryImages()
  188. {
  189. $productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
  190. ->setMethods(['getMediaGalleryImages'])
  191. ->getMockForAbstractClass();
  192. $productMock->expects($this->once())
  193. ->method('getMediaGalleryImages')
  194. ->willReturn($this->getImagesCollection());
  195. $this->imageUrlBuilder->expects($this->exactly(3))
  196. ->method('getUrl')
  197. ->withConsecutive(
  198. [
  199. self::identicalTo('test_file'),
  200. self::identicalTo('product_page_image_small')
  201. ],
  202. [
  203. self::identicalTo('test_file'),
  204. self::identicalTo('product_page_image_medium')
  205. ],
  206. [
  207. self::identicalTo('test_file'),
  208. self::identicalTo('product_page_image_large')
  209. ]
  210. )
  211. ->will(self::onConsecutiveCalls(
  212. 'testSmallImageUrl',
  213. 'testMediumImageUrl',
  214. 'testLargeImageUrl'
  215. ));
  216. $this->_imageHelperMock->expects(self::never())
  217. ->method('setImageFile')
  218. ->with('test_file')
  219. ->willReturnSelf();
  220. $this->_imageHelperMock->expects(self::never())
  221. ->method('getUrl')
  222. ->willReturn('product_page_image_small_url');
  223. $this->_imageHelperMock->expects(self::never())
  224. ->method('getUrl')
  225. ->willReturn('product_page_image_medium_url');
  226. $this->_imageHelperMock->expects(self::never())
  227. ->method('getUrl')
  228. ->willReturn('product_page_image_large_url');
  229. $this->assertInstanceOf(
  230. \Magento\Framework\Data\Collection::class,
  231. $this->_model->getGalleryImages($productMock)
  232. );
  233. }
  234. /**
  235. * @return \Magento\Framework\Data\Collection
  236. */
  237. private function getImagesCollection()
  238. {
  239. $collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
  240. ->disableOriginalConstructor()
  241. ->getMock();
  242. $items = [
  243. new \Magento\Framework\DataObject([
  244. 'file' => 'test_file'
  245. ]),
  246. ];
  247. $collectionMock->expects($this->any())
  248. ->method('getIterator')
  249. ->willReturn(new \ArrayIterator($items));
  250. return $collectionMock;
  251. }
  252. }