MongoTimestampTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 MongoTimestampTest extends TestCase
  9. {
  10. public function testCreate()
  11. {
  12. $timestamp = new \MongoTimestamp(1234567890, 987654321);
  13. $this->assertSame(1234567890, $timestamp->sec);
  14. $this->assertSame(987654321, $timestamp->inc);
  15. $this->assertSame('1234567890', (string) $timestamp);
  16. return $timestamp;
  17. }
  18. /**
  19. * @depends testCreate
  20. */
  21. public function testConvertToBson(\MongoTimestamp $timestamp)
  22. {
  23. $this->skipTestUnless($timestamp instanceof TypeInterface);
  24. $bsonTimestamp = $timestamp->toBSONType();
  25. $this->assertInstanceOf('MongoDB\BSON\Timestamp', $bsonTimestamp);
  26. $this->assertSame('[987654321:1234567890]', (string) $bsonTimestamp);
  27. }
  28. public function testCreateWithGlobalInc()
  29. {
  30. $timestamp1 = new \MongoTimestamp(1234567890);
  31. $timestamp2 = new \MongoTimestamp(1234567890);
  32. $this->assertSame(0, $timestamp1->inc);
  33. $this->assertSame(1, $timestamp2->inc);
  34. }
  35. public function testCreateWithBsonTimestamp()
  36. {
  37. $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoTimestamp')));
  38. $bsonTimestamp = new \MongoDB\BSON\Timestamp(987654321, 1234567890);
  39. $timestamp = new \MongoTimestamp($bsonTimestamp);
  40. $this->assertSame(1234567890, $timestamp->sec);
  41. $this->assertSame(987654321, $timestamp->inc);
  42. }
  43. public function testContructorArgumentOrderDiffers()
  44. {
  45. $this->skipTestUnless(in_array(TypeInterface::class, class_implements('MongoTimestamp')));
  46. /* The legacy MongoTimestamp's constructor takes seconds before the
  47. * increment, while MongoDB\BSON\Timestamp takes the increment first.
  48. */
  49. $bsonTimestamp = new \MongoDB\BSON\Timestamp(12345, 67890);
  50. $timestamp = new \MongoTimestamp(67890, 12345);
  51. $this->assertSame((string) $bsonTimestamp, (string) $timestamp->toBSONType());
  52. }
  53. }