TotalTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Quote\Address;
  7. class TotalTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Quote\Model\Quote\Address\Total
  11. */
  12. protected $model;
  13. protected function setUp()
  14. {
  15. $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  16. ->setMethods(['unserialize'])
  17. ->disableOriginalConstructor()
  18. ->getMockForAbstractClass();
  19. $serializer->expects($this->any())
  20. ->method('unserialize')
  21. ->willReturnCallback(function ($value) {
  22. return json_decode($value, true);
  23. });
  24. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  25. $this->model = $objectManagerHelper->getObject(
  26. \Magento\Quote\Model\Quote\Address\Total::class,
  27. [
  28. 'serializer' => $serializer
  29. ]
  30. );
  31. }
  32. /**
  33. * @param string $code
  34. * @param float $amount
  35. * @param string $storedCode
  36. * @dataProvider setTotalAmountDataProvider
  37. */
  38. public function testSetTotalAmount($code, $amount, $storedCode)
  39. {
  40. $result = $this->model->setTotalAmount($code, $amount);
  41. $this->assertArrayHasKey($storedCode, $result);
  42. $this->assertEquals($result[$storedCode], $amount);
  43. $this->assertEquals($this->model->getAllTotalAmounts()[$code], $amount);
  44. $this->assertSame($this->model, $result);
  45. }
  46. /**
  47. * @return array
  48. */
  49. public function setTotalAmountDataProvider()
  50. {
  51. return [
  52. 'Subtotal' => [
  53. 'code' => 'subtotal',
  54. 'amount' => 42.42,
  55. 'stored_code' => 'subtotal'
  56. ],
  57. 'Other total' => [
  58. 'code' => 'other',
  59. 'amount' => 42.17,
  60. 'stored_code' => 'other_amount'
  61. ]
  62. ];
  63. }
  64. /**
  65. * @param string $code
  66. * @param float $amount
  67. * @param string $storedCode
  68. * @dataProvider setBaseTotalAmountDataProvider
  69. */
  70. public function testSetBaseTotalAmount($code, $amount, $storedCode)
  71. {
  72. $result = $this->model->setBaseTotalAmount($code, $amount);
  73. $this->assertArrayHasKey($storedCode, $result);
  74. $this->assertEquals($result[$storedCode], $amount);
  75. $this->assertEquals($this->model->getAllBaseTotalAmounts()[$code], $amount);
  76. $this->assertSame($this->model, $result);
  77. }
  78. /**
  79. * @return array
  80. */
  81. public function setBaseTotalAmountDataProvider()
  82. {
  83. return [
  84. 'Subtotal' => [
  85. 'code' => 'subtotal',
  86. 'amount' => 17.42,
  87. 'stored_code' => 'base_subtotal'
  88. ],
  89. 'Other total' => [
  90. 'code' => 'other',
  91. 'amount' => 42.17,
  92. 'stored_code' => 'base_other_amount'
  93. ]
  94. ];
  95. }
  96. /**
  97. * @param float $initialAmount
  98. * @param float $delta
  99. * @param float $updatedAmount
  100. * @dataProvider addTotalAmountDataProvider
  101. */
  102. public function testAddTotalAmount($initialAmount, $delta, $updatedAmount)
  103. {
  104. $code = 'turbo';
  105. $this->model->setTotalAmount($code, $initialAmount);
  106. $this->assertSame($this->model, $this->model->addTotalAmount($code, $delta));
  107. $this->assertEquals($updatedAmount, $this->model->getTotalAmount($code));
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function addTotalAmountDataProvider()
  113. {
  114. return [
  115. 'Zero' => [
  116. 'initialAmount' => 0,
  117. 'delta' => 42,
  118. 'updatedAmount' => 42
  119. ],
  120. 'Non-zero' => [
  121. 'initialAmount' => 20,
  122. 'delta' => 22,
  123. 'updatedAmount' => 42
  124. ]
  125. ];
  126. }
  127. /**
  128. * @param float $initialAmount
  129. * @param float $delta
  130. * @param float $updatedAmount
  131. * @dataProvider addBaseTotalAmountDataProvider
  132. */
  133. public function testAddBaseTotalAmount($initialAmount, $delta, $updatedAmount)
  134. {
  135. $code = 'turbo';
  136. $this->model->setBaseTotalAmount($code, $initialAmount);
  137. $this->assertSame($this->model, $this->model->addBaseTotalAmount($code, $delta));
  138. $this->assertEquals($updatedAmount, $this->model->getBaseTotalAmount($code));
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function addBaseTotalAmountDataProvider()
  144. {
  145. return [
  146. 'Zero' => [
  147. 'initialAmount' => 0,
  148. 'delta' => 42,
  149. 'updatedAmount' => 42
  150. ],
  151. 'Non-zero' => [
  152. 'initialAmount' => 20,
  153. 'delta' => 22,
  154. 'updatedAmount' => 42
  155. ]
  156. ];
  157. }
  158. public function testGetTotalAmount()
  159. {
  160. $code = 'super';
  161. $amount = 42;
  162. $this->model->setTotalAmount($code, $amount);
  163. $this->assertEquals($amount, $this->model->getTotalAmount($code));
  164. }
  165. public function testGetTotalAmountAbsent()
  166. {
  167. $this->assertEquals(0, $this->model->getTotalAmount('mega'));
  168. }
  169. public function testGetBaseTotalAmount()
  170. {
  171. $code = 'wow';
  172. $amount = 42;
  173. $this->model->setBaseTotalAmount($code, $amount);
  174. $this->assertEquals($amount, $this->model->getBaseTotalAmount($code));
  175. }
  176. public function testGetBaseTotalAmountAbsent()
  177. {
  178. $this->assertEquals(0, $this->model->getBaseTotalAmount('great'));
  179. }
  180. /**
  181. * Verify handling of serialized, non-serialized input into and out of getFullInfo()
  182. *
  183. * @covers \Magento\Quote\Model\Quote\Address\Total::getFullInfo()
  184. * @param $input
  185. * @param $expected
  186. * @dataProvider getFullInfoDataProvider
  187. */
  188. public function testGetFullInfo($input, $expected)
  189. {
  190. $this->model->setFullInfo($input);
  191. $this->assertEquals($expected, $this->model->getFullInfo());
  192. }
  193. /**
  194. * @return array
  195. */
  196. public function getFullInfoDataProvider()
  197. {
  198. $myArray = ['team' => 'kiwis'];
  199. $serializedInput = json_encode($myArray);
  200. return [
  201. 'simple array' => [
  202. $myArray,
  203. $myArray,
  204. ],
  205. 'serialized array' => [
  206. $serializedInput,
  207. $myArray,
  208. ],
  209. 'null input/output' => [
  210. null,
  211. null,
  212. ],
  213. 'float input' => [
  214. 1.23,
  215. 1.23,
  216. ],
  217. ];
  218. }
  219. }