Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeOptionProviderTest.php
Go to the documentation of this file.
1 <?php
8 
19 
23 class AttributeOptionProviderTest extends \PHPUnit\Framework\TestCase
24 {
28  private $model;
29 
33  private $objectManagerHelper;
34 
38  private $scopeResolver;
39 
43  private $select;
44 
48  private $connectionMock;
49 
53  private $abstractAttribute;
54 
58  private $scope;
59 
63  private $attributeResource;
64 
68  private $optionSelectBuilder;
69 
70  protected function setUp()
71  {
72  $this->select = $this->getMockBuilder(Select::class)
73  ->setMethods([])
74  ->disableOriginalConstructor()
75  ->getMock();
76 
77  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
78  ->disableOriginalConstructor()
79  ->getMockForAbstractClass();
80 
81  $this->scope = $this->getMockBuilder(ScopeInterface::class)
82  ->disableOriginalConstructor()
83  ->getMockForAbstractClass();
84 
85  $this->scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)
86  ->disableOriginalConstructor()
87  ->getMockForAbstractClass();
88 
89  $this->attributeResource = $this->getMockBuilder(Attribute::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $this->optionSelectBuilder = $this->getMockBuilder(OptionSelectBuilderInterface::class)
94  ->disableOriginalConstructor()
95  ->getMockForAbstractClass();
96 
97  $this->abstractAttribute = $this->getMockBuilder(AbstractAttribute::class)
98  ->setMethods(['getSourceModel', 'getSource'])
99  ->disableOriginalConstructor()
100  ->getMockForAbstractClass();
101 
102  $this->objectManagerHelper = new ObjectManagerHelper($this);
103  $this->model = $this->objectManagerHelper->getObject(
104  AttributeOptionProvider::class,
105  [
106  'attributeResource' => $this->attributeResource,
107  'scopeResolver' => $this->scopeResolver,
108  'optionSelectBuilder' => $this->optionSelectBuilder,
109  ]
110  );
111  }
112 
117  public function testGetAttributeOptions(array $options)
118  {
119  $this->scopeResolver->expects($this->any())
120  ->method('getScope')
121  ->willReturn($this->scope);
122 
123  $this->optionSelectBuilder->expects($this->any())
124  ->method('getSelect')
125  ->with($this->abstractAttribute, 4, $this->scope)
126  ->willReturn($this->select);
127 
128  $this->attributeResource->expects($this->once())
129  ->method('getConnection')
130  ->willReturn($this->connectionMock);
131 
132  $this->connectionMock->expects($this->once())
133  ->method('fetchAll')
134  ->with($this->select)
135  ->willReturn($options);
136 
137  $this->assertEquals(
138  $options,
139  $this->model->getAttributeOptions($this->abstractAttribute, 4)
140  );
141  }
142 
148  {
149  $this->scopeResolver->expects($this->any())
150  ->method('getScope')
151  ->willReturn($this->scope);
152 
153  $source = $this->getMockBuilder(AbstractSource::class)
154  ->disableOriginalConstructor()
155  ->setMethods(['getAllOptions'])
156  ->getMockForAbstractClass();
157  $source->expects($this->once())
158  ->method('getAllOptions')
159  ->willReturn([
160  ['value' => 13, 'label' => 'Option Value for index 13'],
161  ['value' => 14, 'label' => 'Option Value for index 14'],
162  ['value' => 15, 'label' => 'Option Value for index 15']
163  ]);
164 
165  $this->abstractAttribute->expects($this->any())
166  ->method('getSource')
167  ->willReturn($source);
168  $this->abstractAttribute->expects($this->atLeastOnce())
169  ->method('getSourceModel')
170  ->willReturn('getSourceModel value');
171 
172  $this->optionSelectBuilder->expects($this->any())
173  ->method('getSelect')
174  ->with($this->abstractAttribute, 1, $this->scope)
175  ->willReturn($this->select);
176 
177  $this->attributeResource->expects($this->once())
178  ->method('getConnection')
179  ->willReturn($this->connectionMock);
180 
181  $this->connectionMock->expects($this->once())
182  ->method('fetchAll')
183  ->with($this->select)
184  ->willReturn($options);
185 
186  $this->assertEquals(
187  $options,
188  $this->model->getAttributeOptions($this->abstractAttribute, 1)
189  );
190  }
191 
196  {
197  return [
198  [
199  [
200  [
201  'sku' => 'Configurable1-Black',
202  'product_id' => 4,
203  'attribute_code' => 'color',
204  'value_index' => '13',
205  'option_title' => 'Black',
206  ],
207  [
208  'sku' => 'Configurable1-White',
209  'product_id' => 4,
210  'attribute_code' => 'color',
211  'value_index' => '14',
212  'option_title' => 'White',
213  ],
214  [
215  'sku' => 'Configurable1-Red',
216  'product_id' => 4,
217  'attribute_code' => 'color',
218  'value_index' => '15',
219  'option_title' => 'Red',
220  ],
221  ],
222  ],
223  ];
224  }
225 
230  {
231  return [
232  [
233  [
234  [
235  'sku' => 'Configurable1-Black',
236  'product_id' => 4,
237  'attribute_code' => 'color',
238  'value_index' => '13',
239  'option_title' => 'Option Value for index 13',
240  'default_title' => 'Option Value for index 13',
241  ],
242  [
243  'sku' => 'Configurable1-White',
244  'product_id' => 4,
245  'attribute_code' => 'color',
246  'value_index' => '14',
247  'option_title' => 'Option Value for index 14',
248  'default_title' => 'Option Value for index 14',
249  ],
250  [
251  'sku' => 'Configurable1-Red',
252  'product_id' => 4,
253  'attribute_code' => 'color',
254  'value_index' => '15',
255  'option_title' => 'Option Value for index 15',
256  'default_title' => 'Option Value for index 15',
257  ],
258  ],
259  ],
260  ];
261  }
262 }
$source
Definition: source.php:23