MongoIdTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. /**
  4. * @author alcaeus <alcaeus@alcaeus.org>
  5. */
  6. class MongoIdTest extends TestCase
  7. {
  8. public function testCreateWithoutParameter()
  9. {
  10. $id = new \MongoId();
  11. $stringId = (string) $id;
  12. $this->assertSame(24, strlen($stringId));
  13. $this->assertSame($stringId, $id->{'$id'});
  14. $serialized = serialize($id);
  15. $this->assertSame(sprintf('C:7:"MongoId":24:{%s}', $stringId), $serialized);
  16. $unserialized = unserialize($serialized);
  17. $this->assertInstanceOf('MongoId', $unserialized);
  18. $this->assertSame($stringId, (string) $unserialized);
  19. }
  20. public function testCreateWithString()
  21. {
  22. $original = '54203e08d51d4a1f868b456e';
  23. $id = new \MongoId($original);
  24. $this->assertSame($original, (string) $id);
  25. $this->assertSame(9127278, $id->getInc());
  26. $this->assertSame(1411399176, $id->getTimestamp());
  27. $this->assertSame(34335, $id->getPID());
  28. }
  29. /**
  30. * @expectedException \MongoException
  31. * @expectedExceptionMessage Invalid object ID
  32. */
  33. public function testCreateWithInvalidStringThrowsMongoException()
  34. {
  35. new \MongoId('invalid');
  36. }
  37. public function testCreateWithObjetId()
  38. {
  39. $original = '54203e08d51d4a1f868b456e';
  40. $objectId = new \MongoDB\BSON\ObjectID($original);
  41. $id = new \MongoId($objectId);
  42. $this->assertSame($original, (string) $id);
  43. $this->assertAttributeNotSame($objectId, 'objectID', $id);
  44. }
  45. /**
  46. * @dataProvider dataIsValid
  47. */
  48. public function testIsValid($expected, $value)
  49. {
  50. $this->assertSame($expected, \MongoId::isValid($value));
  51. }
  52. public static function dataIsValid()
  53. {
  54. $original = '54203e08d51d4a1f868b456e';
  55. return [
  56. 'validId' => [true, '' . $original . ''],
  57. 'MongoId' => [true, new \MongoId($original)],
  58. 'ObjectID' => [true, new \MongoDB\BSON\ObjectID($original)],
  59. 'invalidString' => [false, 'abc'],
  60. ];
  61. }
  62. }