DeltaPriceRoundTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\SalesRule\Test\Unit\Model;
  8. use Magento\Framework\Pricing\PriceCurrencyInterface;
  9. use Magento\SalesRule\Model\DeltaPriceRound;
  10. /**
  11. * Tests for Magento\SalesRule\Model\DeltaPriceRound.
  12. */
  13. class DeltaPriceRoundTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $priceCurrency;
  19. /**
  20. * @var DeltaPriceRound
  21. */
  22. private $model;
  23. /**
  24. * @inheritdoc
  25. */
  26. protected function setUp()
  27. {
  28. $this->priceCurrency = $this->getMockForAbstractClass(PriceCurrencyInterface::class);
  29. $this->priceCurrency->method('round')
  30. ->willReturnCallback(
  31. function ($amount) {
  32. return round($amount, 2);
  33. }
  34. );
  35. $this->model = new DeltaPriceRound($this->priceCurrency);
  36. }
  37. /**
  38. * Tests rounded price based on previous rounding operation delta.
  39. *
  40. * @param array $prices
  41. * @param array $roundedPrices
  42. * @return void
  43. * @dataProvider roundDataProvider
  44. */
  45. public function testRound(array $prices, array $roundedPrices): void
  46. {
  47. foreach ($prices as $key => $price) {
  48. $roundedPrice = $this->model->round($price, 'test');
  49. $this->assertEquals($roundedPrices[$key], $roundedPrice);
  50. }
  51. $this->model->reset('test');
  52. }
  53. /**
  54. * @return array
  55. */
  56. public function roundDataProvider(): array
  57. {
  58. return [
  59. [
  60. 'prices' => [1.004, 1.004],
  61. 'rounded prices' => [1.00, 1.01],
  62. ],
  63. [
  64. 'prices' => [1.005, 1.005],
  65. 'rounded prices' => [1.01, 1.0],
  66. ],
  67. ];
  68. }
  69. /**
  70. * @return void
  71. */
  72. public function testReset(): void
  73. {
  74. $this->assertEquals(1.44, $this->model->round(1.444, 'test'));
  75. $this->model->reset('test');
  76. $this->assertEquals(1.44, $this->model->round(1.444, 'test'));
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function testResetAll(): void
  82. {
  83. $this->assertEquals(1.44, $this->model->round(1.444, 'test1'));
  84. $this->assertEquals(1.44, $this->model->round(1.444, 'test2'));
  85. $this->model->resetAll();
  86. $this->assertEquals(1.44, $this->model->round(1.444, 'test1'));
  87. $this->assertEquals(1.44, $this->model->round(1.444, 'test2'));
  88. }
  89. }