MongoDateTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. use Alcaeus\MongoDbAdapter\TypeInterface;
  5. /**
  6. * @author alcaeus <alcaeus@alcaeus.org>
  7. */
  8. class MongoDateTest extends TestCase
  9. {
  10. public function testTimeZoneDoesNotAlterReturnedDateTime()
  11. {
  12. $initialTZ = ini_get("date.timezone");
  13. ini_set("date.timezone", "UTC");
  14. // Today at 8h 8m 8s
  15. $timestamp = mktime(8, 8, 8);
  16. $date = new \MongoDate($timestamp);
  17. $this->assertSame('08:08:08', $date->toDateTime()->format("H:i:s"));
  18. ini_set("date.timezone", "Europe/Paris");
  19. $this->assertSame('08:08:08', $date->toDateTime()->format("H:i:s"));
  20. ini_set("date.timezone", $initialTZ);
  21. }
  22. public function testCreate()
  23. {
  24. $date = new \MongoDate(1234567890, 123456);
  25. $this->assertAttributeSame(1234567890, 'sec', $date);
  26. $this->assertAttributeSame(123000, 'usec', $date);
  27. $this->assertSame('0.12300000 1234567890', (string) $date);
  28. $dateTime = $date->toDateTime();
  29. $this->assertSame(1234567890, $dateTime->getTimestamp());
  30. $this->assertSame('123000', $dateTime->format('u'));
  31. return $date;
  32. }
  33. /**
  34. * @depends testCreate
  35. */
  36. public function testConvertToBson(\MongoDate $date)
  37. {
  38. $this->skipTestUnless($date instanceof TypeInterface);
  39. $dateTime = $date->toDateTime();
  40. $bsonDate = $date->toBSONType();
  41. $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $bsonDate);
  42. $this->assertSame('1234567890123', (string) $bsonDate);
  43. $bsonDateTime = $bsonDate->toDateTime();
  44. // Compare timestamps to avoid issues with DateTime
  45. $timestamp = $dateTime->format('U') . '.' . $dateTime->format('U');
  46. $bsonTimestamp = $bsonDateTime->format('U') . '.' . $bsonDateTime->format('U');
  47. $this->assertSame((float) $timestamp, (float) $bsonTimestamp);
  48. }
  49. public function testCreateWithString()
  50. {
  51. $date = new \MongoDate('1234567890', '123456');
  52. $this->assertAttributeSame(1234567890, 'sec', $date);
  53. $this->assertAttributeSame(123000, 'usec', $date);
  54. }
  55. public function testCreateWithBsonDate()
  56. {
  57. $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoDate')));
  58. $bsonDate = new \MongoDB\BSON\UTCDateTime(1234567890123);
  59. $date = new \MongoDate($bsonDate);
  60. $this->assertAttributeSame(1234567890, 'sec', $date);
  61. $this->assertAttributeSame(123000, 'usec', $date);
  62. }
  63. public function testSupportMillisecondsWithLeadingZeroes()
  64. {
  65. $date = new \MongoDate('1234567890', '012345');
  66. $this->assertAttributeSame(1234567890, 'sec', $date);
  67. $this->assertAttributeSame(12000, 'usec', $date);
  68. $this->assertSame('0.01200000 1234567890', (string) $date);
  69. $dateTime = $date->toDateTime();
  70. $this->assertSame(1234567890, $dateTime->getTimestamp());
  71. $this->assertSame('012000', $dateTime->format('u'));
  72. }
  73. public function testDSTTransitionDoesNotProduceWrongResults()
  74. {
  75. $initialTZ = ini_get("date.timezone");
  76. ini_set("date.timezone", "Europe/Madrid");
  77. $date = new \MongoDate(1603584000);
  78. $dateTime = $date->toDateTime();
  79. $this->assertSame(1603584000, $dateTime->getTimestamp());
  80. ini_set("date.timezone", $initialTZ);
  81. }
  82. public function testDSTTransitionDoesNotProduceWrongResultsWithMicroSeconds()
  83. {
  84. $initialTZ = ini_get("date.timezone");
  85. ini_set("date.timezone", "Europe/Madrid");
  86. $date = new \MongoDate(1603584000, 123456);
  87. $dateTime = $date->toDateTime();
  88. $this->assertSame(1603584000, $dateTime->getTimestamp());
  89. $this->assertSame('123000', $dateTime->format('u'));
  90. ini_set("date.timezone", $initialTZ);
  91. }
  92. }