MongoDBRefTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, '$db' => 'mongo-php-adapter'];
  32. $object = new \stdClass();
  33. $object->_id = $id;
  34. return [
  35. 'simpleId' => [$validRef, $id],
  36. 'arrayWithIdProperty' => [$validRef, ['_id' => $id]],
  37. 'objectWithIdProperty' => [$validRef, $object],
  38. 'arrayWithoutId' => [null, []],
  39. 'objectWithoutId' => [null, new \stdClass()],
  40. ];
  41. }
  42. /**
  43. * @dataProvider dataIsRef
  44. */
  45. public function testIsRef($expected, $ref)
  46. {
  47. $this->assertSame($expected, \MongoDBRef::isRef($ref));
  48. }
  49. public static function dataIsRef()
  50. {
  51. $objectRef = new \stdClass();
  52. $objectRef->{'$ref'} = 'coll';
  53. $objectRef->{'$id'} = 'id';
  54. return [
  55. 'validRef' => [true, ['$ref' => 'coll', '$id' => 'id']],
  56. 'validRefWithDatabase' => [true, ['$ref' => 'coll', '$id' => 'id', '$db' => 'db']],
  57. 'refMissing' => [false, ['$id' => 'id']],
  58. 'idMissing' => [false, ['$ref' => 'coll']],
  59. 'objectRef' => [true, $objectRef],
  60. 'int' => [false, 5],
  61. ];
  62. }
  63. public function testGet()
  64. {
  65. $id = new \MongoId();
  66. $db = $this->getDatabase();
  67. $document = ['_id' => $id, 'foo' => 'bar'];
  68. $db->selectCollection('test')->insert($document);
  69. $fetchedRef = \MongoDBRef::get($db, ['$ref' => 'test', '$id' => $id]);
  70. $this->assertInternalType('array', $fetchedRef);
  71. $this->assertEquals($document, $fetchedRef);
  72. }
  73. public function testGetThroughMongoDB()
  74. {
  75. $id = new \MongoId();
  76. $db = $this->getDatabase();
  77. $document = ['_id' => $id, 'foo' => 'bar'];
  78. $db->selectCollection('test')->insert($document);
  79. $fetchedRef = $db->getDBRef(['$ref' => 'test', '$id' => $id]);
  80. $this->assertInternalType('array', $fetchedRef);
  81. $this->assertEquals($document, $fetchedRef);
  82. }
  83. public function testGetWithNonExistingDocument()
  84. {
  85. $db = $this->getDatabase();
  86. $this->assertNull(\MongoDBRef::get($db, ['$ref' => 'test', '$id' => 'foo']));
  87. }
  88. public function testGetWithInvalidRef()
  89. {
  90. $db = $this->getDatabase();
  91. $this->assertNull(\MongoDBRef::get($db, []));
  92. }
  93. }