No Description

ComparisonTest.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\Carbon;
  11. class ComparisonTest extends TestFixture
  12. {
  13. public function testEqualToTrue()
  14. {
  15. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->eq(Carbon::createFromDate(2000, 1, 1)));
  16. }
  17. public function testEqualToFalse()
  18. {
  19. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->eq(Carbon::createFromDate(2000, 1, 2)));
  20. }
  21. public function testEqualWithTimezoneTrue()
  22. {
  23. $this->assertTrue(Carbon::create(2000, 1, 1, 12, 0, 0, 'America/Toronto')->eq(Carbon::create(2000, 1, 1, 9, 0, 0, 'America/Vancouver')));
  24. }
  25. public function testEqualWithTimezoneFalse()
  26. {
  27. $this->assertFalse(Carbon::createFromDate(2000, 1, 1, 'America/Toronto')->eq(Carbon::createFromDate(2000, 1, 1, 'America/Vancouver')));
  28. }
  29. public function testNotEqualToTrue()
  30. {
  31. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->ne(Carbon::createFromDate(2000, 1, 2)));
  32. }
  33. public function testNotEqualToFalse()
  34. {
  35. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->ne(Carbon::createFromDate(2000, 1, 1)));
  36. }
  37. public function testNotEqualWithTimezone()
  38. {
  39. $this->assertTrue(Carbon::createFromDate(2000, 1, 1, 'America/Toronto')->ne(Carbon::createFromDate(2000, 1, 1, 'America/Vancouver')));
  40. }
  41. public function testGreaterThanTrue()
  42. {
  43. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->gt(Carbon::createFromDate(1999, 12, 31)));
  44. }
  45. public function testGreaterThanFalse()
  46. {
  47. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->gt(Carbon::createFromDate(2000, 1, 2)));
  48. }
  49. public function testGreaterThanWithTimezoneTrue()
  50. {
  51. $dt1 = Carbon::create(2000, 1, 1, 12, 0, 0, 'America/Toronto');
  52. $dt2 = Carbon::create(2000, 1, 1, 8, 59, 59, 'America/Vancouver');
  53. $this->assertTrue($dt1->gt($dt2));
  54. }
  55. public function testGreaterThanWithTimezoneFalse()
  56. {
  57. $dt1 = Carbon::create(2000, 1, 1, 12, 0, 0, 'America/Toronto');
  58. $dt2 = Carbon::create(2000, 1, 1, 9, 0, 1, 'America/Vancouver');
  59. $this->assertFalse($dt1->gt($dt2));
  60. }
  61. public function testGreaterThanOrEqualTrue()
  62. {
  63. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->gte(Carbon::createFromDate(1999, 12, 31)));
  64. }
  65. public function testGreaterThanOrEqualTrueEqual()
  66. {
  67. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->gte(Carbon::createFromDate(2000, 1, 1)));
  68. }
  69. public function testGreaterThanOrEqualFalse()
  70. {
  71. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->gte(Carbon::createFromDate(2000, 1, 2)));
  72. }
  73. public function testLessThanTrue()
  74. {
  75. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->lt(Carbon::createFromDate(2000, 1, 2)));
  76. }
  77. public function testLessThanFalse()
  78. {
  79. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->lt(Carbon::createFromDate(1999, 12, 31)));
  80. }
  81. public function testLessThanOrEqualTrue()
  82. {
  83. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->lte(Carbon::createFromDate(2000, 1, 2)));
  84. }
  85. public function testLessThanOrEqualTrueEqual()
  86. {
  87. $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->lte(Carbon::createFromDate(2000, 1, 1)));
  88. }
  89. public function testLessThanOrEqualFalse()
  90. {
  91. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->lte(Carbon::createFromDate(1999, 12, 31)));
  92. }
  93. public function testBetweenEqualTrue()
  94. {
  95. $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), true));
  96. }
  97. public function testBetweenNotEqualTrue()
  98. {
  99. $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), false));
  100. }
  101. public function testBetweenEqualFalse()
  102. {
  103. $this->assertFalse(Carbon::createFromDate(1999, 12, 31)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), true));
  104. }
  105. public function testBetweenNotEqualFalse()
  106. {
  107. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), false));
  108. }
  109. public function testBetweenEqualSwitchTrue()
  110. {
  111. $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), true));
  112. }
  113. public function testBetweenNotEqualSwitchTrue()
  114. {
  115. $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), false));
  116. }
  117. public function testBetweenEqualSwitchFalse()
  118. {
  119. $this->assertFalse(Carbon::createFromDate(1999, 12, 31)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), true));
  120. }
  121. public function testBetweenNotEqualSwitchFalse()
  122. {
  123. $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), false));
  124. }
  125. public function testMinIsFluid()
  126. {
  127. $dt = Carbon::now();
  128. $this->assertTrue($dt->min() instanceof Carbon);
  129. }
  130. public function testMinWithNow()
  131. {
  132. $dt = Carbon::create(2012, 1, 1, 0, 0, 0)->min();
  133. $this->assertCarbon($dt, 2012, 1, 1, 0, 0, 0);
  134. }
  135. public function testMinWithInstance()
  136. {
  137. $dt1 = Carbon::create(2013, 12, 31, 23, 59, 59);
  138. $dt2 = Carbon::create(2012, 1, 1, 0, 0, 0)->min($dt1);
  139. $this->assertCarbon($dt2, 2012, 1, 1, 0, 0, 0);
  140. }
  141. public function testMaxIsFluid()
  142. {
  143. $dt = Carbon::now();
  144. $this->assertTrue($dt->max() instanceof Carbon);
  145. }
  146. public function testMaxWithNow()
  147. {
  148. $dt = Carbon::create(2099, 12, 31, 23, 59, 59)->max();
  149. $this->assertCarbon($dt, 2099, 12, 31, 23, 59, 59);
  150. }
  151. public function testMaxWithInstance()
  152. {
  153. $dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);
  154. $dt2 = Carbon::create(2099, 12, 31, 23, 59, 59)->max($dt1);
  155. $this->assertCarbon($dt2, 2099, 12, 31, 23, 59, 59);
  156. }
  157. public function testIsBirthday()
  158. {
  159. $dt1 = Carbon::createFromDate(1987, 4, 23);
  160. $dt2 = Carbon::createFromDate(2014, 9, 26);
  161. $dt3 = Carbon::createFromDate(2014, 4, 23);
  162. $this->assertFalse($dt2->isBirthday($dt1));
  163. $this->assertTrue($dt3->isBirthday($dt1));
  164. }
  165. }