Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductVariationsBuilderTest.php
Go to the documentation of this file.
1 <?php
8 
9 class ProductVariationsBuilderTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $model;
15 
19  private $customAttributeFactory;
20 
24  protected $productFactory;
25 
29  private $variationMatrix;
30 
34  protected $product;
35 
36  protected function setUp()
37  {
38  $this->customAttributeFactory = $this->createMock(\Magento\Framework\Api\AttributeValueFactory::class);
39 
40  $this->product = $this->createPartialMock(
41  \Magento\Catalog\Model\Product::class,
42  ['getData', 'getPrice', 'getName', 'getSku', '__wakeup', 'getCustomAttributes']
43  );
44 
45  $this->productFactory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, ['create']);
46 
47  $this->variationMatrix = $this->createMock(
48  \Magento\ConfigurableProduct\Model\Product\Type\VariationMatrix::class
49  );
50 
51  $this->model = new \Magento\ConfigurableProduct\Model\ProductVariationsBuilder(
52  $this->productFactory,
53  $this->customAttributeFactory,
54  $this->variationMatrix
55  );
56  }
57 
58  public function testCreate()
59  {
60  $output = $this->createPartialMock(
61  \Magento\Catalog\Model\Product::class,
62  ['setPrice', '__wakeup', 'setData', 'getCustomAttributes', 'setName', 'setSku', 'setVisibility']
63  );
64  $attributes = [10 => ['attribute_code' => 'sort_order']];
65  $variations = [
66  [10 => ['value' => 15, 'price' => ['pricing_value' => 10]]],
67  ];
68  $this->variationMatrix->expects($this->once())
69  ->method('getVariations')
70  ->with($attributes)
71  ->willReturn($variations);
72 
73  $this->productFactory->expects($this->once())->method('create')->willReturn($output);
74  $productData = ['id' => '10', 'title' => 'simple'];
75  $this->product->expects($this->once())->method('getData')->willReturn($productData);
76  $this->product->expects($this->once())->method('getName')->willReturn('simple');
77  $this->product->expects($this->once())->method('getSku')->willReturn('simple-sku');
78  $this->product->expects($this->once())->method('getPrice')->willReturn(10);
79 
80  $output->expects($this->at(0))->method('setData')->with($productData);
81 
82  $attribute = $this->createMock(\Magento\Framework\Api\AttributeInterface::class);
83  $attribute->expects($this->once())
84  ->method('setAttributeCode')
85  ->with('sort_order')
86  ->willReturnSelf();
87 
88  $attribute->expects($this->once())
89  ->method('setValue')
90  ->with(15)
91  ->willReturnSelf();
92 
93  $this->customAttributeFactory->expects($this->once())
94  ->method('create')
95  ->willReturn($attribute);
96 
97  $output->expects($this->once())->method('getCustomAttributes')->willReturn([]);
98 
99  $output->expects($this->at(2))->method('setData')->with('custom_attributes', ['sort_order' => $attribute]);
100  $output->expects($this->once())->method('setPrice')->with(10);
101  $output->expects($this->once())->method('setName')->with('simple-15');
102  $output->expects($this->once())->method('setSku')->with('simple-sku-15');
103  $output->expects($this->once())->method('setVisibility')
104  ->with(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE);
105 
106  $this->assertEquals([$output], $this->model->create($this->product, $attributes));
107  }
108 }
$attributes
Definition: matrix.phtml:13
$productData