Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RelationsDataSaverTest.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class RelationsDataSaverTest extends \PHPUnit\Framework\TestCase
18 {
22  private $relationsDataSaver;
23 
27  private $resourceMock;
28 
32  private $connectionMock;
33 
37  private $productRelationMock;
38 
39  protected function setUp()
40  {
41  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
42  $this->resourceMock = $this->getMockBuilder(ResourceConnection::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
46  ->disableOriginalConstructor()
47  ->getMock();
48 
49  $this->productRelationMock = $this->getMockBuilder(Relation::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52 
53  $this->relationsDataSaver = $helper->getObject(
54  RelationsDataSaver::class,
55  [
56  'resource' => $this->resourceMock,
57  'productRelation' => $this->productRelationMock
58  ]
59  );
60  }
61 
62  public function testSaveOptions()
63  {
64  $options = [1, 2];
65  $table_name= 'catalog_product_bundle_option';
66  $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
67  $this->resourceMock->expects($this->once())
68  ->method('getTableName')
69  ->with('catalog_product_bundle_option')
70  ->willReturn($table_name);
71  $this->connectionMock->expects($this->once())
72  ->method('insertOnDuplicate')
73  ->with(
74  $table_name,
75  $options,
76  [
77  'required',
78  'position',
79  'type'
80  ]
81  );
82 
83  $this->relationsDataSaver->saveOptions($options);
84  }
85 
86  public function testSaveOptionValues()
87  {
88  $optionsValues = [1, 2];
89  $table_name= 'catalog_product_bundle_option_value';
90 
91  $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
92  $this->resourceMock->expects($this->once())
93  ->method('getTableName')
94  ->with('catalog_product_bundle_option_value')
95  ->willReturn($table_name);
96  $this->connectionMock->expects($this->once())
97  ->method('insertOnDuplicate')
98  ->with(
99  $table_name,
100  $optionsValues,
101  ['title']
102  );
103 
104  $this->relationsDataSaver->saveOptionValues($optionsValues);
105  }
106 
107  public function testSaveSelections()
108  {
109  $selections = [1, 2];
110  $table_name= 'catalog_product_bundle_selection';
111 
112  $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
113  $this->resourceMock->expects($this->once())
114  ->method('getTableName')
115  ->with('catalog_product_bundle_selection')
116  ->willReturn($table_name);
117  $this->connectionMock->expects($this->once())
118  ->method('insertOnDuplicate')
119  ->with(
120  $table_name,
121  $selections,
122  [
123  'selection_id',
124  'product_id',
125  'position',
126  'is_default',
127  'selection_price_type',
128  'selection_price_value',
129  'selection_qty',
130  'selection_can_change_qty'
131  ]
132  );
133 
134  $this->relationsDataSaver->saveSelections($selections);
135  }
136 
137  public function testSaveProductRelations()
138  {
139  $parentId = 1;
140  $children = [2, 3];
141 
142  $this->productRelationMock->expects($this->once())
143  ->method('processRelations')
144  ->with($parentId, $children);
145 
146  $this->relationsDataSaver->saveProductRelations($parentId, $children);
147  }
148 }
$helper
Definition: iframe.phtml:13
$children
Definition: actions.phtml:11