MongoCollectionTest.php 21 KB

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