PhpBug72338Test.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Tests\AbstractTestCase;
  14. /**
  15. * The problem is, that $date->setTimezone($tz) with $tz in 'HH:MM' notation (["timezone_type"]=>int(1)) put DateTime object
  16. * on inconsistent state. It looks like internal timestamp becomes changed and it affects to such functions:
  17. *
  18. * * $date->modify() uses changed timestamp and result is wrong
  19. *
  20. * * $date->setTimezone($tz) settle this changed timestamp, even in case if $tz is not in 'HH:MM' format
  21. *
  22. * * $date->format('U') returns changed timestamp
  23. *
  24. * @link https://bugs.php.net/bug.php?id=72338 This bug on bugs.php.net
  25. *
  26. * @internal I use days changing in tests because using seconds|minute|hours may run setTimezone within.
  27. */
  28. class PhpBug72338Test extends AbstractTestCase
  29. {
  30. /**
  31. * Ensures that modify don't use changed timestamp
  32. */
  33. public function testModify()
  34. {
  35. $date = Carbon::createFromTimestamp(0)->setTimezone('+02:00')->modify('+1 day');
  36. $this->assertSame('86400', $date->format('U'));
  37. }
  38. /**
  39. * Ensures that $date->format('U') returns unchanged timestamp
  40. */
  41. public function testTimestamp()
  42. {
  43. $date = Carbon::createFromTimestamp(0)->setTimezone('+02:00');
  44. $this->assertSame('0', $date->format('U'));
  45. }
  46. /**
  47. * Ensures that date created from string with timezone and with same timezone set by setTimezone() is equal
  48. */
  49. public function testEqualSetAndCreate()
  50. {
  51. $date = Carbon::createFromTimestamp(0)->setTimezone('+02:00');
  52. $date1 = new Carbon('1970-01-01T02:00:00+02:00');
  53. $this->assertSame($date->format('U'), $date1->format('U'));
  54. }
  55. /**
  56. * Ensures that second call to setTimezone() don't changing timestamp
  57. */
  58. public function testSecondSetTimezone()
  59. {
  60. $date = Carbon::createFromTimestamp(0)->setTimezone('+02:00')->setTimezone('Europe/Moscow');
  61. $this->assertSame('0', $date->format('U'));
  62. }
  63. }