Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TotalTest.php
Go to the documentation of this file.
1 <?php
8 
9 class TotalTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $model;
15 
16  protected function setUp()
17  {
18  $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
19  ->setMethods(['unserialize'])
20  ->disableOriginalConstructor()
21  ->getMockForAbstractClass();
22  $serializer->expects($this->any())
23  ->method('unserialize')
24  ->willReturnCallback(function ($value) {
25  return json_decode($value, true);
26  });
27 
28  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
29  $this->model = $objectManagerHelper->getObject(
30  \Magento\Quote\Model\Quote\Address\Total::class,
31  [
32  'serializer' => $serializer
33  ]
34  );
35  }
36 
43  public function testSetTotalAmount($code, $amount, $storedCode)
44  {
45  $result = $this->model->setTotalAmount($code, $amount);
46  $this->assertArrayHasKey($storedCode, $result);
47  $this->assertEquals($result[$storedCode], $amount);
48  $this->assertEquals($this->model->getAllTotalAmounts()[$code], $amount);
49  $this->assertSame($this->model, $result);
50  }
51 
55  public function setTotalAmountDataProvider()
56  {
57  return [
58  'Subtotal' => [
59  'code' => 'subtotal',
60  'amount' => 42.42,
61  'stored_code' => 'subtotal'
62  ],
63  'Other total' => [
64  'code' => 'other',
65  'amount' => 42.17,
66  'stored_code' => 'other_amount'
67  ]
68  ];
69  }
70 
77  public function testSetBaseTotalAmount($code, $amount, $storedCode)
78  {
79  $result = $this->model->setBaseTotalAmount($code, $amount);
80  $this->assertArrayHasKey($storedCode, $result);
81  $this->assertEquals($result[$storedCode], $amount);
82  $this->assertEquals($this->model->getAllBaseTotalAmounts()[$code], $amount);
83  $this->assertSame($this->model, $result);
84  }
85 
90  {
91  return [
92  'Subtotal' => [
93  'code' => 'subtotal',
94  'amount' => 17.42,
95  'stored_code' => 'base_subtotal'
96  ],
97  'Other total' => [
98  'code' => 'other',
99  'amount' => 42.17,
100  'stored_code' => 'base_other_amount'
101  ]
102  ];
103  }
104 
111  public function testAddTotalAmount($initialAmount, $delta, $updatedAmount)
112  {
113  $code = 'turbo';
114  $this->model->setTotalAmount($code, $initialAmount);
115 
116  $this->assertSame($this->model, $this->model->addTotalAmount($code, $delta));
117  $this->assertEquals($updatedAmount, $this->model->getTotalAmount($code));
118  }
119 
123  public function addTotalAmountDataProvider()
124  {
125  return [
126  'Zero' => [
127  'initialAmount' => 0,
128  'delta' => 42,
129  'updatedAmount' => 42
130  ],
131  'Non-zero' => [
132  'initialAmount' => 20,
133  'delta' => 22,
134  'updatedAmount' => 42
135  ]
136  ];
137  }
138 
145  public function testAddBaseTotalAmount($initialAmount, $delta, $updatedAmount)
146  {
147  $code = 'turbo';
148  $this->model->setBaseTotalAmount($code, $initialAmount);
149 
150  $this->assertSame($this->model, $this->model->addBaseTotalAmount($code, $delta));
151  $this->assertEquals($updatedAmount, $this->model->getBaseTotalAmount($code));
152  }
153 
158  {
159  return [
160  'Zero' => [
161  'initialAmount' => 0,
162  'delta' => 42,
163  'updatedAmount' => 42
164  ],
165  'Non-zero' => [
166  'initialAmount' => 20,
167  'delta' => 22,
168  'updatedAmount' => 42
169  ]
170  ];
171  }
172 
173  public function testGetTotalAmount()
174  {
175  $code = 'super';
176  $amount = 42;
177  $this->model->setTotalAmount($code, $amount);
178  $this->assertEquals($amount, $this->model->getTotalAmount($code));
179  }
180 
181  public function testGetTotalAmountAbsent()
182  {
183  $this->assertEquals(0, $this->model->getTotalAmount('mega'));
184  }
185 
186  public function testGetBaseTotalAmount()
187  {
188  $code = 'wow';
189  $amount = 42;
190  $this->model->setBaseTotalAmount($code, $amount);
191  $this->assertEquals($amount, $this->model->getBaseTotalAmount($code));
192  }
193 
195  {
196  $this->assertEquals(0, $this->model->getBaseTotalAmount('great'));
197  }
198 
207  public function testGetFullInfo($input, $expected)
208  {
209  $this->model->setFullInfo($input);
210  $this->assertEquals($expected, $this->model->getFullInfo());
211  }
212 
216  public function getFullInfoDataProvider()
217  {
218  $myArray = ['team' => 'kiwis'];
219  $serializedInput = json_encode($myArray);
220 
221  return [
222  'simple array' => [
223  $myArray,
224  $myArray,
225  ],
226 
227  'serialized array' => [
228  $serializedInput,
229  $myArray,
230  ],
231 
232  'null input/output' => [
233  null,
234  null,
235  ],
236 
237  'float input' => [
238  1.23,
239  1.23,
240  ],
241  ];
242  }
243 }
testSetBaseTotalAmount($code, $amount, $storedCode)
Definition: TotalTest.php:77
$amount
Definition: order.php:14
$value
Definition: gender.phtml:16
testAddTotalAmount($initialAmount, $delta, $updatedAmount)
Definition: TotalTest.php:111
testSetTotalAmount($code, $amount, $storedCode)
Definition: TotalTest.php:43
testAddBaseTotalAmount($initialAmount, $delta, $updatedAmount)
Definition: TotalTest.php:145
$code
Definition: info.phtml:12