StringsTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 StringsTest extends TestFixture
  12. {
  13. public function testToString()
  14. {
  15. $d = Carbon::now();
  16. $this->assertSame(Carbon::now()->toDateTimeString(), ''.$d);
  17. }
  18. public function testToDateString()
  19. {
  20. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  21. $this->assertSame('1975-12-25', $d->toDateString());
  22. }
  23. public function testToFormattedDateString()
  24. {
  25. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  26. $this->assertSame('Dec 25, 1975', $d->toFormattedDateString());
  27. }
  28. public function testToLocalizedFormattedDateString()
  29. {
  30. /****************
  31. Working out a Travis issue on how to set a different locale
  32. other than EN to test this.
  33. $cache = setlocale(LC_TIME, 0);
  34. setlocale(LC_TIME, 'German');
  35. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  36. $this->assertSame('Donnerstag 25 Dezember 1975', $d->formatLocalized('%A %d %B %Y'));
  37. setlocale(LC_TIME, $cache);
  38. *****************/
  39. }
  40. public function testToTimeString()
  41. {
  42. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  43. $this->assertSame('14:15:16', $d->toTimeString());
  44. }
  45. public function testToDateTimeString()
  46. {
  47. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  48. $this->assertSame('1975-12-25 14:15:16', $d->toDateTimeString());
  49. }
  50. public function testToDateTimeStringWithPaddedZeroes()
  51. {
  52. $d = Carbon::create(2000, 5, 2, 4, 3, 4);
  53. $this->assertSame('2000-05-02 04:03:04', $d->toDateTimeString());
  54. }
  55. public function testToDayDateTimeString()
  56. {
  57. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  58. $this->assertSame('Thu, Dec 25, 1975 2:15 PM', $d->toDayDateTimeString());
  59. }
  60. public function testToATOMString()
  61. {
  62. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  63. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toATOMString());
  64. }
  65. public function testToCOOKIEString()
  66. {
  67. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  68. $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toCOOKIEString());
  69. }
  70. public function testToISO8601String()
  71. {
  72. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  73. $this->assertSame('1975-12-25T14:15:16-0500', $d->toISO8601String());
  74. }
  75. public function testToRC822String()
  76. {
  77. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  78. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRFC822String());
  79. }
  80. public function testToRFC850String()
  81. {
  82. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  83. $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toRFC850String());
  84. }
  85. public function testToRFC1036String()
  86. {
  87. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  88. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRFC1036String());
  89. }
  90. public function testToRFC1123String()
  91. {
  92. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  93. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRFC1123String());
  94. }
  95. public function testToRFC2822String()
  96. {
  97. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  98. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRFC2822String());
  99. }
  100. public function testToRFC3339String()
  101. {
  102. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  103. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toRFC3339String());
  104. }
  105. public function testToRSSString()
  106. {
  107. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  108. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRSSString());
  109. }
  110. public function testToW3CString()
  111. {
  112. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  113. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toW3CString());
  114. }
  115. }