CreateFromTimeTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\CarbonImmutable;
  12. use Carbon\CarbonImmutable as Carbon;
  13. use Carbon\Exceptions\InvalidFormatException;
  14. use DateTimeZone;
  15. use Tests\AbstractTestCase;
  16. class CreateFromTimeTest extends AbstractTestCase
  17. {
  18. public function testCreateWithTestNow()
  19. {
  20. Carbon::setTestNow($testNow = Carbon::create(2011, 1, 1, 12, 13, 14));
  21. $dt = Carbon::create(null, null, null, null, null, null);
  22. $this->assertCarbon($dt, 2011, 1, 1, 12, 13, 14);
  23. $this->assertTrue($testNow->eq($dt));
  24. }
  25. public function testCreateFromDateWithDefaults()
  26. {
  27. $d = Carbon::createFromTime();
  28. $this->assertSame($d->timestamp, Carbon::create(null, null, null, 0, 0, 0)->timestamp);
  29. }
  30. public function testCreateFromDateWithNull()
  31. {
  32. $d = Carbon::createFromTime(null, null, null);
  33. $this->assertSame($d->timestamp, Carbon::create(null, null, null, null, null, null)->timestamp);
  34. }
  35. public function testCreateFromDate()
  36. {
  37. $d = Carbon::createFromTime(23, 5, 21);
  38. $this->assertCarbon($d, Carbon::now()->year, Carbon::now()->month, Carbon::now()->day, 23, 5, 21);
  39. }
  40. public function testCreateFromTimeWithHour()
  41. {
  42. $d = Carbon::createFromTime(22);
  43. $this->assertSame(22, $d->hour);
  44. $this->assertSame(0, $d->minute);
  45. $this->assertSame(0, $d->second);
  46. }
  47. public function testCreateFromTimeWithMinute()
  48. {
  49. $d = Carbon::createFromTime(null, 5);
  50. $this->assertSame(5, $d->minute);
  51. }
  52. public function testCreateFromTimeWithSecond()
  53. {
  54. $d = Carbon::createFromTime(null, null, 21);
  55. $this->assertSame(21, $d->second);
  56. }
  57. public function testCreateFromTimeWithDateTimeZone()
  58. {
  59. $d = Carbon::createFromTime(12, 0, 0, new DateTimeZone('Europe/London'));
  60. $this->assertCarbon($d, Carbon::now('Europe/London')->year, Carbon::now('Europe/London')->month, Carbon::now('Europe/London')->day, 12, 0, 0);
  61. $this->assertSame('Europe/London', $d->tzName);
  62. }
  63. public function testCreateFromTimeWithTimeZoneString()
  64. {
  65. $d = Carbon::createFromTime(12, 0, 0, 'Europe/London');
  66. $this->assertCarbon($d, Carbon::now('Europe/London')->year, Carbon::now('Europe/London')->month, Carbon::now('Europe/London')->day, 12, 0, 0);
  67. $this->assertSame('Europe/London', $d->tzName);
  68. }
  69. public function testCreateFromTime()
  70. {
  71. // disable test for now
  72. // because we need Carbon::now() in Carbon::create() to work with given TZ
  73. $test = Carbon::getTestNow();
  74. Carbon::setTestNow();
  75. $tz = 'Etc/GMT+12';
  76. try {
  77. $now = Carbon::now($tz);
  78. } catch (InvalidFormatException $exception) {
  79. if ($exception->getMessage() !== 'Unknown or bad timezone (Etc/GMT+12)') {
  80. throw $exception;
  81. }
  82. $tz = 'GMT+12';
  83. $now = Carbon::now($tz);
  84. }
  85. $dt = Carbon::createFromTime($now->hour, $now->minute, $now->second, $tz);
  86. // re-enable test
  87. Carbon::setTestNow($test);
  88. // tested without microseconds
  89. // because they appear withing calls to Carbon
  90. $this->assertSame($now->format('c'), $dt->format('c'));
  91. }
  92. }