Нет описания

TestingAidsTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 TestingAidsTest extends TestFixture
  12. {
  13. public function testTestingAidsWithTestNowNotSet()
  14. {
  15. Carbon::setTestNow();
  16. $this->assertFalse(Carbon::hasTestNow());
  17. $this->assertNull(Carbon::getTestNow());
  18. }
  19. public function testTestingAidsWithTestNowSet()
  20. {
  21. $notNow = Carbon::yesterday();
  22. Carbon::setTestNow($notNow);
  23. $this->assertTrue(Carbon::hasTestNow());
  24. $this->assertSame($notNow, Carbon::getTestNow());
  25. }
  26. public function testConstructorWithTestValueSet()
  27. {
  28. $notNow = Carbon::yesterday();
  29. Carbon::setTestNow($notNow);
  30. $this->assertEquals($notNow, new Carbon());
  31. $this->assertEquals($notNow, new Carbon(null));
  32. $this->assertEquals($notNow, new Carbon(''));
  33. $this->assertEquals($notNow, new Carbon('now'));
  34. }
  35. public function testNowWithTestValueSet()
  36. {
  37. $notNow = Carbon::yesterday();
  38. Carbon::setTestNow($notNow);
  39. $this->assertEquals($notNow, Carbon::now());
  40. }
  41. public function testParseWithTestValueSet()
  42. {
  43. $notNow = Carbon::yesterday();
  44. Carbon::setTestNow($notNow);
  45. $this->assertEquals($notNow, Carbon::parse());
  46. $this->assertEquals($notNow, Carbon::parse(null));
  47. $this->assertEquals($notNow, Carbon::parse(''));
  48. $this->assertEquals($notNow, Carbon::parse('now'));
  49. }
  50. public function testParseRelativeWithTestValueSet()
  51. {
  52. $notNow = Carbon::parse('2013-09-01 05:15:05');
  53. Carbon::setTestNow($notNow);
  54. $this->assertSame('2013-09-01 05:10:05', Carbon::parse('5 minutes ago')->toDateTimeString());
  55. $this->assertSame('2013-08-25 05:15:05', Carbon::parse('1 week ago')->toDateTimeString());
  56. $this->assertSame('2013-09-02 00:00:00', Carbon::parse('tomorrow')->toDateTimeString());
  57. $this->assertSame('2013-08-31 00:00:00', Carbon::parse('yesterday')->toDateTimeString());
  58. $this->assertSame('2013-09-02 05:15:05', Carbon::parse('+1 day')->toDateTimeString());
  59. $this->assertSame('2013-08-31 05:15:05', Carbon::parse('-1 day')->toDateTimeString());
  60. $this->assertSame('2013-09-02 00:00:00', Carbon::parse('next monday')->toDateTimeString());
  61. $this->assertSame('2013-09-03 00:00:00', Carbon::parse('next tuesday')->toDateTimeString());
  62. $this->assertSame('2013-09-04 00:00:00', Carbon::parse('next wednesday')->toDateTimeString());
  63. $this->assertSame('2013-09-05 00:00:00', Carbon::parse('next thursday')->toDateTimeString());
  64. $this->assertSame('2013-09-06 00:00:00', Carbon::parse('next friday')->toDateTimeString());
  65. $this->assertSame('2013-09-07 00:00:00', Carbon::parse('next saturday')->toDateTimeString());
  66. $this->assertSame('2013-09-08 00:00:00', Carbon::parse('next sunday')->toDateTimeString());
  67. $this->assertSame('2013-08-26 00:00:00', Carbon::parse('last monday')->toDateTimeString());
  68. $this->assertSame('2013-08-27 00:00:00', Carbon::parse('last tuesday')->toDateTimeString());
  69. $this->assertSame('2013-08-28 00:00:00', Carbon::parse('last wednesday')->toDateTimeString());
  70. $this->assertSame('2013-08-29 00:00:00', Carbon::parse('last thursday')->toDateTimeString());
  71. $this->assertSame('2013-08-30 00:00:00', Carbon::parse('last friday')->toDateTimeString());
  72. $this->assertSame('2013-08-31 00:00:00', Carbon::parse('last saturday')->toDateTimeString());
  73. $this->assertSame('2013-08-25 00:00:00', Carbon::parse('last sunday')->toDateTimeString());
  74. $this->assertSame('2013-09-02 00:00:00', Carbon::parse('this monday')->toDateTimeString());
  75. $this->assertSame('2013-09-03 00:00:00', Carbon::parse('this tuesday')->toDateTimeString());
  76. $this->assertSame('2013-09-04 00:00:00', Carbon::parse('this wednesday')->toDateTimeString());
  77. $this->assertSame('2013-09-05 00:00:00', Carbon::parse('this thursday')->toDateTimeString());
  78. $this->assertSame('2013-09-06 00:00:00', Carbon::parse('this friday')->toDateTimeString());
  79. $this->assertSame('2013-09-07 00:00:00', Carbon::parse('this saturday')->toDateTimeString());
  80. $this->assertSame('2013-09-01 00:00:00', Carbon::parse('this sunday')->toDateTimeString());
  81. $this->assertSame('2013-10-01 05:15:05', Carbon::parse('first day of next month')->toDateTimeString());
  82. $this->assertSame('2013-09-30 05:15:05', Carbon::parse('last day of this month')->toDateTimeString());
  83. }
  84. public function testParseRelativeWithMinusSignsInDate()
  85. {
  86. $notNow = Carbon::parse('2013-09-01 05:15:05');
  87. Carbon::setTestNow($notNow);
  88. $this->assertSame('2000-01-03 00:00:00', Carbon::parse('2000-1-3')->toDateTimeString());
  89. $this->assertSame('2000-10-10 00:00:00', Carbon::parse('2000-10-10')->toDateTimeString());
  90. }
  91. public function testTimeZoneWithTestValueSet()
  92. {
  93. $notNow = Carbon::parse('2013-07-01 12:00:00', 'America/New_York');
  94. Carbon::setTestNow($notNow);
  95. $this->assertSame('2013-07-01T12:00:00-0400', Carbon::parse('now')->toIso8601String());
  96. $this->assertSame('2013-07-01T11:00:00-0500', Carbon::parse('now', 'America/Mexico_City')->toIso8601String());
  97. $this->assertSame('2013-07-01T09:00:00-0700', Carbon::parse('now', 'America/Vancouver')->toIso8601String());
  98. }
  99. }