MongoCollectionTest.php 18 KB

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