PhpBug72338Test.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Carbon;
  12. use Carbon\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);
  36. $date->setTimezone('+02:00');
  37. $date->modify('+1 day');
  38. $this->assertSame('86400', $date->format('U'));
  39. }
  40. /**
  41. * Ensures that $date->format('U') returns unchanged timestamp
  42. */
  43. public function testTimestamp()
  44. {
  45. $date = Carbon::createFromTimestamp(0);
  46. $date->setTimezone('+02:00');
  47. $this->assertSame('0', $date->format('U'));
  48. }
  49. /**
  50. * Ensures that date created from string with timezone and with same timezone set by setTimezone() is equal
  51. */
  52. public function testEqualSetAndCreate()
  53. {
  54. $date = Carbon::createFromTimestamp(0);
  55. $date->setTimezone('+02:00');
  56. $date1 = new Carbon('1970-01-01T02:00:00+02:00');
  57. $this->assertSame($date->format('U'), $date1->format('U'));
  58. }
  59. /**
  60. * Ensures that second call to setTimezone() don't changing timestamp
  61. */
  62. public function testSecondSetTimezone()
  63. {
  64. $date = Carbon::createFromTimestamp(0);
  65. $date->setTimezone('+02:00');
  66. $date->setTimezone('Europe/Moscow');
  67. $this->assertSame('0', $date->format('U'));
  68. }
  69. }