CreateTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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\InvalidTimeZoneException;
  14. use DateTime;
  15. use DateTimeZone;
  16. use InvalidArgumentException;
  17. use Tests\AbstractTestCase;
  18. class CreateTest extends AbstractTestCase
  19. {
  20. public function testCreateReturnsDatingInstance()
  21. {
  22. $d = Carbon::create();
  23. $this->assertInstanceOfCarbon($d);
  24. }
  25. public function testCreateWithDefaults()
  26. {
  27. $d = Carbon::create();
  28. $this->assertSame($d->getTimestamp(), Carbon::create('0000-01-01 00:00:00')->getTimestamp());
  29. }
  30. public function testCreateWithNull()
  31. {
  32. $d = Carbon::create(null, null, null, null, null, null);
  33. $this->assertSame($d->getTimestamp(), Carbon::now()->getTimestamp());
  34. }
  35. public function testCreateAsParseAlias()
  36. {
  37. $d = Carbon::create('2019-02-05 12:30:06.99', 'Asia/Tokyo');
  38. $this->assertSame('2019-02-05 12:30:06.990000 Asia/Tokyo', $d->format('Y-m-d H:i:s.u e'));
  39. }
  40. public function testCreateWithYear()
  41. {
  42. $d = Carbon::create(2012);
  43. $this->assertSame(2012, $d->year);
  44. }
  45. public function testCreateHandlesNegativeYear()
  46. {
  47. $c = Carbon::create(-1, 10, 12, 1, 2, 3);
  48. $this->assertCarbon($c, -1, 10, 12, 1, 2, 3);
  49. }
  50. public function testCreateHandlesFiveDigitsPositiveYears()
  51. {
  52. $c = Carbon::create(999999999, 10, 12, 1, 2, 3);
  53. $this->assertCarbon($c, 999999999, 10, 12, 1, 2, 3);
  54. }
  55. public function testCreateHandlesFiveDigitsNegativeYears()
  56. {
  57. $c = Carbon::create(-999999999, 10, 12, 1, 2, 3);
  58. $this->assertCarbon($c, -999999999, 10, 12, 1, 2, 3);
  59. }
  60. public function testCreateWithMonth()
  61. {
  62. $d = Carbon::create(null, 3);
  63. $this->assertSame(3, $d->month);
  64. }
  65. public function testCreateWithInvalidMonth()
  66. {
  67. $this->expectExceptionObject(new InvalidArgumentException(
  68. 'month must be between 0 and 99, -5 given'
  69. ));
  70. Carbon::create(null, -5);
  71. }
  72. public function testCreateMonthWraps()
  73. {
  74. $d = Carbon::create(2011, 0, 1, 0, 0, 0);
  75. $this->assertCarbon($d, 2010, 12, 1, 0, 0, 0);
  76. }
  77. public function testCreateWithDay()
  78. {
  79. $d = Carbon::create(null, null, 21);
  80. $this->assertSame(21, $d->day);
  81. }
  82. public function testCreateWithInvalidDay()
  83. {
  84. $this->expectExceptionObject(new InvalidArgumentException(
  85. 'day must be between 0 and 99, -4 given'
  86. ));
  87. Carbon::create(null, null, -4);
  88. }
  89. public function testCreateDayWraps()
  90. {
  91. $d = Carbon::create(2011, 1, 40, 0, 0, 0);
  92. $this->assertCarbon($d, 2011, 2, 9, 0, 0, 0);
  93. }
  94. public function testCreateWithHourAndDefaultMinSecToZero()
  95. {
  96. $d = Carbon::create(null, null, null, 14);
  97. $this->assertSame(14, $d->hour);
  98. $this->assertSame(0, $d->minute);
  99. $this->assertSame(0, $d->second);
  100. }
  101. public function testCreateWithInvalidHour()
  102. {
  103. $this->expectExceptionObject(new InvalidArgumentException(
  104. 'hour must be between 0 and 99, -1 given'
  105. ));
  106. Carbon::create(null, null, null, -1);
  107. }
  108. public function testCreateHourWraps()
  109. {
  110. $d = Carbon::create(2011, 1, 1, 24, 0, 0);
  111. $this->assertCarbon($d, 2011, 1, 2, 0, 0, 0);
  112. }
  113. public function testCreateWithMinute()
  114. {
  115. $d = Carbon::create(null, null, null, null, 58);
  116. $this->assertSame(58, $d->minute);
  117. }
  118. public function testCreateWithInvalidMinute()
  119. {
  120. $this->expectExceptionObject(new InvalidArgumentException(
  121. 'minute must be between 0 and 99, -2 given'
  122. ));
  123. Carbon::create(2011, 1, 1, 0, -2, 0);
  124. }
  125. public function testCreateMinuteWraps()
  126. {
  127. $d = Carbon::create(2011, 1, 1, 0, 62, 0);
  128. $this->assertCarbon($d, 2011, 1, 1, 1, 2, 0);
  129. }
  130. public function testCreateWithSecond()
  131. {
  132. $d = Carbon::create(null, null, null, null, null, 59);
  133. $this->assertSame(59, $d->second);
  134. }
  135. public function testCreateWithInvalidSecond()
  136. {
  137. $this->expectExceptionObject(new InvalidArgumentException(
  138. 'second must be between 0 and 99, -2 given'
  139. ));
  140. Carbon::create(null, null, null, null, null, -2);
  141. }
  142. public function testCreateSecondsWrap()
  143. {
  144. $d = Carbon::create(2012, 1, 1, 0, 0, 61);
  145. $this->assertCarbon($d, 2012, 1, 1, 0, 1, 1);
  146. }
  147. public function testCreateWithDateTimeZone()
  148. {
  149. $d = Carbon::create(2012, 1, 1, 0, 0, 0, new DateTimeZone('Europe/London'));
  150. $this->assertCarbon($d, 2012, 1, 1, 0, 0, 0);
  151. $this->assertSame('Europe/London', $d->tzName);
  152. }
  153. public function testCreateWithTimeZoneString()
  154. {
  155. $d = Carbon::create(2012, 1, 1, 0, 0, 0, 'Europe/London');
  156. $this->assertCarbon($d, 2012, 1, 1, 0, 0, 0);
  157. $this->assertSame('Europe/London', $d->tzName);
  158. }
  159. public function testMake()
  160. {
  161. $this->assertCarbon(Carbon::make('2017-01-05'), 2017, 1, 5, 0, 0, 0);
  162. $this->assertCarbon(Carbon::make(new DateTime('2017-01-05')), 2017, 1, 5, 0, 0, 0);
  163. $this->assertCarbon(Carbon::make(new Carbon('2017-01-05')), 2017, 1, 5, 0, 0, 0);
  164. $this->assertNull(Carbon::make(3));
  165. }
  166. public function testCreateWithInvalidTimezoneOffset()
  167. {
  168. $this->expectExceptionObject(new InvalidTimeZoneException(
  169. 'Absolute timezone offset cannot be greater than 100.'
  170. ));
  171. Carbon::createFromDate(2000, 1, 1, -28236);
  172. }
  173. public function testCreateWithValidTimezoneOffset()
  174. {
  175. $dt = Carbon::createFromDate(2000, 1, 1, -4);
  176. $this->assertSame('America/New_York', $dt->tzName);
  177. $dt = Carbon::createFromDate(2000, 1, 1, '-4');
  178. $this->assertSame('-04:00', $dt->tzName);
  179. }
  180. public function testParseFromLocale()
  181. {
  182. $date = Carbon::parseFromLocale('23 Okt 2019', 'de');
  183. $this->assertSame('Wednesday, October 23, 2019 12:00 AM America/Toronto', $date->isoFormat('LLLL zz'));
  184. $date = Carbon::parseFromLocale('23 Okt 2019', 'de', 'Europe/Berlin')->locale('de');
  185. $this->assertSame('Mittwoch, 23. Oktober 2019 00:00 Europe/Berlin', $date->isoFormat('LLLL zz'));
  186. $date = Carbon::parseFromLocale('23 červenec 2019', 'cs');
  187. $this->assertSame('2019-07-23', $date->format('Y-m-d'));
  188. $date = Carbon::parseFromLocale('23 červen 2019', 'cs');
  189. $this->assertSame('2019-06-23', $date->format('Y-m-d'));
  190. Carbon::setTestNow('2021-01-26 15:45:13');
  191. $date = Carbon::parseFromLocale('завтра', 'ru');
  192. $this->assertSame('2021-01-27 00:00:00', $date->format('Y-m-d H:i:s'));
  193. }
  194. public function testParseFromLocaleWithDefaultLocale()
  195. {
  196. Carbon::setLocale('fr');
  197. $date = Carbon::parseFromLocale('Dimanche');
  198. $this->assertSame('dimanche', $date->dayName);
  199. $date = Carbon::parseFromLocale('Lundi');
  200. $this->assertSame('lundi', $date->dayName);
  201. }
  202. public function testCreateFromLocaleFormat()
  203. {
  204. $date = Carbon::createFromLocaleFormat('Y M d H,i,s', 'zh_CN', '2019 四月 4 12,04,21');
  205. $this->assertSame('Thursday, April 4, 2019 12:04 PM America/Toronto', $date->isoFormat('LLLL zz'));
  206. $date = Carbon::createFromLocaleFormat('Y M d H,i,s', 'zh_TW', '2019 四月 4 12,04,21', 'Asia/Shanghai')->locale('zh');
  207. $this->assertSame('2019年4月4日星期四 中午 12点04分 Asia/Shanghai', $date->isoFormat('LLLL zz'));
  208. $this->assertSame(
  209. '2022-12-05 America/Mexico_City',
  210. Carbon::createFromLocaleFormat('d * F * Y', 'es', '05 de diciembre de 2022', 'America/Mexico_City')
  211. ->format('Y-m-d e')
  212. );
  213. $this->assertSame(
  214. '2022-12-05 America/Mexico_City',
  215. Carbon::createFromLocaleFormat('d \of F \of Y', 'es', '05 de diciembre de 2022', 'America/Mexico_City')
  216. ->format('Y-m-d e')
  217. );
  218. $this->assertSame(
  219. '2022-12-05 America/Mexico_City',
  220. Carbon::createFromLocaleFormat('d \o\f F \o\f Y', 'es', '05 de diciembre de 2022', 'America/Mexico_City')
  221. ->format('Y-m-d e')
  222. );
  223. $this->assertSame(
  224. '2022-12-05 America/Mexico_City',
  225. Carbon::createFromLocaleFormat('d \d\e F \d\e Y', 'es', '05 de diciembre de 2022', 'America/Mexico_City')
  226. ->format('Y-m-d e')
  227. );
  228. $this->assertSame(
  229. '2022-12-05 America/Mexico_City',
  230. Carbon::createFromLocaleFormat('d \n\o\t F \n\o\t Y', 'es', '05 not diciembre not 2022', 'America/Mexico_City')
  231. ->format('Y-m-d e')
  232. );
  233. }
  234. public function testCreateFromIsoFormat()
  235. {
  236. $date = Carbon::createFromIsoFormat('!YYYYY MMMM D', '2019 April 4');
  237. $this->assertSame('Thursday, April 4, 2019 12:00 AM America/Toronto', $date->isoFormat('LLLL zz'));
  238. }
  239. public function testCreateFromIsoFormatException()
  240. {
  241. $this->expectExceptionObject(new InvalidArgumentException(
  242. 'Format wo not supported for creation.'
  243. ));
  244. Carbon::createFromIsoFormat('YY D wo', '2019 April 4');
  245. }
  246. public function testCreateFromLocaleIsoFormat()
  247. {
  248. $date = Carbon::createFromLocaleIsoFormat('YYYY MMMM D HH,mm,ss', 'zh_TW', '2019 四月 4 12,04,21');
  249. $this->assertSame('Thursday, April 4, 2019 12:04 PM America/Toronto', $date->isoFormat('LLLL zz'));
  250. $date = Carbon::createFromLocaleIsoFormat('LLL zz', 'zh', '2019年4月4日 下午 2点04分 Asia/Shanghai');
  251. $this->assertSame('Thursday, April 4, 2019 2:04 PM Asia/Shanghai', $date->isoFormat('LLLL zz'));
  252. $this->assertSame('2019年4月4日星期四 下午 2点04分 Asia/Shanghai', $date->locale('zh')->isoFormat('LLLL zz'));
  253. $date = Carbon::createFromLocaleIsoFormat('llll', 'fr_CA', 'mar. 24 juil. 2018 08:34');
  254. $this->assertSame('2018-07-24 08:34', $date->format('Y-m-d H:i'));
  255. }
  256. }