MongoCollectionTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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. $collection = $this->getCollection();
  17. $expected = [
  18. 'ok' => 1.0,
  19. 'n' => 0,
  20. 'err' => null,
  21. 'errmsg' => null,
  22. ];
  23. $document = ['foo' => 'bar'];
  24. $this->assertSame($expected, $collection->insert($document));
  25. $this->assertInstanceOf('MongoId', $document['_id']);
  26. $id = (string) $document['_id'];
  27. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  28. $this->assertSame(1, $newCollection->count());
  29. $object = $newCollection->findOne();
  30. $this->assertNotNull($object);
  31. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  32. $this->assertSame($id, (string) $object->_id);
  33. $this->assertObjectHasAttribute('foo', $object);
  34. $this->assertAttributeSame('bar', 'foo', $object);
  35. }
  36. public function testInsertInvalidData()
  37. {
  38. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
  39. $document = 8;
  40. $this->getCollection()->insert($document);
  41. }
  42. public function testInsertEmptyArray()
  43. {
  44. $document = [];
  45. $this->getCollection()->insert($document);
  46. $this->assertSame(1, $this->getCollection()->count());
  47. }
  48. public function testInsertArrayWithNumericKeys()
  49. {
  50. $document = [1 => 'foo'];
  51. $this->getCollection()->insert($document);
  52. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  53. }
  54. public function testInsertEmptyObject()
  55. {
  56. $document = (object) [];
  57. $this->getCollection()->insert($document);
  58. $this->assertSame(1, $this->getCollection()->count());
  59. }
  60. public function testInsertObjectWithPrivateProperties()
  61. {
  62. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  63. $document = new PrivatePropertiesStub();
  64. $this->getCollection()->insert($document);
  65. }
  66. public function testInsertDuplicate()
  67. {
  68. $collection = $this->getCollection();
  69. $collection->createIndex(['foo' => 1], ['unique' => true]);
  70. $document = ['foo' => 'bar'];
  71. $collection->insert($document);
  72. unset($document['_id']);
  73. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
  74. $collection->insert($document);
  75. }
  76. public function testUnacknowledgedWrite()
  77. {
  78. $document = ['foo' => 'bar'];
  79. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  80. }
  81. public function testInsertWriteConcernException()
  82. {
  83. $this->setExpectedException(
  84. 'MongoWriteConcernException',
  85. "cannot use 'w' > 1 when a host is not replicated"
  86. );
  87. $document = ['foo' => 'bar'];
  88. $this->getCollection()->insert($document, ['w' => 2]);
  89. }
  90. public function testInsertMany()
  91. {
  92. $expected = [
  93. 'ok' => 1.0,
  94. 'n' => 0,
  95. 'syncMillis' => 0,
  96. 'writtenTo' => null,
  97. 'err' => null,
  98. ];
  99. $documents = [
  100. ['foo' => 'bar'],
  101. ['bar' => 'foo']
  102. ];
  103. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  104. foreach ($documents as $document) {
  105. $this->assertInstanceOf('MongoId', $document['_id']);
  106. }
  107. }
  108. public function testInsertManyWithNonNumericKeys()
  109. {
  110. $expected = [
  111. 'ok' => 1.0,
  112. 'n' => 0,
  113. 'syncMillis' => 0,
  114. 'writtenTo' => null,
  115. 'err' => null,
  116. ];
  117. $documents = [
  118. 'a' => ['foo' => 'bar'],
  119. 'b' => ['bar' => 'foo']
  120. ];
  121. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  122. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  123. $this->assertSame(2, $newCollection->count());
  124. }
  125. public function testBatchInsertContinuesOnError()
  126. {
  127. $expected = [
  128. 'ok' => 1.0,
  129. 'n' => 0,
  130. 'syncMillis' => 0,
  131. 'writtenTo' => null,
  132. 'err' => null,
  133. ];
  134. $documents = [
  135. 8,
  136. 'b' => ['bar' => 'foo']
  137. ];
  138. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  139. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  140. $this->assertSame(1, $newCollection->count());
  141. }
  142. public function testBatchInsertException()
  143. {
  144. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
  145. $id = new \MongoId();
  146. $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
  147. $this->getCollection()->batchInsert($documents);
  148. }
  149. public function testBatchInsertEmptyBatchException()
  150. {
  151. $this->setExpectedException('MongoException', 'No write ops were included in the batch');
  152. $documents = [];
  153. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  154. }
  155. public function testUpdateWriteConcern()
  156. {
  157. $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
  158. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  159. }
  160. public function testUpdateOne()
  161. {
  162. $document = ['foo' => 'bar'];
  163. $this->getCollection()->insert($document);
  164. // Unset ID to re-insert
  165. unset($document['_id']);
  166. $this->getCollection()->insert($document);
  167. $expected = [
  168. 'ok' => 1.0,
  169. 'nModified' => 1,
  170. 'n' => 1,
  171. 'err' => null,
  172. 'errmsg' => null,
  173. 'updatedExisting' => true,
  174. ];
  175. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  176. $this->assertSame($expected, $result);
  177. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  178. }
  179. public function testUpdateReplaceOne()
  180. {
  181. $document = ['foo' => 'bar', 'bar' => 'foo'];
  182. $this->getCollection()->insert($document);
  183. // Unset ID to re-insert
  184. unset($document['_id']);
  185. $this->getCollection()->insert($document);
  186. $expected = [
  187. 'ok' => 1.0,
  188. 'nModified' => 1,
  189. 'n' => 1,
  190. 'err' => null,
  191. 'errmsg' => null,
  192. 'updatedExisting' => true,
  193. ];
  194. $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
  195. $this->assertSame($expected, $result);
  196. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  197. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
  198. }
  199. public function testUpdateReplaceMultiple()
  200. {
  201. $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
  202. $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
  203. }
  204. public function testUpdateDuplicate()
  205. {
  206. $collection = $this->getCollection();
  207. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  208. $document = ['foo' => 'bar'];
  209. $collection->insert($document);
  210. $document = ['foo' => 'foo'];
  211. $collection->insert($document);
  212. $this->setExpectedException('MongoDuplicateKeyException');
  213. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  214. }
  215. public function testUpdateMany()
  216. {
  217. $document = ['change' => true, 'foo' => 'bar'];
  218. $this->getCollection()->insert($document);
  219. unset($document['_id']);
  220. $this->getCollection()->insert($document);
  221. $document = ['change' => true, 'foo' => 'foo'];
  222. $this->getCollection()->insert($document);
  223. $expected = [
  224. 'ok' => 1.0,
  225. 'nModified' => 2,
  226. 'n' => 3,
  227. 'err' => null,
  228. 'errmsg' => null,
  229. 'updatedExisting' => true,
  230. ];
  231. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  232. $this->assertSame($expected, $result);
  233. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  234. }
  235. public function testUnacknowledgedUpdate()
  236. {
  237. $document = ['foo' => 'bar'];
  238. $this->getCollection()->insert($document);
  239. unset($document['_id']);
  240. $this->getCollection()->insert($document);
  241. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  242. }
  243. public function testRemoveMultiple()
  244. {
  245. $document = ['change' => true, 'foo' => 'bar'];
  246. $this->getCollection()->insert($document);
  247. unset($document['_id']);
  248. $this->getCollection()->insert($document);
  249. $document = ['change' => true, 'foo' => 'foo'];
  250. $this->getCollection()->insert($document);
  251. $expected = [
  252. 'ok' => 1.0,
  253. 'n' => 2,
  254. 'err' => null,
  255. 'errmsg' => null,
  256. ];
  257. $result = $this->getCollection()->remove(['foo' => 'bar']);
  258. $this->assertSame($expected, $result);
  259. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  260. }
  261. public function testRemoveSingle()
  262. {
  263. $document = ['change' => true, 'foo' => 'bar'];
  264. $this->getCollection()->insert($document);
  265. unset($document['_id']);
  266. $this->getCollection()->insert($document);
  267. unset($document['_id']);
  268. $this->getCollection()->insert($document);
  269. $expected = [
  270. 'ok' => 1.0,
  271. 'n' => 1,
  272. 'err' => null,
  273. 'errmsg' => null,
  274. ];
  275. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  276. $this->assertSame($expected, $result);
  277. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  278. }
  279. public function testRemoveUnacknowledged()
  280. {
  281. $document = ['change' => true, 'foo' => 'bar'];
  282. $this->getCollection()->insert($document);
  283. unset($document['_id']);
  284. $this->getCollection()->insert($document);
  285. unset($document['_id']);
  286. $this->getCollection()->insert($document);
  287. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  288. }
  289. public function testFindReturnsCursor()
  290. {
  291. $this->prepareData();
  292. $collection = $this->getCollection();
  293. $this->assertInstanceOf('MongoCursor', $collection->find());
  294. }
  295. public function testCount()
  296. {
  297. $this->prepareData();
  298. $collection = $this->getCollection();
  299. $this->assertSame(3, $collection->count());
  300. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  301. }
  302. public function testCountTimeout()
  303. {
  304. $this->failMaxTimeMS();
  305. $this->setExpectedException('MongoExecutionTimeoutException');
  306. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  307. }
  308. public function testFindOne()
  309. {
  310. $this->prepareData();
  311. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  312. $this->assertSame(['foo' => 'foo'], $document);
  313. }
  314. public function testFindOneConnectionIssue()
  315. {
  316. $this->setExpectedException('MongoConnectionException');
  317. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  318. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  319. $collection->findOne();
  320. }
  321. public function testDistinct()
  322. {
  323. $this->prepareData();
  324. $values = $this->getCollection()->distinct('foo');
  325. $this->assertInternalType('array', $values);
  326. sort($values);
  327. $this->assertEquals(['bar', 'foo'], $values);
  328. }
  329. public function testDistinctWithQuery()
  330. {
  331. $this->prepareData();
  332. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  333. $this->assertInternalType('array', $values);
  334. $this->assertEquals(['bar'], $values);
  335. }
  336. public function testAggregate()
  337. {
  338. $this->skipTestUnless(extension_loaded('mongo'));
  339. $collection = $this->getCollection();
  340. $this->prepareData();
  341. $pipeline = [
  342. [
  343. '$group' => [
  344. '_id' => '$foo',
  345. 'count' => [ '$sum' => 1 ],
  346. ],
  347. ],
  348. [
  349. '$sort' => ['_id' => 1]
  350. ]
  351. ];
  352. $result = $collection->aggregate($pipeline);
  353. $this->assertInternalType('array', $result);
  354. $this->assertArrayHasKey('result', $result);
  355. $this->assertEquals([
  356. ['_id' => 'bar', 'count' => 2],
  357. ['_id' => 'foo', 'count' => 1],
  358. ], $result['result']);
  359. }
  360. public function testAggregateTimeoutException()
  361. {
  362. $this->skipTestUnless(extension_loaded('mongo'));
  363. $collection = $this->getCollection();
  364. $this->failMaxTimeMS();
  365. $this->setExpectedException('MongoExecutionTimeoutException');
  366. $pipeline = [
  367. [
  368. '$group' => [
  369. '_id' => '$foo',
  370. 'count' => [ '$sum' => 1 ],
  371. ],
  372. ],
  373. [
  374. '$sort' => ['_id' => 1]
  375. ]
  376. ];
  377. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  378. }
  379. public function testAggregateCursor()
  380. {
  381. $collection = $this->getCollection();
  382. $this->prepareData();
  383. $pipeline = [
  384. [
  385. '$group' => [
  386. '_id' => '$foo',
  387. 'count' => [ '$sum' => 1 ],
  388. ],
  389. ],
  390. [
  391. '$sort' => ['_id' => 1]
  392. ]
  393. ];
  394. $cursor = $collection->aggregateCursor($pipeline);
  395. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  396. $this->assertEquals([
  397. ['_id' => 'bar', 'count' => 2],
  398. ['_id' => 'foo', 'count' => 1],
  399. ], iterator_to_array($cursor));
  400. }
  401. public function testReadPreference()
  402. {
  403. $collection = $this->getCollection();
  404. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  405. $this->assertFalse($collection->getSlaveOkay());
  406. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  407. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  408. $this->assertTrue($collection->getSlaveOkay());
  409. $this->assertTrue($collection->setSlaveOkay(true));
  410. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  411. $this->assertTrue($collection->setSlaveOkay(false));
  412. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  413. }
  414. public function testReadPreferenceIsSetInDriver()
  415. {
  416. $this->skipTestIf(extension_loaded('mongo'));
  417. $collection = $this->getCollection();
  418. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  419. // Only way to check whether options are passed down is through debugInfo
  420. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  421. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  422. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  423. }
  424. public function testReadPreferenceIsInherited()
  425. {
  426. $database = $this->getDatabase();
  427. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  428. $collection = $database->selectCollection('test');
  429. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  430. }
  431. public function testWriteConcern()
  432. {
  433. $collection = $this->getCollection();
  434. $this->assertTrue($collection->setWriteConcern('majority', 100));
  435. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  436. }
  437. public function testWriteConcernIsSetInDriver()
  438. {
  439. $this->skipTestIf(extension_loaded('mongo'));
  440. $collection = $this->getCollection();
  441. $this->assertTrue($collection->setWriteConcern(2, 100));
  442. // Only way to check whether options are passed down is through debugInfo
  443. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  444. $this->assertSame(2, $writeConcern->getW());
  445. $this->assertSame(100, $writeConcern->getWtimeout());
  446. }
  447. public function testWriteConcernIsInherited()
  448. {
  449. $database = $this->getDatabase();
  450. $database->setWriteConcern('majority', 100);
  451. $collection = $database->selectCollection('test');
  452. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  453. }
  454. public function testSaveInsert()
  455. {
  456. $id = '54203e08d51d4a1f868b456e';
  457. $collection = $this->getCollection();
  458. $objectId = new \MongoId($id);
  459. $expected = [
  460. 'ok' => 1.0,
  461. 'nModified' => 0,
  462. 'n' => 1,
  463. 'err' => null,
  464. 'errmsg' => null,
  465. 'upserted' => $objectId,
  466. 'updatedExisting' => false,
  467. ];
  468. $document = ['_id' => $objectId, 'foo' => 'bar'];
  469. $this->assertEquals($expected, $collection->save($document));
  470. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  471. $this->assertSame(1, $newCollection->count());
  472. $object = $newCollection->findOne();
  473. $this->assertNotNull($object);
  474. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  475. $this->assertSame($id, (string) $object->_id);
  476. $this->assertObjectHasAttribute('foo', $object);
  477. $this->assertAttributeSame('bar', 'foo', $object);
  478. }
  479. public function testRemoveOne()
  480. {
  481. $id = '54203e08d51d4a1f868b456e';
  482. $collection = $this->getCollection();
  483. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  484. $collection->insert($document);
  485. $collection->remove(['_id' => new \MongoId($id)]);
  486. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  487. $this->assertSame(0, $newCollection->count());
  488. }
  489. public function testSaveUpdate()
  490. {
  491. $expected = [
  492. 'ok' => 1.0,
  493. 'nModified' => 1,
  494. 'n' => 1,
  495. 'err' => null,
  496. 'errmsg' => null,
  497. 'updatedExisting' => true,
  498. ];
  499. $id = '54203e08d51d4a1f868b456e';
  500. $collection = $this->getCollection();
  501. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  502. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  503. $collection->insert($insertDocument);
  504. $this->assertSame($expected, $collection->save($saveDocument));
  505. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  506. $this->assertSame(1, $newCollection->count());
  507. $object = $newCollection->findOne();
  508. $this->assertNotNull($object);
  509. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  510. $this->assertSame($id, (string) $object->_id);
  511. $this->assertObjectHasAttribute('foo', $object);
  512. $this->assertAttributeSame('foo', 'foo', $object);
  513. }
  514. public function testSavingShouldReplaceTheWholeDocument() {
  515. $id = '54203e08d51d4a1f868b456e';
  516. $collection = $this->getCollection();
  517. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  518. $saveDocument = ['_id' => new \MongoId($id)];
  519. $collection->insert($insertDocument);
  520. $collection->save($saveDocument);
  521. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  522. $this->assertSame(1, $newCollection->count());
  523. $object = $newCollection->findOne();
  524. $this->assertNotNull($object);
  525. $this->assertObjectNotHasAttribute('foo', $object);
  526. }
  527. public function testSaveDuplicate()
  528. {
  529. $collection = $this->getCollection();
  530. $collection->createIndex(['foo' => 1], ['unique' => true]);
  531. $document = ['foo' => 'bar'];
  532. $collection->save($document);
  533. $this->setExpectedException('MongoDuplicateKeyException');
  534. unset($document['_id']);
  535. $collection->save($document);
  536. }
  537. public function testSaveEmptyKeys()
  538. {
  539. $document = [];
  540. $this->getCollection()->save($document);
  541. $this->assertSame(1, $this->getCollection()->count());
  542. }
  543. public function testSaveEmptyObject()
  544. {
  545. $document = (object) [];
  546. $this->getCollection()->save($document);
  547. $this->assertSame(1, $this->getCollection()->count());
  548. }
  549. public function testGetDBRef()
  550. {
  551. $collection = $this->getCollection();
  552. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  553. $collection->insert($insertDocument);
  554. $document = $collection->getDBRef([
  555. '$ref' => 'test',
  556. '$id' => 1,
  557. ]);
  558. $this->assertEquals($insertDocument, $document);
  559. }
  560. public function testCreateDBRef()
  561. {
  562. $collection = $this->getCollection();
  563. $reference = $collection->createDBRef(['_id' => 'foo']);
  564. $this->assertSame(
  565. [
  566. '$ref' => 'test',
  567. '$id' => 'foo',
  568. ],
  569. $reference
  570. );
  571. }
  572. public function testCreateIndex()
  573. {
  574. $expected = [
  575. 'createdCollectionAutomatically' => true,
  576. 'numIndexesBefore' => 1,
  577. 'numIndexesAfter' => 2,
  578. 'ok' => 1.0,
  579. ];
  580. $collection = $this->getCollection();
  581. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  582. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  583. $iterator = $newCollection->listIndexes();
  584. $indexes = iterator_to_array($iterator);
  585. $this->assertCount(2, $indexes);
  586. $index = $indexes[1];
  587. $this->assertSame(['foo' => 1], $index->getKey());
  588. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  589. }
  590. public function testCreateIndexInvalid()
  591. {
  592. $this->setExpectedException('MongoException', 'index specification has no elements');
  593. $this->getCollection()->createIndex([]);
  594. }
  595. public function testCreateIndexTwice()
  596. {
  597. $this->getCollection()->createIndex(['foo' => 1]);
  598. $expected = [
  599. 'createdCollectionAutomatically' => false,
  600. 'numIndexesBefore' => 2,
  601. 'numIndexesAfter' => 2,
  602. 'note' => 'all indexes already exist',
  603. 'ok' => 1.0
  604. ];
  605. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  606. }
  607. public function testCreateIndexesWithDifferentOptions()
  608. {
  609. $this->setExpectedException('MongoResultException');
  610. $this->getCollection()->createIndex(['foo' => 1]);
  611. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  612. }
  613. public function testCreateIndexWithSameName()
  614. {
  615. $this->setExpectedException('MongoResultException');
  616. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  617. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  618. }
  619. public function testEnsureIndex()
  620. {
  621. $expected = [
  622. 'createdCollectionAutomatically' => true,
  623. 'numIndexesBefore' => 1,
  624. 'numIndexesAfter' => 2,
  625. 'ok' => 1.0
  626. ];
  627. $collection = $this->getCollection();
  628. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  629. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  630. $indexes = iterator_to_array($newCollection->listIndexes());
  631. $this->assertCount(2, $indexes);
  632. $index = $indexes[1];
  633. $this->assertSame(['bar' => 1], $index->getKey());
  634. $this->assertTrue($index->isUnique());
  635. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  636. }
  637. public function testDeleteIndexUsingIndexName()
  638. {
  639. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  640. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  641. $expected = [
  642. 'nIndexesWas' => 2,
  643. 'errmsg' => 'index not found with name [bar_1]',
  644. 'ok' => 0.0,
  645. 'code' => 27,
  646. ];
  647. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  648. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  649. }
  650. public function testDeleteIndexUsingField()
  651. {
  652. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  653. $newCollection->createIndex(['bar' => 1]);
  654. $expected = [
  655. 'nIndexesWas' => 2,
  656. 'ok' => 1.0,
  657. ];
  658. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  659. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  660. }
  661. public function testDeleteIndexUsingKeys()
  662. {
  663. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  664. $newCollection->createIndex(['bar' => 1]);
  665. $expected = [
  666. 'nIndexesWas' => 2,
  667. 'ok' => 1.0,
  668. ];
  669. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  670. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  671. }
  672. public function testDeleteIndexes()
  673. {
  674. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  675. $newCollection->createIndex(['bar' => 1]);
  676. $expected = [
  677. 'nIndexesWas' => 2,
  678. 'msg' => 'non-_id indexes dropped for collection',
  679. 'ok' => 1.0,
  680. ];
  681. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  682. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  683. }
  684. public function testGetIndexInfo()
  685. {
  686. $collection = $this->getCollection();
  687. $collection->createIndex(['foo' => 1]);
  688. $expected = [
  689. [
  690. 'v' => 1,
  691. 'key' => ['_id' => 1],
  692. 'name' => '_id_',
  693. 'ns' => 'mongo-php-adapter.test',
  694. ],
  695. [
  696. 'v' => 1,
  697. 'key' => ['foo' => 1],
  698. 'name' => 'foo_1',
  699. 'ns' => 'mongo-php-adapter.test',
  700. ],
  701. ];
  702. $this->assertSame(
  703. $expected,
  704. $collection->getIndexInfo()
  705. );
  706. }
  707. public function testFindAndModifyUpdate()
  708. {
  709. $id = '54203e08d51d4a1f868b456e';
  710. $collection = $this->getCollection();
  711. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  712. $collection->insert($document);
  713. $document = $collection->findAndModify(
  714. ['_id' => new \MongoId($id)],
  715. ['$set' => ['foo' => 'foo']]
  716. );
  717. $this->assertSame('bar', $document['foo']);
  718. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  719. $this->assertSame(1, $newCollection->count());
  720. $object = $newCollection->findOne();
  721. $this->assertNotNull($object);
  722. $this->assertAttributeSame('foo', 'foo', $object);
  723. }
  724. public function testFindAndModifyUpdateReplace()
  725. {
  726. $id = '54203e08d51d4a1f868b456e';
  727. $collection = $this->getCollection();
  728. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  729. $collection->insert($document);
  730. $document = $collection->findAndModify(
  731. ['_id' => new \MongoId($id)],
  732. ['_id' => new \MongoId($id), 'foo' => 'boo']
  733. );
  734. $this->assertSame('bar', $document['foo']);
  735. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  736. $this->assertSame(1, $newCollection->count());
  737. $object = $newCollection->findOne();
  738. $this->assertNotNull($object);
  739. $this->assertAttributeSame('boo', 'foo', $object);
  740. $this->assertObjectNotHasAttribute('bar', $object);
  741. }
  742. public function testFindAndModifyUpdateReturnNew()
  743. {
  744. $id = '54203e08d51d4a1f868b456e';
  745. $collection = $this->getCollection();
  746. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  747. $collection->insert($document);
  748. $document = $collection->findAndModify(
  749. ['_id' => new \MongoId($id)],
  750. ['$set' => ['foo' => 'foo']],
  751. null,
  752. ['new' => true]
  753. );
  754. $this->assertSame('foo', $document['foo']);
  755. }
  756. public function testFindAndModifyWithFields()
  757. {
  758. $id = '54203e08d51d4a1f868b456e';
  759. $collection = $this->getCollection();
  760. $document = [
  761. '_id' => new \MongoId($id),
  762. 'foo' => 'bar',
  763. 'bar' => 'foo',
  764. ];
  765. $collection->insert($document);
  766. $document = $collection->findAndModify(
  767. ['_id' => new \MongoId($id)],
  768. ['$set' => ['foo' => 'foo']],
  769. ['foo' => true]
  770. );
  771. $this->assertArrayNotHasKey('bar', $document);
  772. $this->assertArrayHasKey('foo', $document);
  773. }
  774. public function testGroup()
  775. {
  776. $collection = $this->getCollection();
  777. $document1 = ['a' => 2];
  778. $collection->insert($document1);
  779. $document2 = ['b' => 5];
  780. $collection->insert($document2);
  781. $document3 = ['a' => 1];
  782. $collection->insert($document3);
  783. $keys = [];
  784. $initial = ["count" => 0];
  785. $reduce = "function (obj, prev) { prev.count++; }";
  786. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  787. $result = $collection->group($keys, $initial, $reduce, $condition);
  788. $this->assertArraySubset(
  789. [
  790. 'retval' => [['count' => 1.0]],
  791. 'count' => 1.0,
  792. 'keys' => 1,
  793. 'ok' => 1.0,
  794. ],
  795. $result
  796. );
  797. }
  798. public function testMapReduce()
  799. {
  800. $data = array(
  801. array(
  802. 'username' => 'jones',
  803. 'likes' => 20.0,
  804. 'text' => 'Hello world!'
  805. ),
  806. array(
  807. 'username' => 'bob',
  808. 'likes' => 100.0,
  809. 'text' => 'Hello world!'
  810. ),
  811. array(
  812. 'username' => 'bob',
  813. 'likes' => 100.0,
  814. 'text' => 'Hello world!'
  815. ),
  816. );
  817. $collection = $this->getCollection();
  818. $collection->batchInsert($data);
  819. $map = 'function() {
  820. emit(this.username, { count: 1, likes: this.likes });
  821. }';
  822. $reduce = 'function(key, values) {
  823. var result = {count: 0, likes: 0};
  824. values.forEach(function(value) {
  825. result.count += value.count;
  826. result.likes += value.likes;
  827. });
  828. return result;
  829. }';
  830. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  831. $command = [
  832. 'mapreduce' => $this->getCollection()->getName(),
  833. 'map' => new \MongoCode($map),
  834. 'reduce' => new \MongoCode($reduce),
  835. 'query' => (object) [],
  836. 'out' => ['inline' => true],
  837. 'finalize' => new \MongoCode($finalize),
  838. ];
  839. $result = $this->getDatabase()->command($command);
  840. $expected = [
  841. [
  842. '_id' => 'bob',
  843. 'value' => [
  844. 'count' => 2.0,
  845. 'likes' => 200.0,
  846. 'test' => 'test',
  847. ],
  848. ],
  849. [
  850. '_id' => 'jones',
  851. 'value' => [
  852. 'count' => 1.0,
  853. 'likes' => 20.0,
  854. 'test' => 'test',
  855. ],
  856. ],
  857. ];
  858. $this->assertSame(1.0, $result['ok']);
  859. $this->assertSame($expected, $result['results']);
  860. }
  861. public function testFindAndModifyResultException()
  862. {
  863. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  864. $collection = $this->getCollection();
  865. $this->setExpectedException('MongoResultException');
  866. $collection->findAndModify(
  867. array("inprogress" => false, "name" => "Next promo"),
  868. array('$unsupportedOperator' => array("tasks" => -1)),
  869. array("tasks" => true),
  870. array("new" => true)
  871. );
  872. }
  873. public function testFindAndModifyExceptionTimeout()
  874. {
  875. $this->failMaxTimeMS();
  876. $id = '54203e08d51d4a1f868b456e';
  877. $collection = $this->getCollection();
  878. $this->setExpectedException('MongoExecutionTimeoutException');
  879. $document = $collection->findAndModify(
  880. ['_id' => new \MongoId($id)],
  881. null,
  882. null,
  883. ['maxTimeMS' => 1, 'remove' => true]
  884. );
  885. }
  886. public function testFindAndModifyRemove()
  887. {
  888. $id = '54203e08d51d4a1f868b456e';
  889. $collection = $this->getCollection();
  890. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  891. $collection->insert($document);
  892. $document = $collection->findAndModify(
  893. ['_id' => new \MongoId($id)],
  894. null,
  895. null,
  896. ['remove' => true]
  897. );
  898. $this->assertEquals('bar', $document['foo']);
  899. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  900. $this->assertSame(0, $newCollection->count());
  901. }
  902. public function testValidate()
  903. {
  904. $collection = $this->getCollection();
  905. $document = ['foo' => 'bar'];
  906. $collection->insert($document);
  907. $result = $collection->validate();
  908. $this->assertArraySubset(
  909. [
  910. 'ns' => 'mongo-php-adapter.test',
  911. 'nrecords' => 1,
  912. 'nIndexes' => 1,
  913. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  914. 'valid' => true,
  915. 'errors' => [],
  916. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  917. 'ok' => 1.0
  918. ],
  919. $result
  920. );
  921. }
  922. public function testDrop()
  923. {
  924. $document = ['foo' => 'bar'];
  925. $this->getCollection()->insert($document);
  926. $expected = [
  927. 'ns' => (string) $this->getCollection(),
  928. 'nIndexesWas' => 1,
  929. 'ok' => 1.0
  930. ];
  931. $this->assertSame($expected, $this->getCollection()->drop());
  932. }
  933. public function testEmptyCollectionName()
  934. {
  935. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  936. new \MongoCollection($this->getDatabase(), '');
  937. }
  938. public function testSelectCollectionWithNullBytes()
  939. {
  940. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  941. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  942. }
  943. public function testSubCollectionWithNullBytes()
  944. {
  945. $collection = $this->getCollection();
  946. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  947. $this->assertSame('test', $collection->getName());
  948. }
  949. }
  950. class PrivatePropertiesStub
  951. {
  952. private $foo = 'bar';
  953. }