Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GrandTotalDetailsPluginTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10 
14 class GrandTotalDetailsPluginTest extends \PHPUnit\Framework\TestCase
15 {
20 
25 
29  protected $ratesFactoryMock;
30 
34  protected $taxConfigMock;
35 
39  protected $subjectMock;
40 
45 
49  protected $model;
50 
51  protected function setUp()
52  {
53  $this->subjectMock = $this->getMockBuilder(\Magento\Quote\Model\Cart\TotalsConverter::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->totalSegmentExtensionFactoryMock = $this->getMockBuilder(
58  \Magento\Quote\Api\Data\TotalSegmentExtensionFactory::class
59  )->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->detailsFactoryMock = $this->getMockBuilder(
63  \Magento\Tax\Api\Data\GrandTotalDetailsInterfaceFactory::class
64  )
65  ->disableOriginalConstructor()
66  ->setMethods(['create'])
67  ->getMock();
68 
69  $this->ratesFactoryMock = $this->getMockBuilder(\Magento\Tax\Api\Data\GrandTotalRatesInterfaceFactory::class)
70  ->disableOriginalConstructor()
71  ->setMethods(['create'])
72  ->getMock();
73 
74  $this->taxConfigMock = $this->getMockBuilder(\Magento\Tax\Model\Config::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77 
78  $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $serializer->expects($this->any())
83  ->method('serialize')
84  ->willReturnCallback(
85  function ($value) {
86  return json_encode($value);
87  }
88  );
89 
90  $serializer->expects($this->any())
91  ->method('unserialize')
92  ->willReturnCallback(
93  function ($value) {
94  return json_decode($value, true);
95  }
96  );
97 
98  $this->objectManagerHelper = new ObjectManager($this);
99  $this->model = $this->objectManagerHelper->getObject(
100  \Magento\Tax\Model\Quote\GrandTotalDetailsPlugin::class,
101  [
102  'totalSegmentExtensionFactory' => $this->totalSegmentExtensionFactoryMock,
103  'ratesFactory' => $this->ratesFactoryMock,
104  'detailsFactory' => $this->detailsFactoryMock,
105  'taxConfig' => $this->taxConfigMock,
106  'serializer' => $serializer
107  ]
108  );
109  }
110 
115  protected function setupTaxTotal(array $data)
116  {
117  $taxTotalMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\Total::class)
118  ->disableOriginalConstructor()
119  ->getMock();
120 
121  $taxTotalMock->expects($this->any())
122  ->method('getData')
123  ->willReturn($data);
124 
125  return $taxTotalMock;
126  }
127 
132  protected function setupTaxRateFactoryMock(array $taxRate)
133  {
134  $taxRateMock = $this->getMockBuilder(\Magento\Tax\Api\Data\GrandTotalRatesInterface::class)
135  ->getMock();
136 
137  $this->ratesFactoryMock->expects($this->once())
138  ->method('create')
139  ->with([])
140  ->willReturn($taxRateMock);
141 
142  $taxRateMock->expects($this->once())
143  ->method('setPercent')
144  ->with($taxRate['percent'])
145  ->willReturnSelf();
146  $taxRateMock->expects($this->once())
147  ->method('setTitle')
148  ->with($taxRate['title'])
149  ->willReturnSelf();
150  return $taxRateMock;
151  }
152 
157  protected function setupTaxDetails(array $taxDetails)
158  {
159  $taxDetailsMock = $this->getMockBuilder(\Magento\Tax\Api\Data\GrandTotalDetailsInterface::class)
160  ->getMock();
161 
162  $this->detailsFactoryMock->expects($this->once())
163  ->method('create')
164  ->willReturn($taxDetailsMock);
165 
166  $taxDetailsMock->expects($this->once())
167  ->method('setAmount')
168  ->with($taxDetails['amount'])
169  ->willReturnSelf();
170 
171  $taxDetailsMock->expects($this->once())
172  ->method('setRates')
173  ->with($taxDetails['rates'])
174  ->willReturnSelf();
175 
176  $taxDetailsMock->expects($this->once())
177  ->method('setGroupId')
178  ->with(1)
179  ->willReturnSelf();
180 
181  return $taxDetailsMock;
182  }
183 
184  public function testAfterProcess()
185  {
186  $taxRate = [
187  'percent' => 8.25,
188  'title' => 'TX',
189  ];
190  $taxAmount = 10;
191 
192  $taxRateMock = $this->setupTaxRateFactoryMock($taxRate);
193 
194  $taxDetailsMock = $this->setupTaxDetails(
195  [
196  'amount' => $taxAmount,
197  'rates' => [$taxRateMock],
198  ]
199  );
200 
201  $taxTotalData = [
202  'full_info' => json_encode([
203  [
204  'amount' => $taxAmount,
205  'rates' => [$taxRate],
206  ],
207  ]),
208  ];
209  $taxTotalMock = $this->setupTaxTotal($taxTotalData);
210  $addressTotals = [
211  'tax' => $taxTotalMock,
212  ];
213 
214  $extensionAttributeMock = $this->getMockBuilder(
215  \Magento\Quote\Api\Data\TotalSegmentExtensionInterface::class
216  )->setMethods(
217  [
218  'setTaxGrandtotalDetails',
219 
220  ]
221  )->getMockForAbstractClass();
222  $extensionAttributeMock->expects($this->once())
223  ->method('setTaxGrandtotalDetails')
224  ->with([$taxDetailsMock])
225  ->willReturnSelf();
226 
227  $taxSegmentMock = $this->getMockBuilder(\Magento\Quote\Model\Cart\TotalSegment::class)
228  ->disableOriginalConstructor()
229  ->getMock();
230  $taxSegmentMock->expects($this->once())
231  ->method('getExtensionAttributes')
232  ->willReturn($extensionAttributeMock);
233  $taxSegmentMock->expects($this->once())
234  ->method('setExtensionAttributes')
235  ->with($extensionAttributeMock)
236  ->willReturnSelf();
237 
238  $totalSegments = [
239  'tax' => $taxSegmentMock,
240  ];
241 
242  $result = $this->model->afterProcess($this->subjectMock, $totalSegments, $addressTotals);
243  $this->assertEquals($totalSegments, $result);
244  }
245 }
$value
Definition: gender.phtml:16
$taxRate
Definition: tax_rule.php:12