AbstractPriceTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Test\Unit\Price;
  7. use \Magento\Framework\Pricing\Price\AbstractPrice;
  8. /**
  9. * Class RegularPriceTest
  10. */
  11. class AbstractPriceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var AbstractPrice
  15. */
  16. protected $price;
  17. /**
  18. * @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $priceInfoMock;
  21. /**
  22. * @var \Magento\Framework\Pricing\SaleableInterface |\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $saleableItemMock;
  25. /**
  26. * @var \Magento\Framework\Pricing\Adjustment\Calculator |\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $calculatorMock;
  29. /**
  30. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $priceCurrencyMock;
  33. /**
  34. * Test setUp
  35. */
  36. protected function setUp()
  37. {
  38. $qty = 1;
  39. $this->saleableItemMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  40. $this->priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
  41. $this->calculatorMock = $this->createMock(\Magento\Framework\Pricing\Adjustment\Calculator::class);
  42. $this->saleableItemMock->expects($this->once())
  43. ->method('getPriceInfo')
  44. ->will($this->returnValue($this->priceInfoMock));
  45. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  46. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  47. $this->price = $objectManager->getObject(
  48. \Magento\Framework\Pricing\Test\Unit\Price\Stub::class,
  49. [
  50. 'saleableItem' => $this->saleableItemMock,
  51. 'quantity' => $qty,
  52. 'calculator' => $this->calculatorMock,
  53. 'priceCurrency' => $this->priceCurrencyMock,
  54. ]
  55. );
  56. }
  57. /**
  58. * Test method testGetDisplayValue
  59. */
  60. public function testGetAmount()
  61. {
  62. $priceValue = $this->price->getValue();
  63. $amountValue = 88;
  64. $this->calculatorMock->expects($this->once())
  65. ->method('getAmount')
  66. ->with($this->equalTo($priceValue))
  67. ->will($this->returnValue($amountValue));
  68. $this->assertEquals($amountValue, $this->price->getAmount());
  69. }
  70. /**
  71. * Test method getPriceType
  72. */
  73. public function testGetPriceCode()
  74. {
  75. $this->assertEquals(AbstractPrice::PRICE_CODE, $this->price->getPriceCode());
  76. }
  77. public function testGetCustomAmount()
  78. {
  79. $exclude = false;
  80. $amount = 21.0;
  81. $convertedValue = 30.25;
  82. $customAmount = 42.0;
  83. $this->priceCurrencyMock->expects($this->any())
  84. ->method('convertAndRound')
  85. ->with($amount)
  86. ->will($this->returnValue($convertedValue));
  87. $this->calculatorMock->expects($this->once())
  88. ->method('getAmount')
  89. ->with($convertedValue, $this->saleableItemMock, $exclude)
  90. ->will($this->returnValue($customAmount));
  91. $this->assertEquals($customAmount, $this->price->getCustomAmount($amount, $exclude));
  92. }
  93. public function testGetCustomAmountDefault()
  94. {
  95. $customAmount = 42.0;
  96. $this->calculatorMock->expects($this->once())
  97. ->method('getAmount')
  98. ->with($this->price->getValue(), $this->saleableItemMock, null)
  99. ->will($this->returnValue($customAmount));
  100. $this->assertEquals($customAmount, $this->price->getCustomAmount());
  101. }
  102. public function testGetQuantity()
  103. {
  104. $this->assertEquals(1, $this->price->getQuantity());
  105. }
  106. }