Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TaxTest.php
Go to the documentation of this file.
1 <?php
11 
13 
14 class TaxTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $objectManager;
20 
21  protected function setUp()
22  {
23  $this->objectManager = new ObjectManager($this);
24  }
25 
26  public function testGetBackendModelName()
27  {
28  $this->assertEquals(
29  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
30  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::getBackendModelName()
31  );
32  }
33 
39  public function testValidate($data, $expected)
40  {
41  $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class)
42  ->setMethods(['getName'])
43  ->disableOriginalConstructor()
44  ->getMock();
45  $attributeMock
46  ->expects($this->any())
47  ->method('getName')
48  ->will($this->returnValue('weeeTax'));
49 
50  $modelMock = $this->getMockBuilder(\Magento\Weee\Model\Attribute\Backend\Weee\Tax::class)
51  ->setMethods(['getAttribute'])
52  ->disableOriginalConstructor()
53  ->getMock();
54  $modelMock
55  ->expects($this->any())
56  ->method('getAttribute')
57  ->will($this->returnValue($attributeMock));
58 
59  $taxes = [reset($data)];
60  $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
61  ->setMethods(['getData'])
62  ->disableOriginalConstructor()
63  ->getMock();
64  $productMock
65  ->expects($this->any())
66  ->method('getData')
67  ->will($this->returnValue($taxes));
68 
69  // No exception
70  $modelMock->validate($productMock);
71 
72  $taxes = $data;
73  $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
74  ->setMethods(['getData'])
75  ->disableOriginalConstructor()
76  ->getMock();
77  $productMock
78  ->expects($this->any())
79  ->method('getData')
80  ->will($this->returnValue($taxes));
81 
82  // Exception caught
83  $this->expectException('Exception');
84  $this->expectExceptionMessage($expected);
85  $modelMock->validate($productMock);
86  }
87 
91  public function dataProviderValidate()
92  {
93  return [
94  'withDuplicate' => [
95  'data' => [
96  ['state' => 12, 'country' => 'US', 'website_id' => '1'],
97  ['state' => 99, 'country' => 'ES', 'website_id' => '1'],
98  ['state' => 12, 'country' => 'US', 'website_id' => '1'],
99  ['state' => null, 'country' => 'ES', 'website_id' => '1']
100  ],
101  'expected' => 'Set unique country-state combinations within the same fixed product tax. '
102  . 'Verify the combinations and try again.',
103  ]
104  ];
105  }
106 
107  public function testAfterLoad()
108  {
109  $data = [['website_id' => 1, 'value' => 1]];
110 
111  $attributeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax::class)
112  ->setMethods(['loadProductData'])
113  ->disableOriginalConstructor()
114  ->getMock();
115  $attributeTaxMock
116  ->expects($this->any())
117  ->method('loadProductData')
118  ->will($this->returnValue($data));
119 
120  $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class)
121  ->setMethods(['getName'])
122  ->disableOriginalConstructor()
123  ->getMock();
124  $attributeMock
125  ->expects($this->any())
126  ->method('getName')
127  ->will($this->returnValue('weeeTax'));
128 
129  $model = $this->objectManager->getObject(
130  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
131  [
132  'attributeTax' => $attributeTaxMock,
133  '_attribute' => $attributeMock
134  ]
135  );
136 
137  $model->setAttribute($attributeMock);
138  $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
139  ->setMethods(['setData'])
140  ->disableOriginalConstructor()
141  ->getMock();
142 
143  $result = $model->afterLoad($productMock);
144  $this->assertNotNull($result);
145  }
146 
155  public function testAfterSaveWithRegion($origData, $currentData, $expectedData)
156  {
157  $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
158  ->setMethods(['getOrigData', 'getData'])
159  ->disableOriginalConstructor()
160  ->getMock();
161 
162  $productMock
163  ->expects($this->once())
164  ->method('getOrigData')
165  ->will($this->returnValue($origData));
166  $productMock
167  ->expects($this->any())
168  ->method('getData')
169  ->will($this->returnValue($currentData));
170 
171  $attributeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax::class)
172  ->setMethods(['deleteProductData', 'insertProductData'])
173  ->disableOriginalConstructor()
174  ->getMock();
175  $attributeTaxMock
176  ->expects($this->once())
177  ->method('deleteProductData')
178  ->will($this->returnValue(null));
179  $attributeTaxMock
180  ->expects($this->once())
181  ->method('insertProductData')
182  ->with($productMock, $expectedData)
183  ->will($this->returnValue(null));
184 
185  $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class)
186  ->setMethods(['getName', 'getId'])
187  ->disableOriginalConstructor()
188  ->getMock();
189  $attributeMock
190  ->expects($this->any())
191  ->method('getName')
192  ->will($this->returnValue('weeeTax'));
193  $attributeMock
194  ->expects($this->any())
195  ->method('getId')
196  ->will($this->returnValue(1));
197 
198  $model = $this->objectManager->getObject(
199  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
200  [
201  'attributeTax' => $attributeTaxMock,
202  '_attribute' => $attributeMock
203  ]
204  );
205 
206  $model->setAttribute($attributeMock);
207  $model->afterSave($productMock);
208  }
209 
214  {
215  return [
216  'withRegion' => [
217  'origData' => [['state' => 12, 'country' => 'US', 'website_id' => '1']],
218  'currentData' => [['state' => 12, 'country' => 'US', 'website_id' => '2', 'price' => 100]],
219  'expectedData' => ['state' => 12, 'country' => 'US', 'website_id' => '2', 'value' => 100,
220  'attribute_id' => 1]],
221  'withNoRegion' => [
222  'origData' => [['country' => 'US', 'website_id' => '1']],
223  'currentData' => [['country' => 'US', 'website_id' => '2', 'price' => 100]],
224  'expectedData' => ['state' => 0, 'country' => 'US', 'website_id' => '2', 'value' => 100,
225  'attribute_id' => 1]]
226  ];
227  }
228 
229  public function testAfterDelete()
230  {
231  $attributeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax::class)
232  ->setMethods(['deleteProductData'])
233  ->disableOriginalConstructor()
234  ->getMock();
235  $attributeTaxMock
236  ->expects($this->once())
237  ->method('deleteProductData')
238  ->with(null, null)
239  ->will($this->returnValue(null));
240 
241  $model = $this->objectManager->getObject(
242  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
243  [
244  'attributeTax' => $attributeTaxMock,
245  ]
246  );
247 
248  $model->afterDelete(null);
249  }
250 
251  public function testGetTable()
252  {
253  $attributeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax::class)
254  ->setMethods(['getTable'])
255  ->disableOriginalConstructor()
256  ->getMock();
257  $attributeTaxMock
258  ->expects($this->once())
259  ->method('getTable')
260  ->with('weee_tax')
261  ->will($this->returnValue(null));
262 
263  $model = $this->objectManager->getObject(
264  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
265  [
266  'attributeTax' => $attributeTaxMock,
267  ]
268  );
269 
270  $model->getTable();
271  }
272 
278  public function testGetEntityIdField() : void
279  {
280  $attributeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\ResourceModel\Attribute\Backend\Weee\Tax::class)
281  ->setMethods(['getIdFieldName'])
282  ->disableOriginalConstructor()
283  ->getMock();
284 
285  $attributeTaxMock
286  ->expects($this->once())
287  ->method('getIdFieldName')
288  ->willReturn(null);
289 
290  $model = $this->objectManager->getObject(
291  \Magento\Weee\Model\Attribute\Backend\Weee\Tax::class,
292  [
293  'attributeTax' => $attributeTaxMock,
294  ]
295  );
296 
297  $model->getEntityIdField();
298  }
299 }
testAfterSaveWithRegion($origData, $currentData, $expectedData)
Definition: TaxTest.php:155