SpecialPriceTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Unit\Pricing\Price;
  7. use \Magento\Bundle\Pricing\Price\SpecialPrice;
  8. use Magento\Store\Api\Data\WebsiteInterface;
  9. class SpecialPriceTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var SpecialPrice
  13. */
  14. protected $model;
  15. /**
  16. * @var \Magento\Framework\Pricing\SaleableInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $saleable;
  19. /**
  20. * @var \Magento\Framework\Pricing\PriceInfo\Base |\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $priceInfo;
  23. /**
  24. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $localeDate;
  27. /**
  28. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $priceCurrencyMock;
  31. protected function setUp()
  32. {
  33. $this->saleable = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->localeDate = $this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
  37. $this->priceInfo = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
  38. $this->saleable->expects($this->once())
  39. ->method('getPriceInfo')
  40. ->will($this->returnValue($this->priceInfo));
  41. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  42. $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  43. $this->model = $objectHelper->getObject(
  44. \Magento\Bundle\Pricing\Price\SpecialPrice::class,
  45. [
  46. 'saleableItem' => $this->saleable,
  47. 'localeDate' => $this->localeDate,
  48. 'priceCurrency' => $this->priceCurrencyMock
  49. ]
  50. );
  51. }
  52. /**
  53. * @param $regularPrice
  54. * @param $specialPrice
  55. * @param $isScopeDateInInterval
  56. * @param $value
  57. * @param $percent
  58. * @dataProvider getValueDataProvider
  59. */
  60. public function testGetValue($regularPrice, $specialPrice, $isScopeDateInInterval, $value, $percent)
  61. {
  62. $specialFromDate = 'some date from';
  63. $specialToDate = 'some date to';
  64. $this->saleable->expects($this->once())
  65. ->method('getSpecialPrice')
  66. ->will($this->returnValue($specialPrice));
  67. $this->saleable->expects($this->once())
  68. ->method('getSpecialFromDate')
  69. ->will($this->returnValue($specialFromDate));
  70. $this->saleable->expects($this->once())
  71. ->method('getSpecialToDate')
  72. ->will($this->returnValue($specialToDate));
  73. $this->localeDate->expects($this->once())
  74. ->method('isScopeDateInInterval')
  75. ->with(WebsiteInterface::ADMIN_CODE, $specialFromDate, $specialToDate)
  76. ->will($this->returnValue($isScopeDateInInterval));
  77. $this->priceCurrencyMock->expects($this->never())
  78. ->method('convertAndRound');
  79. if ($isScopeDateInInterval) {
  80. $price = $this->createMock(\Magento\Framework\Pricing\Price\PriceInterface::class);
  81. $this->priceInfo->expects($this->once())
  82. ->method('getPrice')
  83. ->with(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)
  84. ->will($this->returnValue($price));
  85. $price->expects($this->once())
  86. ->method('getValue')
  87. ->will($this->returnValue($regularPrice));
  88. }
  89. $this->assertEquals($value, $this->model->getValue());
  90. //check that the second call will get data from cache the same as in first call
  91. $this->assertEquals($value, $this->model->getValue());
  92. $this->assertEquals($percent, $this->model->getDiscountPercent());
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function getValueDataProvider()
  98. {
  99. return [
  100. ['regularPrice' => 100, 'specialPrice' => 40, 'isScopeDateInInterval' => true, 'value' => 40,
  101. 'percent' => 40, ],
  102. ['regularPrice' => 75, 'specialPrice' => 40, 'isScopeDateInInterval' => true, 'value' => 30,
  103. 'percent' => 40],
  104. ['regularPrice' => 75, 'specialPrice' => 40, 'isScopeDateInInterval' => false, 'value' => false,
  105. 'percent' => null],
  106. ];
  107. }
  108. }