MongoCollectionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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->assertSame(['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. public function testSaveInsert()
  160. {
  161. $id = '54203e08d51d4a1f868b456e';
  162. $collection = $this->getCollection();
  163. $collection->save(['_id' => new \MongoId($id), 'foo' => 'bar']);
  164. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  165. $this->assertSame(1, $newCollection->count());
  166. $object = $newCollection->findOne();
  167. $this->assertNotNull($object);
  168. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  169. $this->assertSame($id, (string) $object->_id);
  170. $this->assertObjectHasAttribute('foo', $object);
  171. $this->assertAttributeSame('bar', 'foo', $object);
  172. }
  173. public function testSaveUpdate()
  174. {
  175. $id = '54203e08d51d4a1f868b456e';
  176. $collection = $this->getCollection();
  177. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  178. $collection->save(['_id' => new \MongoId($id), 'foo' => 'foo']);
  179. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  180. $this->assertSame(1, $newCollection->count());
  181. $object = $newCollection->findOne();
  182. $this->assertNotNull($object);
  183. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  184. $this->assertSame($id, (string) $object->_id);
  185. $this->assertObjectHasAttribute('foo', $object);
  186. $this->assertAttributeSame('foo', 'foo', $object);
  187. }
  188. public function testGetDBRef()
  189. {
  190. $collection = $this->getCollection();
  191. $collection->insert(['_id' => 1, 'foo' => 'bar']);
  192. $document = $collection->getDBRef([
  193. '$ref' => 'test',
  194. '$id' => 1,
  195. ]);
  196. $this->assertEquals(['_id' => 1, 'foo' => 'bar'], $document);
  197. }
  198. public function testCreateDBRef()
  199. {
  200. $collection = $this->getCollection();
  201. $reference = $collection->createDBRef(['_id' => 'foo']);
  202. $this->assertSame(
  203. [
  204. '$ref' => 'test',
  205. '$id' => 'foo',
  206. ],
  207. $reference
  208. );
  209. }
  210. public function testCreateIndex()
  211. {
  212. $collection = $this->getCollection();
  213. $collection->createIndex(['foo' => 1]);
  214. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  215. $iterator = $newCollection->listIndexes();
  216. $indexes = iterator_to_array($iterator);
  217. $this->assertCount(2, $indexes);
  218. $index = $indexes[1];
  219. $this->assertSame(['foo' => 1], $index->getKey());
  220. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  221. }
  222. public function testEnsureIndex()
  223. {
  224. $collection = $this->getCollection();
  225. $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
  226. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  227. $indexes = iterator_to_array($newCollection->listIndexes());
  228. $this->assertCount(2, $indexes);
  229. $index = $indexes[1];
  230. $this->assertSame(['bar' => 1], $index->getKey());
  231. $this->assertTrue($index->isUnique());
  232. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  233. }
  234. public function testDeleteIndexUsingIndexName()
  235. {
  236. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  237. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  238. $expected = [
  239. 'nIndexesWas' => 2,
  240. 'ok' => 1.0,
  241. ];
  242. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  243. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  244. }
  245. public function testDeleteIndexUsingKeys()
  246. {
  247. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  248. $newCollection->createIndex(['bar' => 1]);
  249. $expected = [
  250. 'nIndexesWas' => 2,
  251. 'ok' => 1.0,
  252. ];
  253. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  254. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  255. }
  256. public function testDeleteIndexes()
  257. {
  258. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  259. $newCollection->createIndex(['bar' => 1]);
  260. $expected = [
  261. 'nIndexesWas' => 2,
  262. 'msg' => 'non-_id indexes dropped for collection',
  263. 'ok' => 1.0,
  264. ];
  265. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  266. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  267. }
  268. public function testGetIndexInfo()
  269. {
  270. $collection = $this->getCollection();
  271. $collection->createIndex(['foo' => 1]);
  272. $expected = [
  273. [
  274. 'v' => 1,
  275. 'key' => ['_id' => 1],
  276. 'name' => '_id_',
  277. 'ns' => 'mongo-php-adapter.test',
  278. ],
  279. [
  280. 'v' => 1,
  281. 'key' => ['foo' => 1],
  282. 'name' => 'foo_1',
  283. 'ns' => 'mongo-php-adapter.test',
  284. ],
  285. ];
  286. $this->assertSame(
  287. $expected,
  288. $collection->getIndexInfo()
  289. );
  290. }
  291. public function testFindAndModifyUpdate()
  292. {
  293. $id = '54203e08d51d4a1f868b456e';
  294. $collection = $this->getCollection();
  295. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  296. $document = $collection->findAndModify(
  297. ['_id' => new \MongoId($id)],
  298. ['$set' => ['foo' => 'foo']]
  299. );
  300. $this->assertSame('bar', $document['foo']);
  301. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  302. $this->assertSame(1, $newCollection->count());
  303. $object = $newCollection->findOne();
  304. $this->assertNotNull($object);
  305. $this->assertAttributeSame('foo', 'foo', $object);
  306. }
  307. public function testFindAndModifyUpdateReturnNew()
  308. {
  309. $id = '54203e08d51d4a1f868b456e';
  310. $collection = $this->getCollection();
  311. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  312. $document = $collection->findAndModify(
  313. ['_id' => new \MongoId($id)],
  314. ['$set' => ['foo' => 'foo']],
  315. null,
  316. ['new' => true]
  317. );
  318. $this->assertSame('foo', $document['foo']);
  319. }
  320. public function testFindAndModifyWithFields()
  321. {
  322. $id = '54203e08d51d4a1f868b456e';
  323. $collection = $this->getCollection();
  324. $collection->insert([
  325. '_id' => new \MongoId($id),
  326. 'foo' => 'bar',
  327. 'bar' => 'foo',
  328. ]);
  329. $document = $collection->findAndModify(
  330. ['_id' => new \MongoId($id)],
  331. ['$set' => ['foo' => 'foo']],
  332. ['foo' => true]
  333. );
  334. $this->assertArrayNotHasKey('bar', $document);
  335. $this->assertArrayHasKey('foo', $document);
  336. }
  337. public function testGroup()
  338. {
  339. $collection = $this->getCollection();
  340. $collection->insert(['a' => 2]);
  341. $collection->insert(['b' => 5]);
  342. $collection->insert(['a' => 1]);
  343. $keys = [];
  344. $initial = ["count" => 0];
  345. $reduce = "function (obj, prev) { prev.count++; }";
  346. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  347. $result = $collection->group($keys, $initial, $reduce, $condition);
  348. $this->assertArraySubset(
  349. [
  350. 'retval' => [['count' => 1.0]],
  351. 'count' => 1.0,
  352. 'keys' => 1,
  353. 'ok' => 1.0,
  354. ],
  355. $result
  356. );
  357. }
  358. public function testFindAndModifyRemove()
  359. {
  360. $id = '54203e08d51d4a1f868b456e';
  361. $collection = $this->getCollection();
  362. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  363. $document = $collection->findAndModify(
  364. ['_id' => new \MongoId($id)],
  365. null,
  366. null,
  367. ['remove' => true]
  368. );
  369. $this->assertEquals('bar', $document['foo']);
  370. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  371. $this->assertSame(0, $newCollection->count());
  372. }
  373. public function testValidate()
  374. {
  375. $collection = $this->getCollection();
  376. $collection->insert(['foo' => 'bar']);
  377. $result = $collection->validate();
  378. $this->assertArraySubset(
  379. [
  380. 'ns' => 'mongo-php-adapter.test',
  381. 'nrecords' => 1,
  382. 'nIndexes' => 1,
  383. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  384. 'valid' => true,
  385. 'errors' => [],
  386. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  387. 'ok' => 1.0
  388. ],
  389. $result
  390. );
  391. }
  392. /**
  393. * @return \MongoCollection
  394. */
  395. protected function prepareData()
  396. {
  397. $collection = $this->getCollection();
  398. $collection->insert(['foo' => 'bar']);
  399. $collection->insert(['foo' => 'bar']);
  400. $collection->insert(['foo' => 'foo']);
  401. return $collection;
  402. }
  403. }