MongoDateTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use Alcaeus\MongoDbAdapter\TypeInterface;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoDateTest extends TestCase
  8. {
  9. public function testCreate()
  10. {
  11. $date = new \MongoDate(1234567890, 123456);
  12. $this->assertAttributeSame(1234567890, 'sec', $date);
  13. $this->assertAttributeSame(123000, 'usec', $date);
  14. $this->assertSame('0.12300000 1234567890', (string) $date);
  15. $dateTime = $date->toDateTime();
  16. $this->assertSame(1234567890, $dateTime->getTimestamp());
  17. $this->assertSame('123000', $dateTime->format('u'));
  18. return $date;
  19. }
  20. /**
  21. * @depends testCreate
  22. */
  23. public function testConvertToBson(\MongoDate $date)
  24. {
  25. $this->skipTestUnless($date instanceof TypeInterface);
  26. $dateTime = $date->toDateTime();
  27. $bsonDate = $date->toBSONType();
  28. $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $bsonDate);
  29. $this->assertSame('1234567890123', (string) $bsonDate);
  30. $this->assertEquals($dateTime, $bsonDate->toDateTime());
  31. }
  32. public function testCreateWithBsonDate()
  33. {
  34. $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoDate')));
  35. $bsonDate = new \MongoDB\BSON\UTCDateTime(1234567890123);
  36. $date = new \MongoDate($bsonDate);
  37. $this->assertAttributeSame(1234567890, 'sec', $date);
  38. $this->assertAttributeSame(123000, 'usec', $date);
  39. }
  40. }