ConstructTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 ConstructTest extends TestFixture
  12. {
  13. public function testCreatesAnInstanceDefaultToNow()
  14. {
  15. $c = new Carbon();
  16. $now = Carbon::now();
  17. $this->assertEquals('Carbon\Carbon', get_class($c));
  18. $this->assertEquals($now->tzName, $c->tzName);
  19. $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second);
  20. }
  21. public function testParseCreatesAnInstanceDefaultToNow()
  22. {
  23. $c = Carbon::parse();
  24. $now = Carbon::now();
  25. $this->assertEquals('Carbon\Carbon', get_class($c));
  26. $this->assertEquals($now->tzName, $c->tzName);
  27. $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second);
  28. }
  29. public function testWithFancyString()
  30. {
  31. $c = new Carbon('first day of January 2008');
  32. $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
  33. }
  34. public function testParseWithFancyString()
  35. {
  36. $c = Carbon::parse('first day of January 2008');
  37. $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
  38. }
  39. public function testDefaultTimezone()
  40. {
  41. $c = new Carbon('now');
  42. $this->assertSame('America/Toronto', $c->tzName);
  43. }
  44. public function testParseWithDefaultTimezone()
  45. {
  46. $c = Carbon::parse('now');
  47. $this->assertSame('America/Toronto', $c->tzName);
  48. }
  49. public function testSettingTimezone()
  50. {
  51. $c = new Carbon('now', new \DateTimeZone('Europe/London'));
  52. $this->assertSame('Europe/London', $c->tzName);
  53. $this->assertSame(1, $c->offsetHours);
  54. }
  55. public function testParseSettingTimezone()
  56. {
  57. $c = Carbon::parse('now', new \DateTimeZone('Europe/London'));
  58. $this->assertSame('Europe/London', $c->tzName);
  59. $this->assertSame(1, $c->offsetHours);
  60. }
  61. public function testSettingTimezoneWithString()
  62. {
  63. $c = new Carbon('now', 'Asia/Tokyo');
  64. $this->assertSame('Asia/Tokyo', $c->tzName);
  65. $this->assertSame(9, $c->offsetHours);
  66. }
  67. public function testParseSettingTimezoneWithString()
  68. {
  69. $c = Carbon::parse('now', 'Asia/Tokyo');
  70. $this->assertSame('Asia/Tokyo', $c->tzName);
  71. $this->assertSame(9, $c->offsetHours);
  72. }
  73. }