Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductRepositorySaveTest.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Catalog\Model\ProductFactory;
18 use PHPUnit_Framework_MockObject_MockObject as MockObject;
19 
24 class ProductRepositorySaveTest extends \PHPUnit\Framework\TestCase
25 {
29  private $productAttributeRepository;
30 
34  private $productFactory;
35 
39  private $product;
40 
44  private $result;
45 
49  private $productRepository;
50 
54  private $extensionAttributes;
55 
59  private $eavAttribute;
60 
64  private $option;
65 
69  private $plugin;
70 
71  protected function setUp()
72  {
73  $this->productAttributeRepository = $this->getMockForAbstractClass(ProductAttributeRepositoryInterface::class);
74 
75  $this->productFactory = $this->getMockBuilder(ProductFactory::class)
76  ->disableOriginalConstructor()
77  ->setMethods(['create'])
78  ->getMock();
79 
80  $this->product = $this->getMockBuilder(Product::class)
81  ->disableOriginalConstructor()
82  ->setMethods(['getTypeId', 'getExtensionAttributes'])
83  ->getMock();
84 
85  $this->result = $this->getMockBuilder(Product::class)
86  ->disableOriginalConstructor()
87  ->setMethods(['getExtensionAttributes'])
88  ->getMock();
89 
90  $this->productRepository = $this->getMockForAbstractClass(ProductRepositoryInterface::class);
91 
92  $this->extensionAttributes = $this->getMockBuilder(ProductExtensionAttributes::class)
93  ->disableOriginalConstructor()
94  ->setMethods(['getConfigurableProductOptions', 'getConfigurableProductLinks'])
95  ->getMockForAbstractClass();
96 
97  $this->eavAttribute = $this->getMockForAbstractClass(ProductAttributeInterface::class);
98 
99  $this->option = $this->getMockForAbstractClass(OptionInterface::class);
100 
101  $this->plugin = (new ObjectManager($this))->getObject(
102  ProductRepositorySave::class,
103  [
104  'productAttributeRepository' => $this->productAttributeRepository,
105  'productFactory' => $this->productFactory
106  ]
107  );
108  }
109 
111  {
112  $this->product->expects(static::once())
113  ->method('getTypeId')
114  ->willReturn('simple');
115  $this->product->expects(static::never())
116  ->method('getExtensionAttributes');
117 
118  $this->assertEquals(
119  $this->result,
120  $this->plugin->afterSave($this->productRepository, $this->result, $this->product)
121  );
122  }
123 
124  public function testAfterSaveWithoutOptions()
125  {
126  $this->product->expects(static::once())
127  ->method('getTypeId')
128  ->willReturn(Configurable::TYPE_CODE);
129 
130  $this->result->expects(static::once())
131  ->method('getExtensionAttributes')
132  ->willReturn($this->extensionAttributes);
133 
134  $this->extensionAttributes->expects(static::once())
135  ->method('getConfigurableProductOptions')
136  ->willReturn([]);
137  $this->extensionAttributes->expects(static::once())
138  ->method('getConfigurableProductLinks')
139  ->willReturn([]);
140 
141  $this->productAttributeRepository->expects(static::never())
142  ->method('get');
143 
144  $this->assertEquals(
145  $this->result,
146  $this->plugin->afterSave($this->productRepository, $this->result, $this->product)
147  );
148  }
149 
154  public function testAfterSaveWithLinks()
155  {
156  $links = [4, 5];
157  $this->product->expects(static::once())
158  ->method('getTypeId')
159  ->willReturn(Configurable::TYPE_CODE);
160 
161  $this->result->expects(static::once())
162  ->method('getExtensionAttributes')
163  ->willReturn($this->extensionAttributes);
164  $this->extensionAttributes->expects(static::once())
165  ->method('getConfigurableProductOptions')
166  ->willReturn(null);
167  $this->extensionAttributes->expects(static::once())
168  ->method('getConfigurableProductLinks')
169  ->willReturn($links);
170 
171  $this->productAttributeRepository->expects(static::never())
172  ->method('get');
173 
174  $product = $this->getMockBuilder(Product::class)
175  ->disableOriginalConstructor()
176  ->setMethods(['load', 'getData', '__wakeup'])
177  ->getMock();
178 
179  $this->productFactory->expects(static::exactly(2))
180  ->method('create')
181  ->willReturn($product);
182 
183  $product->expects(static::exactly(2))
184  ->method('load')
185  ->willReturnSelf();
186  $product->expects(static::never())
187  ->method('getData');
188 
189  $this->plugin->afterSave($this->productRepository, $this->result, $this->product);
190  }
191 
197  {
198  $simpleProductId = 4;
199  $links = [$simpleProductId, 5];
200  $attributeCode = 'color';
201  $attributeId = 23;
202 
203  $this->option->expects(static::once())
204  ->method('getAttributeId')
205  ->willReturn($attributeId);
206 
207  $this->product->expects(static::once())
208  ->method('getTypeId')
209  ->willReturn(Configurable::TYPE_CODE);
210 
211  $this->result->expects(static::once())
212  ->method('getExtensionAttributes')
213  ->willReturn($this->extensionAttributes);
214  $this->extensionAttributes->expects(static::once())
215  ->method('getConfigurableProductOptions')
216  ->willReturn([$this->option]);
217  $this->extensionAttributes->expects(static::once())
218  ->method('getConfigurableProductLinks')
219  ->willReturn($links);
220 
221  $this->productAttributeRepository->expects(static::once())
222  ->method('get')
223  ->willReturn($this->eavAttribute);
224 
225  $this->eavAttribute->expects(static::once())
226  ->method('getAttributeCode')
227  ->willReturn($attributeCode);
228 
229  $product = $this->getMockBuilder(Product::class)
230  ->disableOriginalConstructor()
231  ->setMethods(['load', 'getData', '__wakeup'])
232  ->getMock();
233 
234  $this->productFactory->expects(static::once())
235  ->method('create')
236  ->willReturn($product);
237  $product->expects(static::once())
238  ->method('load')
239  ->with($simpleProductId)
240  ->willReturnSelf();
241  $product->expects(static::once())
242  ->method('getData')
243  ->with($attributeCode)
244  ->willReturn(false);
245 
246  $this->plugin->afterSave($this->productRepository, $this->result, $this->product);
247  }
248 
254  {
255  $links = [4, 5];
256  $attributeCode = 'color';
257  $attributeId = 23;
258 
259  $this->option->expects(static::once())
260  ->method('getAttributeId')
261  ->willReturn($attributeId);
262 
263  $this->product->expects(static::once())
264  ->method('getTypeId')
265  ->willReturn(Configurable::TYPE_CODE);
266 
267  $this->result->expects(static::once())
268  ->method('getExtensionAttributes')
269  ->willReturn($this->extensionAttributes);
270  $this->extensionAttributes->expects(static::once())
271  ->method('getConfigurableProductOptions')
272  ->willReturn([$this->option]);
273  $this->extensionAttributes->expects(static::once())
274  ->method('getConfigurableProductLinks')
275  ->willReturn($links);
276 
277  $this->productAttributeRepository->expects(static::once())
278  ->method('get')
279  ->willReturn($this->eavAttribute);
280 
281  $this->eavAttribute->expects(static::once())
282  ->method('getAttributeCode')
283  ->willReturn($attributeCode);
284 
285  $product = $this->getMockBuilder(Product::class)
286  ->disableOriginalConstructor()
287  ->setMethods(['load', 'getData', '__wakeup'])
288  ->getMock();
289 
290  $this->productFactory->expects(static::exactly(2))
291  ->method('create')
292  ->willReturn($product);
293  $product->expects(static::exactly(2))
294  ->method('load')
295  ->willReturnSelf();
296  $product->expects(static::exactly(4))
297  ->method('getData')
298  ->with($attributeCode)
299  ->willReturn($attributeId);
300 
301  $this->plugin->afterSave($this->productRepository, $this->result, $this->product);
302  }
303 }
$attributeCode
Definition: extend.phtml:12