CreateFromTimeStringTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 DateTimeZone;
  14. use Tests\AbstractTestCase;
  15. class CreateFromTimeStringTest extends AbstractTestCase
  16. {
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. Carbon::setTestNow();
  21. }
  22. public function testCreateFromTimeString()
  23. {
  24. $d = Carbon::createFromTimeString('22:45');
  25. $this->assertSame(22, $d->hour);
  26. $this->assertSame(45, $d->minute);
  27. $this->assertSame(0, $d->second);
  28. $this->assertSame(0, $d->micro);
  29. }
  30. public function testCreateFromTimeStringWithSecond()
  31. {
  32. $d = Carbon::createFromTimeString('22:45:12');
  33. $this->assertSame(22, $d->hour);
  34. $this->assertSame(45, $d->minute);
  35. $this->assertSame(12, $d->second);
  36. $this->assertSame(0, $d->micro);
  37. }
  38. public function testCreateFromTimeStringWithMicroSecond()
  39. {
  40. $d = Carbon::createFromTimeString('22:45:00.625341');
  41. $this->assertSame(22, $d->hour);
  42. $this->assertSame(45, $d->minute);
  43. $this->assertSame(0, $d->second);
  44. $this->assertSame(625341, $d->micro);
  45. }
  46. public function testCreateFromTimeStringWithDateTimeZone()
  47. {
  48. $d = Carbon::createFromTimeString('12:20:30', new DateTimeZone('Europe/London'));
  49. $this->assertCarbonTime($d, 12, 20, 30, 0);
  50. $this->assertSame('Europe/London', $d->tzName);
  51. }
  52. public function testCreateFromTimeStringWithTimeZoneString()
  53. {
  54. $d = Carbon::createFromTimeString('12:20:30', 'Europe/London');
  55. $this->assertCarbonTime($d, 12, 20, 30, 0);
  56. $this->assertSame('Europe/London', $d->tzName);
  57. }
  58. }