MongoCollectionTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. $expected = [
  19. 'ok' => 1.0,
  20. 'n' => 0,
  21. 'err' => null,
  22. 'errmsg' => null,
  23. ];
  24. $this->assertSame($expected, $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']));
  25. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  26. $this->assertSame(1, $newCollection->count());
  27. $object = $newCollection->findOne();
  28. $this->assertNotNull($object);
  29. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  30. $this->assertSame($id, (string) $object->_id);
  31. $this->assertObjectHasAttribute('foo', $object);
  32. $this->assertAttributeSame('bar', 'foo', $object);
  33. }
  34. public function testUnacknowledgedWrite()
  35. {
  36. $this->assertTrue($this->getCollection()->insert(['foo' => 'bar'], ['w' => 0]));
  37. }
  38. public function testInsertMany()
  39. {
  40. $expected = [
  41. 'connectionId' => 0,
  42. 'n' => 0,
  43. 'syncMillis' => 0,
  44. 'writtenTo' => null,
  45. 'err' => null,
  46. 'errmsg' => null
  47. ];
  48. $documents = [
  49. ['foo' => 'bar'],
  50. ['bar' => 'foo']
  51. ];
  52. $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
  53. }
  54. public function testUpdateOne()
  55. {
  56. $this->getCollection()->insert(['foo' => 'bar']);
  57. $this->getCollection()->insert(['foo' => 'bar']);
  58. $expected = [
  59. 'ok' => 1.0,
  60. 'nModified' => 1,
  61. 'n' => 1,
  62. 'err' => null,
  63. 'errmsg' => null,
  64. 'updatedExisting' => true,
  65. ];
  66. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  67. $this->assertSame($expected, $result);
  68. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  69. }
  70. public function testUpdateMany()
  71. {
  72. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  73. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  74. $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
  75. $expected = [
  76. 'ok' => 1.0,
  77. 'nModified' => 2,
  78. 'n' => 3,
  79. 'err' => null,
  80. 'errmsg' => null,
  81. 'updatedExisting' => true,
  82. ];
  83. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  84. $this->assertSame($expected, $result);
  85. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  86. }
  87. public function testUnacknowledgedUpdate()
  88. {
  89. $this->getCollection()->insert(['foo' => 'bar']);
  90. $this->getCollection()->insert(['foo' => 'bar']);
  91. $this->assertTrue($this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']], ['w' => 0]));
  92. }
  93. public function testRemoveMultiple()
  94. {
  95. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  96. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  97. $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
  98. $expected = [
  99. 'ok' => 1.0,
  100. 'n' => 2,
  101. 'err' => null,
  102. 'errmsg' => null,
  103. ];
  104. $result = $this->getCollection()->remove(['foo' => 'bar']);
  105. $this->assertSame($expected, $result);
  106. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  107. }
  108. public function testRemoveSingle()
  109. {
  110. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  111. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  112. $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
  113. $expected = [
  114. 'ok' => 1.0,
  115. 'n' => 1,
  116. 'err' => null,
  117. 'errmsg' => null,
  118. ];
  119. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  120. $this->assertSame($expected, $result);
  121. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  122. }
  123. public function testRemoveUnacknowledged()
  124. {
  125. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  126. $this->getCollection()->insert(['change' => true, 'foo' => 'bar']);
  127. $this->getCollection()->insert(['change' => true, 'foo' => 'foo']);
  128. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  129. }
  130. public function testFindReturnsCursor()
  131. {
  132. $this->prepareData();
  133. $collection = $this->getCollection();
  134. $this->assertInstanceOf('MongoCursor', $collection->find());
  135. }
  136. public function testCount()
  137. {
  138. $this->prepareData();
  139. $collection = $this->getCollection();
  140. $this->assertSame(3, $collection->count());
  141. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  142. }
  143. public function testFindOne()
  144. {
  145. $this->prepareData();
  146. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  147. $this->assertSame(['foo' => 'foo'], $document);
  148. }
  149. public function testDistinct()
  150. {
  151. $this->prepareData();
  152. $values = $this->getCollection()->distinct('foo');
  153. $this->assertInternalType('array', $values);
  154. sort($values);
  155. $this->assertEquals(['bar', 'foo'], $values);
  156. }
  157. public function testDistinctWithQuery()
  158. {
  159. $this->prepareData();
  160. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  161. $this->assertInternalType('array', $values);
  162. $this->assertEquals(['bar'], $values);
  163. }
  164. public function testAggregate()
  165. {
  166. $collection = $this->getCollection();
  167. $collection->insert(['foo' => 'bar']);
  168. $collection->insert(['foo' => 'bar']);
  169. $collection->insert(['foo' => 'foo']);
  170. $pipeline = [
  171. [
  172. '$group' => [
  173. '_id' => '$foo',
  174. 'count' => [ '$sum' => 1 ],
  175. ],
  176. ],
  177. [
  178. '$sort' => ['_id' => 1]
  179. ]
  180. ];
  181. $result = $collection->aggregate($pipeline);
  182. $this->assertInternalType('array', $result);
  183. $this->assertArrayHasKey('result', $result);
  184. $this->assertEquals([
  185. ['_id' => 'bar', 'count' => 2],
  186. ['_id' => 'foo', 'count' => 1],
  187. ], $result['result']);
  188. }
  189. public function testAggregateCursor()
  190. {
  191. $collection = $this->getCollection();
  192. $collection->insert(['foo' => 'bar']);
  193. $collection->insert(['foo' => 'bar']);
  194. $collection->insert(['foo' => 'foo']);
  195. $pipeline = [
  196. [
  197. '$group' => [
  198. '_id' => '$foo',
  199. 'count' => [ '$sum' => 1 ],
  200. ],
  201. ],
  202. [
  203. '$sort' => ['_id' => 1]
  204. ]
  205. ];
  206. $cursor = $collection->aggregateCursor($pipeline);
  207. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  208. $this->assertEquals([
  209. ['_id' => 'bar', 'count' => 2],
  210. ['_id' => 'foo', 'count' => 1],
  211. ], iterator_to_array($cursor));
  212. }
  213. public function testReadPreference()
  214. {
  215. $collection = $this->getCollection();
  216. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  217. $this->assertFalse($collection->getSlaveOkay());
  218. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  219. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  220. $this->assertTrue($collection->getSlaveOkay());
  221. // Only way to check whether options are passed down is through debugInfo
  222. $writeConcern = $collection->getCollection()->__debugInfo()['readPreference'];
  223. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  224. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  225. $this->assertTrue($collection->setSlaveOkay(true));
  226. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  227. $this->assertTrue($collection->setSlaveOkay(false));
  228. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  229. }
  230. public function testReadPreferenceIsInherited()
  231. {
  232. $database = $this->getDatabase();
  233. $database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  234. $collection = $database->selectCollection('test');
  235. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  236. }
  237. public function testWriteConcern()
  238. {
  239. $collection = $this->getCollection();
  240. $this->assertSame(['w' => 1, 'wtimeout' => 0], $collection->getWriteConcern());
  241. $this->assertSame(1, $collection->w);
  242. $this->assertSame(0, $collection->wtimeout);
  243. $this->assertTrue($collection->setWriteConcern('majority', 100));
  244. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  245. $collection->w = 2;
  246. $this->assertSame(['w' => 2, 'wtimeout' => 100], $collection->getWriteConcern());
  247. $collection->wtimeout = -1;
  248. $this->assertSame(['w' => 2, 'wtimeout' => 0], $collection->getWriteConcern());
  249. // Only way to check whether options are passed down is through debugInfo
  250. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  251. $this->assertSame(2, $writeConcern->getW());
  252. $this->assertSame(0, $writeConcern->getWtimeout());
  253. }
  254. public function testWriteConcernIsInherited()
  255. {
  256. $database = $this->getDatabase();
  257. $database->setWriteConcern('majority', 100);
  258. $collection = $database->selectCollection('test');
  259. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  260. }
  261. public function testSaveInsert()
  262. {
  263. $id = '54203e08d51d4a1f868b456e';
  264. $collection = $this->getCollection();
  265. $collection->save(['_id' => new \MongoId($id), 'foo' => 'bar']);
  266. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  267. $this->assertSame(1, $newCollection->count());
  268. $object = $newCollection->findOne();
  269. $this->assertNotNull($object);
  270. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  271. $this->assertSame($id, (string) $object->_id);
  272. $this->assertObjectHasAttribute('foo', $object);
  273. $this->assertAttributeSame('bar', 'foo', $object);
  274. }
  275. public function testRemoveOne()
  276. {
  277. $id = '54203e08d51d4a1f868b456e';
  278. $collection = $this->getCollection();
  279. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  280. $collection->remove(['_id' => new \MongoId($id)]);
  281. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  282. $this->assertSame(0, $newCollection->count());
  283. }
  284. public function testSaveUpdate()
  285. {
  286. $id = '54203e08d51d4a1f868b456e';
  287. $collection = $this->getCollection();
  288. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  289. $collection->save(['_id' => new \MongoId($id), 'foo' => 'foo']);
  290. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  291. $this->assertSame(1, $newCollection->count());
  292. $object = $newCollection->findOne();
  293. $this->assertNotNull($object);
  294. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  295. $this->assertSame($id, (string) $object->_id);
  296. $this->assertObjectHasAttribute('foo', $object);
  297. $this->assertAttributeSame('foo', 'foo', $object);
  298. }
  299. public function testGetDBRef()
  300. {
  301. $collection = $this->getCollection();
  302. $collection->insert(['_id' => 1, 'foo' => 'bar']);
  303. $document = $collection->getDBRef([
  304. '$ref' => 'test',
  305. '$id' => 1,
  306. ]);
  307. $this->assertEquals(['_id' => 1, 'foo' => 'bar'], $document);
  308. }
  309. public function testCreateDBRef()
  310. {
  311. $collection = $this->getCollection();
  312. $reference = $collection->createDBRef(['_id' => 'foo']);
  313. $this->assertSame(
  314. [
  315. '$ref' => 'test',
  316. '$id' => 'foo',
  317. ],
  318. $reference
  319. );
  320. }
  321. public function testCreateIndex()
  322. {
  323. $collection = $this->getCollection();
  324. $collection->createIndex(['foo' => 1]);
  325. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  326. $iterator = $newCollection->listIndexes();
  327. $indexes = iterator_to_array($iterator);
  328. $this->assertCount(2, $indexes);
  329. $index = $indexes[1];
  330. $this->assertSame(['foo' => 1], $index->getKey());
  331. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  332. }
  333. public function testEnsureIndex()
  334. {
  335. $collection = $this->getCollection();
  336. $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
  337. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  338. $indexes = iterator_to_array($newCollection->listIndexes());
  339. $this->assertCount(2, $indexes);
  340. $index = $indexes[1];
  341. $this->assertSame(['bar' => 1], $index->getKey());
  342. $this->assertTrue($index->isUnique());
  343. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  344. }
  345. public function testDeleteIndexUsingIndexName()
  346. {
  347. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  348. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  349. $expected = [
  350. 'nIndexesWas' => 2,
  351. 'ok' => 1.0,
  352. ];
  353. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  354. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  355. }
  356. public function testDeleteIndexUsingKeys()
  357. {
  358. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  359. $newCollection->createIndex(['bar' => 1]);
  360. $expected = [
  361. 'nIndexesWas' => 2,
  362. 'ok' => 1.0,
  363. ];
  364. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  365. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  366. }
  367. public function testDeleteIndexes()
  368. {
  369. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  370. $newCollection->createIndex(['bar' => 1]);
  371. $expected = [
  372. 'nIndexesWas' => 2,
  373. 'msg' => 'non-_id indexes dropped for collection',
  374. 'ok' => 1.0,
  375. ];
  376. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  377. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  378. }
  379. public function testGetIndexInfo()
  380. {
  381. $collection = $this->getCollection();
  382. $collection->createIndex(['foo' => 1]);
  383. $expected = [
  384. [
  385. 'v' => 1,
  386. 'key' => ['_id' => 1],
  387. 'name' => '_id_',
  388. 'ns' => 'mongo-php-adapter.test',
  389. ],
  390. [
  391. 'v' => 1,
  392. 'key' => ['foo' => 1],
  393. 'name' => 'foo_1',
  394. 'ns' => 'mongo-php-adapter.test',
  395. ],
  396. ];
  397. $this->assertSame(
  398. $expected,
  399. $collection->getIndexInfo()
  400. );
  401. }
  402. public function testFindAndModifyUpdate()
  403. {
  404. $id = '54203e08d51d4a1f868b456e';
  405. $collection = $this->getCollection();
  406. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  407. $document = $collection->findAndModify(
  408. ['_id' => new \MongoId($id)],
  409. ['$set' => ['foo' => 'foo']]
  410. );
  411. $this->assertSame('bar', $document['foo']);
  412. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  413. $this->assertSame(1, $newCollection->count());
  414. $object = $newCollection->findOne();
  415. $this->assertNotNull($object);
  416. $this->assertAttributeSame('foo', 'foo', $object);
  417. }
  418. public function testFindAndModifyUpdateReturnNew()
  419. {
  420. $id = '54203e08d51d4a1f868b456e';
  421. $collection = $this->getCollection();
  422. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  423. $document = $collection->findAndModify(
  424. ['_id' => new \MongoId($id)],
  425. ['$set' => ['foo' => 'foo']],
  426. null,
  427. ['new' => true]
  428. );
  429. $this->assertSame('foo', $document['foo']);
  430. }
  431. public function testFindAndModifyWithFields()
  432. {
  433. $id = '54203e08d51d4a1f868b456e';
  434. $collection = $this->getCollection();
  435. $collection->insert([
  436. '_id' => new \MongoId($id),
  437. 'foo' => 'bar',
  438. 'bar' => 'foo',
  439. ]);
  440. $document = $collection->findAndModify(
  441. ['_id' => new \MongoId($id)],
  442. ['$set' => ['foo' => 'foo']],
  443. ['foo' => true]
  444. );
  445. $this->assertArrayNotHasKey('bar', $document);
  446. $this->assertArrayHasKey('foo', $document);
  447. }
  448. public function testGroup()
  449. {
  450. $collection = $this->getCollection();
  451. $collection->insert(['a' => 2]);
  452. $collection->insert(['b' => 5]);
  453. $collection->insert(['a' => 1]);
  454. $keys = [];
  455. $initial = ["count" => 0];
  456. $reduce = "function (obj, prev) { prev.count++; }";
  457. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  458. $result = $collection->group($keys, $initial, $reduce, $condition);
  459. $this->assertArraySubset(
  460. [
  461. 'retval' => [['count' => 1.0]],
  462. 'count' => 1.0,
  463. 'keys' => 1,
  464. 'ok' => 1.0,
  465. ],
  466. $result
  467. );
  468. }
  469. public function testFindAndModifyRemove()
  470. {
  471. $id = '54203e08d51d4a1f868b456e';
  472. $collection = $this->getCollection();
  473. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  474. $document = $collection->findAndModify(
  475. ['_id' => new \MongoId($id)],
  476. null,
  477. null,
  478. ['remove' => true]
  479. );
  480. $this->assertEquals('bar', $document['foo']);
  481. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  482. $this->assertSame(0, $newCollection->count());
  483. }
  484. public function testValidate()
  485. {
  486. $collection = $this->getCollection();
  487. $collection->insert(['foo' => 'bar']);
  488. $result = $collection->validate();
  489. $this->assertArraySubset(
  490. [
  491. 'ns' => 'mongo-php-adapter.test',
  492. 'nrecords' => 1,
  493. 'nIndexes' => 1,
  494. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  495. 'valid' => true,
  496. 'errors' => [],
  497. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  498. 'ok' => 1.0
  499. ],
  500. $result
  501. );
  502. }
  503. public function testDrop()
  504. {
  505. $this->getCollection()->insert(['foo' => 'bar']);
  506. $expected = [
  507. 'ns' => (string) $this->getCollection(),
  508. 'nIndexesWas' => 1,
  509. 'ok' => 1.0
  510. ];
  511. $this->assertSame($expected, $this->getCollection()->drop());
  512. }
  513. /**
  514. * @return \MongoCollection
  515. */
  516. protected function prepareData()
  517. {
  518. $collection = $this->getCollection();
  519. $collection->insert(['foo' => 'bar']);
  520. $collection->insert(['foo' => 'bar']);
  521. $collection->insert(['foo' => 'foo']);
  522. return $collection;
  523. }
  524. }