MongoCollectionTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\ReadPreference;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoCollectionTest extends TestCase
  8. {
  9. public function testGetNestedCollections()
  10. {
  11. $collection = $this->getCollection()->foo->bar;
  12. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  13. }
  14. public function testCreateRecord()
  15. {
  16. $id = '54203e08d51d4a1f868b456e';
  17. $collection = $this->getCollection();
  18. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  19. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  20. $this->assertSame(1, $newCollection->count());
  21. $object = $newCollection->findOne();
  22. $this->assertNotNull($object);
  23. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  24. $this->assertSame($id, (string) $object->_id);
  25. $this->assertObjectHasAttribute('foo', $object);
  26. $this->assertAttributeSame('bar', 'foo', $object);
  27. }
  28. public function testFindReturnsCursor()
  29. {
  30. $this->prepareData();
  31. $collection = $this->getCollection();
  32. $this->assertInstanceOf('MongoCursor', $collection->find());
  33. }
  34. public function testCount()
  35. {
  36. $this->prepareData();
  37. $collection = $this->getCollection();
  38. $this->assertSame(3, $collection->count());
  39. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  40. }
  41. public function testFindOne()
  42. {
  43. $this->prepareData();
  44. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  45. $this->assertEquals(['foo' => 'foo'], $document);
  46. }
  47. public function testDistinct()
  48. {
  49. $this->prepareData();
  50. $values = $this->getCollection()->distinct('foo');
  51. $this->assertInternalType('array', $values);
  52. sort($values);
  53. $this->assertEquals(['bar', 'foo'], $values);
  54. }
  55. public function testDistinctWithQuery()
  56. {
  57. $this->prepareData();
  58. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  59. $this->assertInternalType('array', $values);
  60. $this->assertEquals(['bar'], $values);
  61. }
  62. public function testAggregate()
  63. {
  64. $collection = $this->getCollection();
  65. $collection->insert(['foo' => 'bar']);
  66. $collection->insert(['foo' => 'bar']);
  67. $collection->insert(['foo' => 'foo']);
  68. $pipeline = [
  69. [
  70. '$group' => [
  71. '_id' => '$foo',
  72. 'count' => [ '$sum' => 1 ],
  73. ],
  74. ],
  75. [
  76. '$sort' => ['_id' => 1]
  77. ]
  78. ];
  79. $result = $collection->aggregate($pipeline);
  80. $this->assertInternalType('array', $result);
  81. $this->assertArrayHasKey('result', $result);
  82. $this->assertEquals([
  83. ['_id' => 'bar', 'count' => 2],
  84. ['_id' => 'foo', 'count' => 1],
  85. ], $result['result']);
  86. }
  87. public function testAggregateCursor()
  88. {
  89. $collection = $this->getCollection();
  90. $collection->insert(['foo' => 'bar']);
  91. $collection->insert(['foo' => 'bar']);
  92. $collection->insert(['foo' => 'foo']);
  93. $pipeline = [
  94. [
  95. '$group' => [
  96. '_id' => '$foo',
  97. 'count' => [ '$sum' => 1 ],
  98. ],
  99. ],
  100. [
  101. '$sort' => ['_id' => 1]
  102. ]
  103. ];
  104. $cursor = $collection->aggregateCursor($pipeline);
  105. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  106. $this->assertEquals([
  107. ['_id' => 'bar', 'count' => 2],
  108. ['_id' => 'foo', 'count' => 1],
  109. ], iterator_to_array($cursor));
  110. }
  111. public function testReadPreference()
  112. {
  113. $collection = $this->getCollection();
  114. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  115. $this->assertFalse($collection->getSlaveOkay());
  116. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  117. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  118. $this->assertTrue($collection->getSlaveOkay());
  119. // Only way to check whether options are passed down is through debugInfo
  120. $writeConcern = $collection->getCollection()->__debugInfo()['readPreference'];
  121. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  122. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  123. $this->assertTrue($collection->setSlaveOkay(true));
  124. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  125. $this->assertTrue($collection->setSlaveOkay(false));
  126. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  127. }
  128. public function testReadPreferenceIsInherited()
  129. {
  130. $database = $this->getDatabase();
  131. $database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  132. $collection = $database->selectCollection('test');
  133. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  134. }
  135. public function testWriteConcern()
  136. {
  137. $collection = $this->getCollection();
  138. $this->assertSame(['w' => 1, 'wtimeout' => 0], $collection->getWriteConcern());
  139. $this->assertSame(1, $collection->w);
  140. $this->assertSame(0, $collection->wtimeout);
  141. $this->assertTrue($collection->setWriteConcern('majority', 100));
  142. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  143. $collection->w = 2;
  144. $this->assertSame(['w' => 2, 'wtimeout' => 100], $collection->getWriteConcern());
  145. $collection->wtimeout = -1;
  146. $this->assertSame(['w' => 2, 'wtimeout' => 0], $collection->getWriteConcern());
  147. // Only way to check whether options are passed down is through debugInfo
  148. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  149. $this->assertSame(2, $writeConcern->getW());
  150. $this->assertSame(0, $writeConcern->getWtimeout());
  151. }
  152. public function testWriteConcernIsInherited()
  153. {
  154. $database = $this->getDatabase();
  155. $database->setWriteConcern('majority', 100);
  156. $collection = $database->selectCollection('test');
  157. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  158. }
  159. /**
  160. * @return \MongoCollection
  161. */
  162. protected function getCollection($name = 'test')
  163. {
  164. return $this->getDatabase()->selectCollection($name);
  165. }
  166. /**
  167. * @return \MongoDB
  168. */
  169. protected function getDatabase()
  170. {
  171. $client = new \MongoClient();
  172. return $client->selectDB('mongo-php-adapter');
  173. }
  174. /**
  175. * @return \MongoCollection
  176. */
  177. protected function prepareData()
  178. {
  179. $collection = $this->getCollection();
  180. $collection->insert(['foo' => 'bar']);
  181. $collection->insert(['foo' => 'bar']);
  182. $collection->insert(['foo' => 'foo']);
  183. return $collection;
  184. }
  185. }