Aucune description

StringsTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 MyCarbon extends Carbon
  12. {
  13. }
  14. class StringsTest extends TestFixture
  15. {
  16. public function testToString()
  17. {
  18. $d = Carbon::now();
  19. $this->assertSame(Carbon::now()->toDateTimeString(), ''.$d);
  20. }
  21. public function testSetToStringFormat()
  22. {
  23. Carbon::setToStringFormat('jS \o\f F, Y g:i:s a');
  24. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  25. $this->assertSame('25th of December, 1975 2:15:16 pm', ''.$d);
  26. }
  27. public function testResetToStringFormat()
  28. {
  29. $d = Carbon::now();
  30. Carbon::setToStringFormat('123');
  31. Carbon::resetToStringFormat();
  32. $this->assertSame($d->toDateTimeString(), ''.$d);
  33. }
  34. public function testExtendedClassToString()
  35. {
  36. $d = MyCarbon::now();
  37. $this->assertSame($d->toDateTimeString(), ''.$d);
  38. }
  39. public function testToDateString()
  40. {
  41. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  42. $this->assertSame('1975-12-25', $d->toDateString());
  43. }
  44. public function testToFormattedDateString()
  45. {
  46. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  47. $this->assertSame('Dec 25, 1975', $d->toFormattedDateString());
  48. }
  49. public function testToLocalizedFormattedDateString()
  50. {
  51. /****************
  52. Working out a Travis issue on how to set a different locale
  53. other than EN to test this.
  54. $cache = setlocale(LC_TIME, 0);
  55. setlocale(LC_TIME, 'German');
  56. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  57. $this->assertSame('Donnerstag 25 Dezember 1975', $d->formatLocalized('%A %d %B %Y'));
  58. setlocale(LC_TIME, $cache);
  59. *****************/
  60. }
  61. public function testToLocalizedFormattedTimezonedDateString()
  62. {
  63. $d = Carbon::create(1975, 12, 25, 14, 15, 16, 'Europe/London');
  64. $this->assertSame('Thursday 25 December 1975 14:15', $d->formatLocalized('%A %d %B %Y %H:%M'));
  65. }
  66. public function testToTimeString()
  67. {
  68. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  69. $this->assertSame('14:15:16', $d->toTimeString());
  70. }
  71. public function testToDateTimeString()
  72. {
  73. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  74. $this->assertSame('1975-12-25 14:15:16', $d->toDateTimeString());
  75. }
  76. public function testToDateTimeStringWithPaddedZeroes()
  77. {
  78. $d = Carbon::create(2000, 5, 2, 4, 3, 4);
  79. $this->assertSame('2000-05-02 04:03:04', $d->toDateTimeString());
  80. }
  81. public function testToDayDateTimeString()
  82. {
  83. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  84. $this->assertSame('Thu, Dec 25, 1975 2:15 PM', $d->toDayDateTimeString());
  85. }
  86. public function testToAtomString()
  87. {
  88. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  89. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toAtomString());
  90. }
  91. public function testToCOOKIEString()
  92. {
  93. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  94. if (\DateTime::COOKIE === 'l, d-M-y H:i:s T') {
  95. $cookieString = 'Thursday, 25-Dec-75 14:15:16 EST';
  96. } else {
  97. $cookieString = 'Thursday, 25-Dec-1975 14:15:16 EST';
  98. }
  99. $this->assertSame($cookieString, $d->toCOOKIEString());
  100. }
  101. public function testToIso8601String()
  102. {
  103. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  104. $this->assertSame('1975-12-25T14:15:16-0500', $d->toIso8601String());
  105. }
  106. public function testToRC822String()
  107. {
  108. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  109. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRfc822String());
  110. }
  111. public function testToRfc850String()
  112. {
  113. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  114. $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toRfc850String());
  115. }
  116. public function testToRfc1036String()
  117. {
  118. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  119. $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRfc1036String());
  120. }
  121. public function testToRfc1123String()
  122. {
  123. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  124. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRfc1123String());
  125. }
  126. public function testToRfc2822String()
  127. {
  128. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  129. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRfc2822String());
  130. }
  131. public function testToRfc3339String()
  132. {
  133. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  134. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toRfc3339String());
  135. }
  136. public function testToRssString()
  137. {
  138. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  139. $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRssString());
  140. }
  141. public function testToW3cString()
  142. {
  143. $d = Carbon::create(1975, 12, 25, 14, 15, 16);
  144. $this->assertSame('1975-12-25T14:15:16-05:00', $d->toW3cString());
  145. }
  146. }