MongoDBRefTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoDBRefTest extends TestCase
  8. {
  9. public function testCreate()
  10. {
  11. $id = new \MongoId();
  12. $ref = \MongoDBRef::create('foo', $id);
  13. $this->assertSame(['$ref' => 'foo', '$id' => $id], $ref);
  14. }
  15. public function testCreateWithDatabase()
  16. {
  17. $id = new \MongoId();
  18. $ref = \MongoDBRef::create('foo', $id, 'database');
  19. $this->assertSame(['$ref' => 'foo', '$id' => $id, '$db' => 'database'], $ref);
  20. }
  21. /**
  22. * @dataProvider dataCreateThroughMongoDB
  23. */
  24. public function testCreateThroughMongoDB($expected, $document_or_id)
  25. {
  26. $ref = $this->getDatabase()->createDBRef('test', $document_or_id);
  27. $this->assertEquals($expected, $ref);
  28. }
  29. public static function dataCreateThroughMongoDB()
  30. {
  31. $id = new \MongoId();
  32. $validRef = ['$ref' => 'test', '$id' => $id];
  33. $object = new \stdClass();
  34. $object->_id = $id;
  35. $objectWithoutId = new \stdClass();
  36. return [
  37. 'simpleId' => [$validRef, $id],
  38. 'arrayWithIdProperty' => [$validRef, ['_id' => $id]],
  39. 'objectWithIdProperty' => [$validRef, $object],
  40. 'arrayWithoutId' => [null, []],
  41. 'objectWithoutId' => [['$ref' => 'test', '$id' => $objectWithoutId], $objectWithoutId],
  42. ];
  43. }
  44. /**
  45. * @dataProvider dataIsRef
  46. */
  47. public function testIsRef($expected, $ref)
  48. {
  49. $this->assertSame($expected, \MongoDBRef::isRef($ref));
  50. }
  51. public static function dataIsRef()
  52. {
  53. $objectRef = new \stdClass();
  54. $objectRef->{'$ref'} = 'coll';
  55. $objectRef->{'$id'} = 'id';
  56. return [
  57. 'validRef' => [true, ['$ref' => 'coll', '$id' => 'id']],
  58. 'validRefWithDatabase' => [true, ['$ref' => 'coll', '$id' => 'id', '$db' => 'db']],
  59. 'refMissing' => [false, ['$id' => 'id']],
  60. 'idMissing' => [false, ['$ref' => 'coll']],
  61. 'objectRef' => [true, $objectRef],
  62. 'int' => [false, 5],
  63. ];
  64. }
  65. public function testGet()
  66. {
  67. $id = new \MongoId();
  68. $db = $this->getDatabase();
  69. $document = ['_id' => $id, 'foo' => 'bar'];
  70. $db->selectCollection('test')->insert($document);
  71. $fetchedRef = \MongoDBRef::get($db, ['$ref' => 'test', '$id' => $id]);
  72. $this->assertInternalType('array', $fetchedRef);
  73. $this->assertEquals($document, $fetchedRef);
  74. }
  75. public function testGetThroughMongoDB()
  76. {
  77. $id = new \MongoId();
  78. $db = $this->getDatabase();
  79. $document = ['_id' => $id, 'foo' => 'bar'];
  80. $db->selectCollection('test')->insert($document);
  81. $fetchedRef = $db->getDBRef(['$ref' => 'test', '$id' => $id]);
  82. $this->assertInternalType('array', $fetchedRef);
  83. $this->assertEquals($document, $fetchedRef);
  84. }
  85. public function testGetWithNonExistingDocument()
  86. {
  87. $db = $this->getDatabase();
  88. $this->assertNull(\MongoDBRef::get($db, ['$ref' => 'test', '$id' => 'foo']));
  89. }
  90. public function testGetWithInvalidRef()
  91. {
  92. $db = $this->getDatabase();
  93. $this->assertNull(\MongoDBRef::get($db, []));
  94. }
  95. public function testGetWithDifferentDatabase()
  96. {
  97. $database = $this->getDatabase();
  98. $collection = $this->getCollection();
  99. $document = ['foo' => 'bar'];
  100. $collection->insert($document);
  101. $ref = [
  102. '$ref' => $collection->getName(),
  103. '$id' => $document['_id'],
  104. '$db' => (string) $database,
  105. ];
  106. $referencedDocument = $this->getClient()->selectDB('foo')->getDBRef($ref);
  107. $this->assertEquals($document, $referencedDocument);
  108. }
  109. }