MongoCollectionTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 testSaveUpdate()
  276. {
  277. $id = '54203e08d51d4a1f868b456e';
  278. $collection = $this->getCollection();
  279. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  280. $collection->save(['_id' => new \MongoId($id), 'foo' => 'foo']);
  281. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  282. $this->assertSame(1, $newCollection->count());
  283. $object = $newCollection->findOne();
  284. $this->assertNotNull($object);
  285. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  286. $this->assertSame($id, (string) $object->_id);
  287. $this->assertObjectHasAttribute('foo', $object);
  288. $this->assertAttributeSame('foo', 'foo', $object);
  289. }
  290. public function testGetDBRef()
  291. {
  292. $collection = $this->getCollection();
  293. $collection->insert(['_id' => 1, 'foo' => 'bar']);
  294. $document = $collection->getDBRef([
  295. '$ref' => 'test',
  296. '$id' => 1,
  297. ]);
  298. $this->assertEquals(['_id' => 1, 'foo' => 'bar'], $document);
  299. }
  300. public function testCreateDBRef()
  301. {
  302. $collection = $this->getCollection();
  303. $reference = $collection->createDBRef(['_id' => 'foo']);
  304. $this->assertSame(
  305. [
  306. '$ref' => 'test',
  307. '$id' => 'foo',
  308. ],
  309. $reference
  310. );
  311. }
  312. public function testCreateIndex()
  313. {
  314. $collection = $this->getCollection();
  315. $collection->createIndex(['foo' => 1]);
  316. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  317. $iterator = $newCollection->listIndexes();
  318. $indexes = iterator_to_array($iterator);
  319. $this->assertCount(2, $indexes);
  320. $index = $indexes[1];
  321. $this->assertSame(['foo' => 1], $index->getKey());
  322. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  323. }
  324. public function testEnsureIndex()
  325. {
  326. $collection = $this->getCollection();
  327. $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
  328. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  329. $indexes = iterator_to_array($newCollection->listIndexes());
  330. $this->assertCount(2, $indexes);
  331. $index = $indexes[1];
  332. $this->assertSame(['bar' => 1], $index->getKey());
  333. $this->assertTrue($index->isUnique());
  334. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  335. }
  336. public function testDeleteIndexUsingIndexName()
  337. {
  338. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  339. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  340. $expected = [
  341. 'nIndexesWas' => 2,
  342. 'ok' => 1.0,
  343. ];
  344. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  345. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  346. }
  347. public function testDeleteIndexUsingKeys()
  348. {
  349. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  350. $newCollection->createIndex(['bar' => 1]);
  351. $expected = [
  352. 'nIndexesWas' => 2,
  353. 'ok' => 1.0,
  354. ];
  355. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  356. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  357. }
  358. public function testDeleteIndexes()
  359. {
  360. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  361. $newCollection->createIndex(['bar' => 1]);
  362. $expected = [
  363. 'nIndexesWas' => 2,
  364. 'msg' => 'non-_id indexes dropped for collection',
  365. 'ok' => 1.0,
  366. ];
  367. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  368. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  369. }
  370. public function testGetIndexInfo()
  371. {
  372. $collection = $this->getCollection();
  373. $collection->createIndex(['foo' => 1]);
  374. $expected = [
  375. [
  376. 'v' => 1,
  377. 'key' => ['_id' => 1],
  378. 'name' => '_id_',
  379. 'ns' => 'mongo-php-adapter.test',
  380. ],
  381. [
  382. 'v' => 1,
  383. 'key' => ['foo' => 1],
  384. 'name' => 'foo_1',
  385. 'ns' => 'mongo-php-adapter.test',
  386. ],
  387. ];
  388. $this->assertSame(
  389. $expected,
  390. $collection->getIndexInfo()
  391. );
  392. }
  393. public function testFindAndModifyUpdate()
  394. {
  395. $id = '54203e08d51d4a1f868b456e';
  396. $collection = $this->getCollection();
  397. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  398. $document = $collection->findAndModify(
  399. ['_id' => new \MongoId($id)],
  400. ['$set' => ['foo' => 'foo']]
  401. );
  402. $this->assertSame('bar', $document['foo']);
  403. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  404. $this->assertSame(1, $newCollection->count());
  405. $object = $newCollection->findOne();
  406. $this->assertNotNull($object);
  407. $this->assertAttributeSame('foo', 'foo', $object);
  408. }
  409. public function testFindAndModifyUpdateReturnNew()
  410. {
  411. $id = '54203e08d51d4a1f868b456e';
  412. $collection = $this->getCollection();
  413. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  414. $document = $collection->findAndModify(
  415. ['_id' => new \MongoId($id)],
  416. ['$set' => ['foo' => 'foo']],
  417. null,
  418. ['new' => true]
  419. );
  420. $this->assertSame('foo', $document['foo']);
  421. }
  422. public function testFindAndModifyWithFields()
  423. {
  424. $id = '54203e08d51d4a1f868b456e';
  425. $collection = $this->getCollection();
  426. $collection->insert([
  427. '_id' => new \MongoId($id),
  428. 'foo' => 'bar',
  429. 'bar' => 'foo',
  430. ]);
  431. $document = $collection->findAndModify(
  432. ['_id' => new \MongoId($id)],
  433. ['$set' => ['foo' => 'foo']],
  434. ['foo' => true]
  435. );
  436. $this->assertArrayNotHasKey('bar', $document);
  437. $this->assertArrayHasKey('foo', $document);
  438. }
  439. public function testGroup()
  440. {
  441. $collection = $this->getCollection();
  442. $collection->insert(['a' => 2]);
  443. $collection->insert(['b' => 5]);
  444. $collection->insert(['a' => 1]);
  445. $keys = [];
  446. $initial = ["count" => 0];
  447. $reduce = "function (obj, prev) { prev.count++; }";
  448. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  449. $result = $collection->group($keys, $initial, $reduce, $condition);
  450. $this->assertArraySubset(
  451. [
  452. 'retval' => [['count' => 1.0]],
  453. 'count' => 1.0,
  454. 'keys' => 1,
  455. 'ok' => 1.0,
  456. ],
  457. $result
  458. );
  459. }
  460. public function testFindAndModifyRemove()
  461. {
  462. $id = '54203e08d51d4a1f868b456e';
  463. $collection = $this->getCollection();
  464. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  465. $document = $collection->findAndModify(
  466. ['_id' => new \MongoId($id)],
  467. null,
  468. null,
  469. ['remove' => true]
  470. );
  471. $this->assertEquals('bar', $document['foo']);
  472. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  473. $this->assertSame(0, $newCollection->count());
  474. }
  475. public function testValidate()
  476. {
  477. $collection = $this->getCollection();
  478. $collection->insert(['foo' => 'bar']);
  479. $result = $collection->validate();
  480. $this->assertArraySubset(
  481. [
  482. 'ns' => 'mongo-php-adapter.test',
  483. 'nrecords' => 1,
  484. 'nIndexes' => 1,
  485. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  486. 'valid' => true,
  487. 'errors' => [],
  488. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  489. 'ok' => 1.0
  490. ],
  491. $result
  492. );
  493. }
  494. public function testDrop()
  495. {
  496. $this->getCollection()->insert(['foo' => 'bar']);
  497. $expected = [
  498. 'ns' => (string) $this->getCollection(),
  499. 'nIndexesWas' => 1,
  500. 'ok' => 1.0
  501. ];
  502. $this->assertSame($expected, $this->getCollection()->drop());
  503. }
  504. /**
  505. * @return \MongoCollection
  506. */
  507. protected function prepareData()
  508. {
  509. $collection = $this->getCollection();
  510. $collection->insert(['foo' => 'bar']);
  511. $collection->insert(['foo' => 'bar']);
  512. $collection->insert(['foo' => 'foo']);
  513. return $collection;
  514. }
  515. }