SettersTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 InvalidArgumentException;
  15. use RuntimeException;
  16. use Tests\AbstractTestCase;
  17. class SettersTest extends AbstractTestCase
  18. {
  19. public function testYearSetter()
  20. {
  21. $this->expectExceptionObject(new RuntimeException(
  22. 'Carbon\CarbonImmutable class is immutable.'
  23. ));
  24. $d = Carbon::now();
  25. $d->year = 1995;
  26. }
  27. public function testMonthSetter()
  28. {
  29. $this->expectExceptionObject(new RuntimeException(
  30. 'Carbon\CarbonImmutable class is immutable.'
  31. ));
  32. $d = Carbon::now();
  33. $d->month = 3;
  34. }
  35. public function testMonthSetterWithWrap()
  36. {
  37. $this->expectExceptionObject(new RuntimeException(
  38. 'Carbon\CarbonImmutable class is immutable.'
  39. ));
  40. $d = Carbon::now();
  41. $d->month = 13;
  42. }
  43. public function testDaySetter()
  44. {
  45. $this->expectExceptionObject(new RuntimeException(
  46. 'Carbon\CarbonImmutable class is immutable.'
  47. ));
  48. $d = Carbon::now();
  49. $d->day = 2;
  50. }
  51. public function testDaySetterWithWrap()
  52. {
  53. $this->expectExceptionObject(new RuntimeException(
  54. 'Carbon\CarbonImmutable class is immutable.'
  55. ));
  56. $d = Carbon::createFromDate(2012, 8, 5);
  57. $d->day = 32;
  58. }
  59. public function testHourSetter()
  60. {
  61. $this->expectExceptionObject(new RuntimeException(
  62. 'Carbon\CarbonImmutable class is immutable.'
  63. ));
  64. $d = Carbon::now();
  65. $d->hour = 2;
  66. }
  67. public function testHourSetterWithWrap()
  68. {
  69. $this->expectExceptionObject(new RuntimeException(
  70. 'Carbon\CarbonImmutable class is immutable.'
  71. ));
  72. $d = Carbon::now();
  73. $d->hour = 25;
  74. }
  75. public function testMinuteSetter()
  76. {
  77. $this->expectExceptionObject(new RuntimeException(
  78. 'Carbon\CarbonImmutable class is immutable.'
  79. ));
  80. $d = Carbon::now();
  81. $d->minute = 2;
  82. }
  83. public function testMinuteSetterWithWrap()
  84. {
  85. $this->expectExceptionObject(new RuntimeException(
  86. 'Carbon\CarbonImmutable class is immutable.'
  87. ));
  88. $d = Carbon::now();
  89. $d->minute = 65;
  90. }
  91. public function testSecondSetter()
  92. {
  93. $this->expectExceptionObject(new RuntimeException(
  94. 'Carbon\CarbonImmutable class is immutable.'
  95. ));
  96. $d = Carbon::now();
  97. $d->second = 2;
  98. }
  99. public function testTimeSetter()
  100. {
  101. $d = Carbon::now();
  102. $d = $d->setTime(1, 1, 1);
  103. $this->assertSame(1, $d->second);
  104. $d = $d->setTime(1, 1);
  105. $this->assertSame(0, $d->second);
  106. }
  107. public function testTimeSetterWithChaining()
  108. {
  109. $d = Carbon::now();
  110. $d = $d->setTime(2, 2, 2)->setTime(1, 1, 1);
  111. $this->assertInstanceOfCarbon($d);
  112. $this->assertSame(1, $d->second);
  113. $d = $d->setTime(2, 2, 2)->setTime(1, 1);
  114. $this->assertInstanceOfCarbon($d);
  115. $this->assertSame(0, $d->second);
  116. }
  117. public function testTimeSetterWithZero()
  118. {
  119. $d = Carbon::now();
  120. $d = $d->setTime(1, 1);
  121. $this->assertSame(0, $d->second);
  122. }
  123. public function testDateTimeSetter()
  124. {
  125. $d = Carbon::now();
  126. $d = $d->setDateTime($d->year, $d->month, $d->day, 1, 1, 1);
  127. $this->assertSame(1, $d->second);
  128. }
  129. public function testDateTimeSetterWithZero()
  130. {
  131. $d = Carbon::now();
  132. $d = $d->setDateTime($d->year, $d->month, $d->day, 1, 1);
  133. $this->assertSame(0, $d->second);
  134. }
  135. public function testDateTimeSetterWithChaining()
  136. {
  137. $d = Carbon::now();
  138. $d = $d->setDateTime(2013, 9, 24, 17, 4, 29);
  139. $this->assertInstanceOfCarbon($d);
  140. $d = $d->setDateTime(2014, 10, 25, 18, 5, 30);
  141. $this->assertInstanceOfCarbon($d);
  142. $this->assertCarbon($d, 2014, 10, 25, 18, 5, 30);
  143. }
  144. /**
  145. * @link https://github.com/briannesbitt/Carbon/issues/539
  146. */
  147. public function testSetDateAfterStringCreation()
  148. {
  149. $d = new Carbon('first day of this month');
  150. $this->assertSame(1, $d->day);
  151. $d = $d->setDate($d->year, $d->month, 12);
  152. $this->assertSame(12, $d->day);
  153. }
  154. public function testSecondSetterWithWrap()
  155. {
  156. $this->expectExceptionObject(new RuntimeException(
  157. 'Carbon\CarbonImmutable class is immutable.'
  158. ));
  159. $d = Carbon::now();
  160. $d->second = 65;
  161. }
  162. public function testTimestampSetter()
  163. {
  164. $this->expectExceptionObject(new RuntimeException(
  165. 'Carbon\CarbonImmutable class is immutable.'
  166. ));
  167. $d = Carbon::now();
  168. $d->timestamp = 10;
  169. }
  170. public function testSetTimezoneWithInvalidTimezone()
  171. {
  172. $this->expectExceptionObject(new InvalidArgumentException(
  173. 'Unknown or bad timezone (sdf)'
  174. ));
  175. $d = Carbon::now();
  176. $d->setTimezone('sdf');
  177. }
  178. public function testTimezoneWithInvalidTimezone()
  179. {
  180. $this->expectExceptionObject(new RuntimeException(
  181. 'Carbon\CarbonImmutable class is immutable.'
  182. ));
  183. /** @var mixed $d */
  184. $d = Carbon::now();
  185. $d->timezone = 'sdf';
  186. }
  187. public function testTimezoneWithInvalidTimezoneSetter()
  188. {
  189. $this->expectExceptionObject(new InvalidArgumentException(
  190. 'Unknown or bad timezone (sdf)'
  191. ));
  192. $d = Carbon::now();
  193. $d->timezone('sdf');
  194. }
  195. public function testTzWithInvalidTimezone()
  196. {
  197. $this->expectExceptionObject(new RuntimeException(
  198. 'Carbon\CarbonImmutable class is immutable.'
  199. ));
  200. /** @var mixed $d */
  201. $d = Carbon::now();
  202. $d->tz = 'sdf';
  203. }
  204. public function testTzWithInvalidTimezoneSetter()
  205. {
  206. $this->expectExceptionObject(new InvalidArgumentException(
  207. 'Unknown or bad timezone (sdf)'
  208. ));
  209. $d = Carbon::now();
  210. $d->tz('sdf');
  211. }
  212. public function testSetTimezoneUsingString()
  213. {
  214. $d = Carbon::now();
  215. $d = $d->setTimezone('America/Toronto');
  216. $this->assertSame('America/Toronto', $d->tzName);
  217. }
  218. public function testShiftTimezone()
  219. {
  220. $d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
  221. $d2 = $d->copy()->setTimezone('America/Toronto');
  222. $this->assertSame(0, $d2->getTimestamp() - $d->getTimestamp());
  223. $this->assertSame('04:53:12', $d2->format('H:i:s'));
  224. $d = Carbon::parse('2018-08-13 10:53:12', 'Europe/Paris');
  225. $d2 = $d->copy()->shiftTimezone('America/Toronto');
  226. $this->assertSame(21600, $d2->getTimestamp() - $d->getTimestamp());
  227. $this->assertSame('America/Toronto', $d2->tzName);
  228. $this->assertSame('10:53:12', $d2->format('H:i:s'));
  229. $d = Carbon::parse('2018-03-25 00:53:12.321654 America/Toronto')->shiftTimezone('Europe/Oslo');
  230. $this->assertSame('2018-03-25 00:53:12.321654 Europe/Oslo', $d->format('Y-m-d H:i:s.u e'));
  231. }
  232. public function testTimezoneUsingString()
  233. {
  234. $this->expectExceptionObject(new RuntimeException(
  235. 'Carbon\CarbonImmutable class is immutable.'
  236. ));
  237. /** @var mixed $d */
  238. $d = Carbon::now();
  239. $d->timezone = 'America/Toronto';
  240. }
  241. public function testTzUsingString()
  242. {
  243. $this->expectExceptionObject(new RuntimeException(
  244. 'Carbon\CarbonImmutable class is immutable.'
  245. ));
  246. /** @var mixed $d */
  247. $d = Carbon::now();
  248. $d->tz = 'America/Toronto';
  249. }
  250. public function testSetTimezoneUsingDateTimeZone()
  251. {
  252. $d = Carbon::now();
  253. $d = $d->setTimezone(new DateTimeZone('America/Toronto'));
  254. $this->assertSame('America/Toronto', $d->tzName);
  255. }
  256. public function testTimezoneUsingDateTimeZone()
  257. {
  258. $this->expectExceptionObject(new RuntimeException(
  259. 'Carbon\CarbonImmutable class is immutable.'
  260. ));
  261. /** @var mixed $d */
  262. $d = Carbon::now();
  263. $d->timezone = new DateTimeZone('America/Toronto');
  264. }
  265. public function testTzUsingDateTimeZone()
  266. {
  267. $this->expectExceptionObject(new RuntimeException(
  268. 'Carbon\CarbonImmutable class is immutable.'
  269. ));
  270. /** @var mixed $d */
  271. $d = Carbon::now();
  272. $d->tz = new DateTimeZone('America/Toronto');
  273. }
  274. public function testInvalidSetter()
  275. {
  276. $this->expectExceptionObject(new RuntimeException(
  277. 'Carbon\CarbonImmutable class is immutable.'
  278. ));
  279. /** @var mixed $d */
  280. $d = Carbon::now();
  281. $d->doesNotExit = 'bb';
  282. }
  283. /**
  284. * @dataProvider \Tests\CarbonImmutable\SettersTest::dataForTestSetTimeFromTimeString
  285. *
  286. * @param int $hour
  287. * @param int $minute
  288. * @param int $second
  289. * @param string $time
  290. */
  291. public function testSetTimeFromTimeString($hour, $minute, $second, $time)
  292. {
  293. Carbon::setTestNow(Carbon::create(2016, 2, 12, 1, 2, 3));
  294. $d = Carbon::now()->setTimeFromTimeString($time);
  295. $this->assertCarbon($d, 2016, 2, 12, $hour, $minute, $second);
  296. }
  297. public static function dataForTestSetTimeFromTimeString()
  298. {
  299. return [
  300. [9, 15, 30, '09:15:30'],
  301. [9, 15, 0, '09:15'],
  302. [9, 0, 0, '09'],
  303. [9, 5, 3, '9:5:3'],
  304. [9, 5, 0, '9:5'],
  305. [9, 0, 0, '9'],
  306. ];
  307. }
  308. public function testWeekendDaysSetter()
  309. {
  310. $weekendDays = [Carbon::FRIDAY, Carbon::SATURDAY];
  311. $d = Carbon::now();
  312. $d->setWeekendDays($weekendDays);
  313. $this->assertSame($weekendDays, $d->getWeekendDays());
  314. Carbon::setWeekendDays([Carbon::SATURDAY, Carbon::SUNDAY]);
  315. }
  316. public function testMidDayAtSetter()
  317. {
  318. $d = Carbon::now();
  319. $d->setMidDayAt(11);
  320. $this->assertSame(11, $d->getMidDayAt());
  321. $d->setMidDayAt(12);
  322. $this->assertSame(12, $d->getMidDayAt());
  323. }
  324. public function testSetter()
  325. {
  326. $d = Carbon::now();
  327. $d->setMidDayAt(11);
  328. $this->assertSame(11, $d->getMidDayAt());
  329. $d->setMidDayAt(12);
  330. $this->assertSame(12, $d->getMidDayAt());
  331. }
  332. }