MongoDBRefTest.php 3.2 KB

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