QuoteTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Model\Cart\SalesModel;
  7. class QuoteTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /** @var \Magento\Payment\Model\Cart\SalesModel\Quote */
  10. protected $_model;
  11. /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject */
  12. protected $_quoteMock;
  13. protected function setUp()
  14. {
  15. $this->_quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  16. $this->_model = new \Magento\Payment\Model\Cart\SalesModel\Quote($this->_quoteMock);
  17. }
  18. public function testGetDataUsingMethod()
  19. {
  20. $this->_quoteMock->expects(
  21. $this->once()
  22. )->method(
  23. 'getDataUsingMethod'
  24. )->with(
  25. 'any key',
  26. 'any args'
  27. )->will(
  28. $this->returnValue('some value')
  29. );
  30. $this->assertEquals('some value', $this->_model->getDataUsingMethod('any key', 'any args'));
  31. }
  32. public function testGetTaxContainer()
  33. {
  34. $this->_quoteMock->expects(
  35. $this->any()
  36. )->method(
  37. 'getBillingAddress'
  38. )->will(
  39. $this->returnValue('billing address')
  40. );
  41. $this->_quoteMock->expects(
  42. $this->any()
  43. )->method(
  44. 'getShippingAddress'
  45. )->will(
  46. $this->returnValue('shipping address')
  47. );
  48. $this->assertEquals('shipping address', $this->_model->getTaxContainer());
  49. $this->_quoteMock->expects($this->any())->method('getIsVirtual')->will($this->returnValue(1));
  50. $this->assertEquals('billing address', $this->_model->getTaxContainer());
  51. }
  52. /**
  53. * @param string $pItem
  54. * @param string $name
  55. * @param int $qty
  56. * @param float $price
  57. * @dataProvider getAllItemsDataProvider
  58. */
  59. public function testGetAllItems($pItem, $name, $qty, $price)
  60. {
  61. $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item\AbstractItem::class);
  62. $itemMock->expects($this->any())->method('getParentItem')->will($this->returnValue($pItem));
  63. $itemMock->expects($this->once())->method('__call')->with('getName')->will($this->returnValue($name));
  64. $itemMock->expects($this->any())->method('getTotalQty')->will($this->returnValue($qty));
  65. $itemMock->expects($this->any())->method('getBaseCalculationPrice')->will($this->returnValue($price));
  66. $expected = [
  67. new \Magento\Framework\DataObject(
  68. [
  69. 'parent_item' => $pItem,
  70. 'name' => $name,
  71. 'qty' => $qty,
  72. 'price' => $price,
  73. 'original_item' => $itemMock,
  74. ]
  75. ),
  76. ];
  77. $this->_quoteMock->expects($this->once())->method('getAllItems')->will($this->returnValue([$itemMock]));
  78. $this->assertEquals($expected, $this->_model->getAllItems());
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getAllItemsDataProvider()
  84. {
  85. return [
  86. ['parent item 1', 'name 1', 1, 0.1],
  87. ['parent item 2', 'name 2', 2, 1.2],
  88. ['parent item 3', 'name 3', 3, 2.3],
  89. ];
  90. }
  91. public function testGetBaseSubtotal()
  92. {
  93. $this->_quoteMock->expects(
  94. $this->once()
  95. )->method(
  96. '__call'
  97. )->with(
  98. 'getBaseSubtotal'
  99. )->will(
  100. $this->returnValue(100)
  101. );
  102. $this->assertEquals(100, $this->_model->getBaseSubtotal());
  103. }
  104. /**
  105. * @param int $isVirtual
  106. * @param string $getterMethod
  107. * @dataProvider getterDataProvider
  108. */
  109. public function testGetter($isVirtual, $getterMethod)
  110. {
  111. $address = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  112. $address->expects(
  113. $this->any()
  114. )->method(
  115. '__call'
  116. )->with(
  117. $getterMethod
  118. )->will(
  119. $this->returnValue($getterMethod)
  120. );
  121. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  122. $quoteMock->expects($this->any())->method('getIsVirtual')->will($this->returnValue($isVirtual));
  123. $method = 'getShippingAddress';
  124. if ($isVirtual) {
  125. $method = 'getBillingAddress';
  126. }
  127. $quoteMock->expects($this->any())->method($method)->will($this->returnValue($address));
  128. $model = new \Magento\Payment\Model\Cart\SalesModel\Quote($quoteMock);
  129. $this->assertEquals($getterMethod, $model->{$getterMethod}());
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getterDataProvider()
  135. {
  136. return [
  137. [0, 'getBaseTaxAmount'],
  138. [1, 'getBaseTaxAmount'],
  139. [0, 'getBaseShippingAmount'],
  140. [1, 'getBaseShippingAmount'],
  141. [0, 'getBaseDiscountAmount'],
  142. [1, 'getBaseDiscountAmount']
  143. ];
  144. }
  145. }