RoundingTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonInterval;
  12. use Carbon\CarbonInterval;
  13. use InvalidArgumentException;
  14. use Tests\AbstractTestCase;
  15. class RoundingTest extends AbstractTestCase
  16. {
  17. public function testThrowsExceptionForCompositeInterval()
  18. {
  19. $this->expectExceptionObject(new InvalidArgumentException(
  20. 'Rounding is only possible with single unit intervals.'
  21. ));
  22. CarbonInterval::days(2)->round('2 hours 50 minutes');
  23. }
  24. public function testFloor()
  25. {
  26. $this->assertSame(21, CarbonInterval::days(21)->floorWeeks()->totalDays);
  27. $this->assertSame(21, CarbonInterval::days(24)->floorWeeks()->totalDays);
  28. $this->assertSame(21, CarbonInterval::days(25)->floorWeeks()->totalDays);
  29. $this->assertSame(21, CarbonInterval::days(27)->floorWeeks()->totalDays);
  30. $this->assertSame(28, CarbonInterval::days(28)->floorWeeks()->totalDays);
  31. $this->assertSame(1000, CarbonInterval::milliseconds(1234)->floor()->totalMilliseconds);
  32. $this->assertSame(1000, CarbonInterval::milliseconds(1834)->floor()->totalMilliseconds);
  33. $this->assertSame(20, CarbonInterval::days(21)->floor('2 days')->totalDays);
  34. $this->assertSame(18, CarbonInterval::days(21)->floor(CarbonInterval::days(6))->totalDays);
  35. $this->assertSame(18, CarbonInterval::days(22)->floorUnit('day', 6)->totalDays);
  36. }
  37. public function testRound()
  38. {
  39. $this->assertSame(21, CarbonInterval::days(21)->roundWeeks()->totalDays);
  40. $this->assertSame(21, CarbonInterval::days(24)->roundWeeks()->totalDays);
  41. $this->assertSame(28, CarbonInterval::days(25)->roundWeeks()->totalDays);
  42. $this->assertSame(28, CarbonInterval::days(27)->roundWeeks()->totalDays);
  43. $this->assertSame(28, CarbonInterval::days(28)->roundWeeks()->totalDays);
  44. $this->assertSame(-7, CarbonInterval::make('7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays);
  45. $this->assertSame(-7, CarbonInterval::make('-7 days 23 hours 34 minutes')->roundWeeks()->totalDays);
  46. $this->assertSame(7, CarbonInterval::make('-7 days 23 hours 34 minutes')->invert()->roundWeeks()->totalDays);
  47. $this->assertSame(1000, CarbonInterval::milliseconds(1234)->round()->totalMilliseconds);
  48. $this->assertSame(2000, CarbonInterval::milliseconds(1834)->round()->totalMilliseconds);
  49. $this->assertSame(20, CarbonInterval::days(20)->round('2 days')->totalDays);
  50. $this->assertSame(18, CarbonInterval::days(20)->round(CarbonInterval::days(6))->totalDays);
  51. $this->assertSame(22, CarbonInterval::days(21)->round('2 days')->totalDays);
  52. $this->assertSame(24, CarbonInterval::days(21)->round(CarbonInterval::days(6))->totalDays);
  53. $this->assertSame(22, CarbonInterval::days(22)->round('2 days')->totalDays);
  54. $this->assertSame(24, CarbonInterval::days(22)->round(CarbonInterval::days(6))->totalDays);
  55. $this->assertSame(24, CarbonInterval::days(22)->roundUnit('day', 6)->totalDays);
  56. }
  57. public function testTotalAfterRound()
  58. {
  59. $this->assertSame(19, CarbonInterval::make('43h3m6s')->roundMinutes()->hours);
  60. $this->assertSame(43.05, CarbonInterval::make('43h3m6s')->roundMinutes()->totalHours);
  61. }
  62. public function testWithCascadeFactors()
  63. {
  64. $cascades = CarbonInterval::getCascadeFactors();
  65. CarbonInterval::setCascadeFactors([
  66. 'millisecond' => [1000, 'microseconds'],
  67. 'second' => [1000, 'milliseconds'],
  68. 'minute' => [60, 'seconds'],
  69. 'hour' => [60, 'minutes'],
  70. ]);
  71. $this->assertEqualsWithDelta(
  72. 43.166666666666664,
  73. CarbonInterval::make('43h3m6s')
  74. ->ceilMinutes(10)
  75. ->totalHours,
  76. 0.00000001
  77. );
  78. $this->assertSame(
  79. 43,
  80. CarbonInterval::make('43h3m6s')
  81. ->floorMinutes(6)
  82. ->totalHours
  83. );
  84. $this->assertSame(
  85. 43.05,
  86. CarbonInterval::make('43h3m6s')
  87. ->roundMinutes()
  88. ->totalHours
  89. );
  90. $this->assertEqualsWithDelta(
  91. 43.05833333333333,
  92. CarbonInterval::make('43h3m26s')
  93. ->roundMinutes(0.5)
  94. ->totalHours,
  95. 0.00000001
  96. );
  97. $this->assertSame(
  98. -43.05,
  99. CarbonInterval::make('43h3m6s')
  100. ->invert()
  101. ->roundMinutes(0.5)
  102. ->totalHours
  103. );
  104. CarbonInterval::setCascadeFactors($cascades);
  105. }
  106. public function testCeil()
  107. {
  108. $this->assertSame(21, CarbonInterval::days(21)->ceilWeeks()->totalDays);
  109. $this->assertSame(28, CarbonInterval::days(24)->ceilWeeks()->totalDays);
  110. $this->assertSame(28, CarbonInterval::days(25)->ceilWeeks()->totalDays);
  111. $this->assertSame(28, CarbonInterval::days(27)->ceilWeeks()->totalDays);
  112. $this->assertSame(28, CarbonInterval::days(28)->ceilWeeks()->totalDays);
  113. $this->assertSame(2000, CarbonInterval::milliseconds(1234)->ceil()->totalMilliseconds);
  114. $this->assertSame(2000, CarbonInterval::milliseconds(1834)->ceil()->totalMilliseconds);
  115. $this->assertSame(20, CarbonInterval::days(20)->ceil('2 days')->totalDays);
  116. $this->assertSame(24, CarbonInterval::days(20)->ceil(CarbonInterval::days(6))->totalDays);
  117. $this->assertSame(22, CarbonInterval::days(21)->ceil('2 days')->totalDays);
  118. $this->assertSame(24, CarbonInterval::days(21)->ceil(CarbonInterval::days(6))->totalDays);
  119. $this->assertSame(22, CarbonInterval::days(22)->ceil('2 days')->totalDays);
  120. $this->assertSame(24, CarbonInterval::days(22)->ceil(CarbonInterval::days(6))->totalDays);
  121. $this->assertSame(24, CarbonInterval::days(22)->ceilUnit('day', 6)->totalDays);
  122. }
  123. }