CreateTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 CreateTest extends TestFixture
  12. {
  13. public function testCreateReturnsDatingInstance()
  14. {
  15. $d = Carbon::create();
  16. $this->assertTrue($d instanceof Carbon);
  17. }
  18. public function testCreateWithDefaults()
  19. {
  20. $d = Carbon::create();
  21. $this->assertSame($d->timestamp, Carbon::now()->timestamp);
  22. }
  23. public function testCreateWithYear()
  24. {
  25. $d = Carbon::create(2012);
  26. $this->assertSame(2012, $d->year);
  27. }
  28. public function testCreateWithInvalidYear()
  29. {
  30. $this->setExpectedException('InvalidArgumentException');
  31. $d = Carbon::create(-3);
  32. }
  33. public function testCreateWithMonth()
  34. {
  35. $d = Carbon::create(null, 3);
  36. $this->assertSame(3, $d->month);
  37. }
  38. public function testCreateWithInvalidMonth()
  39. {
  40. $this->setExpectedException('InvalidArgumentException');
  41. $d = Carbon::create(null, -5);
  42. }
  43. public function testCreateMonthWraps()
  44. {
  45. $d = Carbon::create(2011, 0, 1, 0, 0, 0);
  46. $this->assertCarbon($d, 2010, 12, 1, 0, 0, 0);
  47. }
  48. public function testCreateWithDay()
  49. {
  50. $d = Carbon::create(null, null, 21);
  51. $this->assertSame(21, $d->day);
  52. }
  53. public function testCreateWithInvalidDay()
  54. {
  55. $this->setExpectedException('InvalidArgumentException');
  56. $d = Carbon::create(null, null, -4);
  57. }
  58. public function testCreateDayWraps()
  59. {
  60. $d = Carbon::create(2011, 1, 40, 0, 0, 0);
  61. $this->assertCarbon($d, 2011, 2, 9, 0, 0, 0);
  62. }
  63. public function testCreateWithHourAndDefaultMinSecToZero()
  64. {
  65. $d = Carbon::create(null, null, null, 14);
  66. $this->assertSame(14, $d->hour);
  67. $this->assertSame(0, $d->minute);
  68. $this->assertSame(0, $d->second);
  69. }
  70. public function testCreateWithInvalidHour()
  71. {
  72. $this->setExpectedException('InvalidArgumentException');
  73. $d = Carbon::create(null, null, null, -1);
  74. }
  75. public function testCreateHourWraps()
  76. {
  77. $d = Carbon::create(2011, 1, 1, 24, 0, 0);
  78. $this->assertCarbon($d, 2011, 1, 2, 0, 0, 0);
  79. }
  80. public function testCreateWithMinute()
  81. {
  82. $d = Carbon::create(null, null, null, null, 58);
  83. $this->assertSame(58, $d->minute);
  84. }
  85. public function testCreateWithInvalidMinute()
  86. {
  87. $this->setExpectedException('InvalidArgumentException');
  88. $d = Carbon::create(2011, 1, 1, 0, -2, 0);
  89. }
  90. public function testCreateMinuteWraps()
  91. {
  92. $d = Carbon::create(2011, 1, 1, 0, 62, 0);
  93. $this->assertCarbon($d, 2011, 1, 1, 1, 2, 0);
  94. }
  95. public function testCreateWithSecond()
  96. {
  97. $d = Carbon::create(null, null, null, null, null, 59);
  98. $this->assertSame(59, $d->second);
  99. }
  100. public function testCreateWithInvalidSecond()
  101. {
  102. $this->setExpectedException('InvalidArgumentException');
  103. $d = Carbon::create(null, null, null, null, null, -2);
  104. }
  105. public function testCreateSecondsWrap()
  106. {
  107. $d = Carbon::create(2012, 1, 1, 0, 0, 61);
  108. $this->assertCarbon($d, 2012, 1, 1, 0, 1, 1);
  109. }
  110. public function testCreateWithDateTimeZone()
  111. {
  112. $d = Carbon::create(2012, 1, 1, 0, 0, 0, new \DateTimeZone('Europe/London'));
  113. $this->assertCarbon($d, 2012, 1, 1, 0, 0, 0);
  114. $this->assertSame('Europe/London', $d->tzName);
  115. }
  116. public function testCreateWithTimeZoneString()
  117. {
  118. $d = Carbon::create(2012, 1, 1, 0, 0, 0, 'Europe/London');
  119. $this->assertCarbon($d, 2012, 1, 1, 0, 0, 0);
  120. $this->assertSame('Europe/London', $d->tzName);
  121. }
  122. }