AttributeOptionProviderTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Test\Unit\Model;
  7. use Magento\ConfigurableProduct\Model\AttributeOptionProvider;
  8. use Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface;
  9. use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
  10. use Magento\Framework\App\ScopeInterface;
  11. use Magento\Framework\App\ScopeResolverInterface;
  12. use Magento\Framework\DB\Select;
  13. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  14. use Magento\Framework\DB\Adapter\AdapterInterface;
  15. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  16. use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute;
  17. /**
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class AttributeOptionProviderTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var AttributeOptionProvider
  24. */
  25. private $model;
  26. /**
  27. * @var ObjectManagerHelper
  28. */
  29. private $objectManagerHelper;
  30. /**
  31. * @var ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $scopeResolver;
  34. /**
  35. * @var Select|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $select;
  38. /**
  39. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $connectionMock;
  42. /**
  43. * @var AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $abstractAttribute;
  46. /**
  47. * @var ScopeInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $scope;
  50. /**
  51. * @var Attribute|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $attributeResource;
  54. /**
  55. * @var OptionSelectBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $optionSelectBuilder;
  58. protected function setUp()
  59. {
  60. $this->select = $this->getMockBuilder(Select::class)
  61. ->setMethods([])
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
  65. ->disableOriginalConstructor()
  66. ->getMockForAbstractClass();
  67. $this->scope = $this->getMockBuilder(ScopeInterface::class)
  68. ->disableOriginalConstructor()
  69. ->getMockForAbstractClass();
  70. $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)
  71. ->disableOriginalConstructor()
  72. ->getMockForAbstractClass();
  73. $this->attributeResource = $this->getMockBuilder(Attribute::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->optionSelectBuilder = $this->getMockBuilder(OptionSelectBuilderInterface::class)
  77. ->disableOriginalConstructor()
  78. ->getMockForAbstractClass();
  79. $this->abstractAttribute = $this->getMockBuilder(AbstractAttribute::class)
  80. ->setMethods(['getSourceModel', 'getSource'])
  81. ->disableOriginalConstructor()
  82. ->getMockForAbstractClass();
  83. $this->objectManagerHelper = new ObjectManagerHelper($this);
  84. $this->model = $this->objectManagerHelper->getObject(
  85. AttributeOptionProvider::class,
  86. [
  87. 'attributeResource' => $this->attributeResource,
  88. 'scopeResolver' => $this->scopeResolver,
  89. 'optionSelectBuilder' => $this->optionSelectBuilder,
  90. ]
  91. );
  92. }
  93. /**
  94. * @param array $options
  95. * @dataProvider getAttributeOptionsDataProvider
  96. */
  97. public function testGetAttributeOptions(array $options)
  98. {
  99. $this->scopeResolver->expects($this->any())
  100. ->method('getScope')
  101. ->willReturn($this->scope);
  102. $this->optionSelectBuilder->expects($this->any())
  103. ->method('getSelect')
  104. ->with($this->abstractAttribute, 4, $this->scope)
  105. ->willReturn($this->select);
  106. $this->attributeResource->expects($this->once())
  107. ->method('getConnection')
  108. ->willReturn($this->connectionMock);
  109. $this->connectionMock->expects($this->once())
  110. ->method('fetchAll')
  111. ->with($this->select)
  112. ->willReturn($options);
  113. $this->assertEquals(
  114. $options,
  115. $this->model->getAttributeOptions($this->abstractAttribute, 4)
  116. );
  117. }
  118. /**
  119. * @param array $options
  120. * @dataProvider optionsWithBackendModelDataProvider
  121. */
  122. public function testGetAttributeOptionsWithBackendModel(array $options)
  123. {
  124. $this->scopeResolver->expects($this->any())
  125. ->method('getScope')
  126. ->willReturn($this->scope);
  127. $source = $this->getMockBuilder(AbstractSource::class)
  128. ->disableOriginalConstructor()
  129. ->setMethods(['getAllOptions'])
  130. ->getMockForAbstractClass();
  131. $source->expects($this->once())
  132. ->method('getAllOptions')
  133. ->willReturn([
  134. ['value' => 13, 'label' => 'Option Value for index 13'],
  135. ['value' => 14, 'label' => 'Option Value for index 14'],
  136. ['value' => 15, 'label' => 'Option Value for index 15']
  137. ]);
  138. $this->abstractAttribute->expects($this->any())
  139. ->method('getSource')
  140. ->willReturn($source);
  141. $this->abstractAttribute->expects($this->atLeastOnce())
  142. ->method('getSourceModel')
  143. ->willReturn('getSourceModel value');
  144. $this->optionSelectBuilder->expects($this->any())
  145. ->method('getSelect')
  146. ->with($this->abstractAttribute, 1, $this->scope)
  147. ->willReturn($this->select);
  148. $this->attributeResource->expects($this->once())
  149. ->method('getConnection')
  150. ->willReturn($this->connectionMock);
  151. $this->connectionMock->expects($this->once())
  152. ->method('fetchAll')
  153. ->with($this->select)
  154. ->willReturn($options);
  155. $this->assertEquals(
  156. $options,
  157. $this->model->getAttributeOptions($this->abstractAttribute, 1)
  158. );
  159. }
  160. /**
  161. * @return array
  162. */
  163. public function getAttributeOptionsDataProvider()
  164. {
  165. return [
  166. [
  167. [
  168. [
  169. 'sku' => 'Configurable1-Black',
  170. 'product_id' => 4,
  171. 'attribute_code' => 'color',
  172. 'value_index' => '13',
  173. 'option_title' => 'Black',
  174. ],
  175. [
  176. 'sku' => 'Configurable1-White',
  177. 'product_id' => 4,
  178. 'attribute_code' => 'color',
  179. 'value_index' => '14',
  180. 'option_title' => 'White',
  181. ],
  182. [
  183. 'sku' => 'Configurable1-Red',
  184. 'product_id' => 4,
  185. 'attribute_code' => 'color',
  186. 'value_index' => '15',
  187. 'option_title' => 'Red',
  188. ],
  189. ],
  190. ],
  191. ];
  192. }
  193. /**
  194. * @return array
  195. */
  196. public function optionsWithBackendModelDataProvider()
  197. {
  198. return [
  199. [
  200. [
  201. [
  202. 'sku' => 'Configurable1-Black',
  203. 'product_id' => 4,
  204. 'attribute_code' => 'color',
  205. 'value_index' => '13',
  206. 'option_title' => 'Option Value for index 13',
  207. 'default_title' => 'Option Value for index 13',
  208. ],
  209. [
  210. 'sku' => 'Configurable1-White',
  211. 'product_id' => 4,
  212. 'attribute_code' => 'color',
  213. 'value_index' => '14',
  214. 'option_title' => 'Option Value for index 14',
  215. 'default_title' => 'Option Value for index 14',
  216. ],
  217. [
  218. 'sku' => 'Configurable1-Red',
  219. 'product_id' => 4,
  220. 'attribute_code' => 'color',
  221. 'value_index' => '15',
  222. 'option_title' => 'Option Value for index 15',
  223. 'default_title' => 'Option Value for index 15',
  224. ],
  225. ],
  226. ],
  227. ];
  228. }
  229. }