MongoCollectionTest.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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->setExpectedException('MongoDuplicateKeyException', 'E11000 duplicate key error index: 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 testUpdateDuplicate()
  180. {
  181. $collection = $this->getCollection();
  182. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  183. $document = ['foo' => 'bar'];
  184. $collection->insert($document);
  185. $document = ['foo' => 'foo'];
  186. $collection->insert($document);
  187. $this->setExpectedException('MongoDuplicateKeyException');
  188. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  189. }
  190. public function testUpdateMany()
  191. {
  192. $document = ['change' => true, 'foo' => 'bar'];
  193. $this->getCollection()->insert($document);
  194. unset($document['_id']);
  195. $this->getCollection()->insert($document);
  196. $document = ['change' => true, 'foo' => 'foo'];
  197. $this->getCollection()->insert($document);
  198. $expected = [
  199. 'ok' => 1.0,
  200. 'nModified' => 2,
  201. 'n' => 3,
  202. 'err' => null,
  203. 'errmsg' => null,
  204. 'updatedExisting' => true,
  205. ];
  206. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  207. $this->assertSame($expected, $result);
  208. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  209. }
  210. public function testUnacknowledgedUpdate()
  211. {
  212. $document = ['foo' => 'bar'];
  213. $this->getCollection()->insert($document);
  214. unset($document['_id']);
  215. $this->getCollection()->insert($document);
  216. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  217. }
  218. public function testRemoveMultiple()
  219. {
  220. $document = ['change' => true, 'foo' => 'bar'];
  221. $this->getCollection()->insert($document);
  222. unset($document['_id']);
  223. $this->getCollection()->insert($document);
  224. $document = ['change' => true, 'foo' => 'foo'];
  225. $this->getCollection()->insert($document);
  226. $expected = [
  227. 'ok' => 1.0,
  228. 'n' => 2,
  229. 'err' => null,
  230. 'errmsg' => null,
  231. ];
  232. $result = $this->getCollection()->remove(['foo' => 'bar']);
  233. $this->assertSame($expected, $result);
  234. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  235. }
  236. public function testRemoveSingle()
  237. {
  238. $document = ['change' => true, 'foo' => 'bar'];
  239. $this->getCollection()->insert($document);
  240. unset($document['_id']);
  241. $this->getCollection()->insert($document);
  242. unset($document['_id']);
  243. $this->getCollection()->insert($document);
  244. $expected = [
  245. 'ok' => 1.0,
  246. 'n' => 1,
  247. 'err' => null,
  248. 'errmsg' => null,
  249. ];
  250. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  251. $this->assertSame($expected, $result);
  252. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  253. }
  254. public function testRemoveUnacknowledged()
  255. {
  256. $document = ['change' => true, 'foo' => 'bar'];
  257. $this->getCollection()->insert($document);
  258. unset($document['_id']);
  259. $this->getCollection()->insert($document);
  260. unset($document['_id']);
  261. $this->getCollection()->insert($document);
  262. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  263. }
  264. public function testFindReturnsCursor()
  265. {
  266. $this->prepareData();
  267. $collection = $this->getCollection();
  268. $this->assertInstanceOf('MongoCursor', $collection->find());
  269. }
  270. public function testCount()
  271. {
  272. $this->prepareData();
  273. $collection = $this->getCollection();
  274. $this->assertSame(3, $collection->count());
  275. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  276. }
  277. public function testCountTimeout()
  278. {
  279. $this->failMaxTimeMS();
  280. $this->setExpectedException('MongoExecutionTimeoutException');
  281. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  282. }
  283. public function testFindOne()
  284. {
  285. $this->prepareData();
  286. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  287. $this->assertSame(['foo' => 'foo'], $document);
  288. }
  289. public function testFindOneConnectionIssue()
  290. {
  291. $this->setExpectedException('MongoConnectionException');
  292. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  293. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  294. $collection->findOne();
  295. }
  296. public function testDistinct()
  297. {
  298. $this->prepareData();
  299. $values = $this->getCollection()->distinct('foo');
  300. $this->assertInternalType('array', $values);
  301. sort($values);
  302. $this->assertEquals(['bar', 'foo'], $values);
  303. }
  304. public function testDistinctWithQuery()
  305. {
  306. $this->prepareData();
  307. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  308. $this->assertInternalType('array', $values);
  309. $this->assertEquals(['bar'], $values);
  310. }
  311. public function testAggregate()
  312. {
  313. $this->skipTestUnless(extension_loaded('mongo'));
  314. $collection = $this->getCollection();
  315. $this->prepareData();
  316. $pipeline = [
  317. [
  318. '$group' => [
  319. '_id' => '$foo',
  320. 'count' => [ '$sum' => 1 ],
  321. ],
  322. ],
  323. [
  324. '$sort' => ['_id' => 1]
  325. ]
  326. ];
  327. $result = $collection->aggregate($pipeline);
  328. $this->assertInternalType('array', $result);
  329. $this->assertArrayHasKey('result', $result);
  330. $this->assertEquals([
  331. ['_id' => 'bar', 'count' => 2],
  332. ['_id' => 'foo', 'count' => 1],
  333. ], $result['result']);
  334. }
  335. public function testAggregateTimeoutException()
  336. {
  337. $this->skipTestUnless(extension_loaded('mongo'));
  338. $collection = $this->getCollection();
  339. $this->failMaxTimeMS();
  340. $this->setExpectedException('MongoExecutionTimeoutException');
  341. $pipeline = [
  342. [
  343. '$group' => [
  344. '_id' => '$foo',
  345. 'count' => [ '$sum' => 1 ],
  346. ],
  347. ],
  348. [
  349. '$sort' => ['_id' => 1]
  350. ]
  351. ];
  352. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  353. }
  354. public function testAggregateCursor()
  355. {
  356. $collection = $this->getCollection();
  357. $this->prepareData();
  358. $pipeline = [
  359. [
  360. '$group' => [
  361. '_id' => '$foo',
  362. 'count' => [ '$sum' => 1 ],
  363. ],
  364. ],
  365. [
  366. '$sort' => ['_id' => 1]
  367. ]
  368. ];
  369. $cursor = $collection->aggregateCursor($pipeline);
  370. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  371. $this->assertEquals([
  372. ['_id' => 'bar', 'count' => 2],
  373. ['_id' => 'foo', 'count' => 1],
  374. ], iterator_to_array($cursor));
  375. }
  376. public function testReadPreference()
  377. {
  378. $collection = $this->getCollection();
  379. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  380. $this->assertFalse($collection->getSlaveOkay());
  381. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  382. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  383. $this->assertTrue($collection->getSlaveOkay());
  384. $this->assertTrue($collection->setSlaveOkay(true));
  385. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  386. $this->assertTrue($collection->setSlaveOkay(false));
  387. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  388. }
  389. public function testReadPreferenceIsSetInDriver()
  390. {
  391. $this->skipTestIf(extension_loaded('mongo'));
  392. $collection = $this->getCollection();
  393. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  394. // Only way to check whether options are passed down is through debugInfo
  395. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  396. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  397. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  398. }
  399. public function testReadPreferenceIsInherited()
  400. {
  401. $database = $this->getDatabase();
  402. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  403. $collection = $database->selectCollection('test');
  404. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  405. }
  406. public function testWriteConcern()
  407. {
  408. $collection = $this->getCollection();
  409. $this->assertTrue($collection->setWriteConcern('majority', 100));
  410. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  411. }
  412. public function testWriteConcernIsSetInDriver()
  413. {
  414. $this->skipTestIf(extension_loaded('mongo'));
  415. $collection = $this->getCollection();
  416. $this->assertTrue($collection->setWriteConcern(2, 100));
  417. // Only way to check whether options are passed down is through debugInfo
  418. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  419. $this->assertSame(2, $writeConcern->getW());
  420. $this->assertSame(100, $writeConcern->getWtimeout());
  421. }
  422. public function testWriteConcernIsInherited()
  423. {
  424. $database = $this->getDatabase();
  425. $database->setWriteConcern('majority', 100);
  426. $collection = $database->selectCollection('test');
  427. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  428. }
  429. public function testSaveInsert()
  430. {
  431. $id = '54203e08d51d4a1f868b456e';
  432. $collection = $this->getCollection();
  433. $objectId = new \MongoId($id);
  434. $expected = [
  435. 'ok' => 1.0,
  436. 'nModified' => 0,
  437. 'n' => 1,
  438. 'err' => null,
  439. 'errmsg' => null,
  440. 'upserted' => $objectId,
  441. 'updatedExisting' => false,
  442. ];
  443. $document = ['_id' => $objectId, 'foo' => 'bar'];
  444. $this->assertEquals($expected, $collection->save($document));
  445. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  446. $this->assertSame(1, $newCollection->count());
  447. $object = $newCollection->findOne();
  448. $this->assertNotNull($object);
  449. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  450. $this->assertSame($id, (string) $object->_id);
  451. $this->assertObjectHasAttribute('foo', $object);
  452. $this->assertAttributeSame('bar', 'foo', $object);
  453. }
  454. public function testRemoveOne()
  455. {
  456. $id = '54203e08d51d4a1f868b456e';
  457. $collection = $this->getCollection();
  458. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  459. $collection->insert($document);
  460. $collection->remove(['_id' => new \MongoId($id)]);
  461. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  462. $this->assertSame(0, $newCollection->count());
  463. }
  464. public function testSaveUpdate()
  465. {
  466. $expected = [
  467. 'ok' => 1.0,
  468. 'nModified' => 1,
  469. 'n' => 1,
  470. 'err' => null,
  471. 'errmsg' => null,
  472. 'updatedExisting' => true,
  473. ];
  474. $id = '54203e08d51d4a1f868b456e';
  475. $collection = $this->getCollection();
  476. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  477. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  478. $collection->insert($insertDocument);
  479. $this->assertSame($expected, $collection->save($saveDocument));
  480. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  481. $this->assertSame(1, $newCollection->count());
  482. $object = $newCollection->findOne();
  483. $this->assertNotNull($object);
  484. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  485. $this->assertSame($id, (string) $object->_id);
  486. $this->assertObjectHasAttribute('foo', $object);
  487. $this->assertAttributeSame('foo', 'foo', $object);
  488. }
  489. public function testSavingShouldReplaceTheWholeDocument() {
  490. $id = '54203e08d51d4a1f868b456e';
  491. $collection = $this->getCollection();
  492. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  493. $saveDocument = ['_id' => new \MongoId($id)];
  494. $collection->insert($insertDocument);
  495. $collection->save($saveDocument);
  496. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  497. $this->assertSame(1, $newCollection->count());
  498. $object = $newCollection->findOne();
  499. $this->assertNotNull($object);
  500. $this->assertObjectNotHasAttribute('foo', $object);
  501. }
  502. public function testSaveDuplicate()
  503. {
  504. $collection = $this->getCollection();
  505. $collection->createIndex(['foo' => 1], ['unique' => true]);
  506. $document = ['foo' => 'bar'];
  507. $collection->save($document);
  508. $this->setExpectedException('MongoDuplicateKeyException');
  509. unset($document['_id']);
  510. $collection->save($document);
  511. }
  512. public function testSaveEmptyKeys()
  513. {
  514. $document = [];
  515. $this->getCollection()->save($document);
  516. $this->assertSame(1, $this->getCollection()->count());
  517. }
  518. public function testSaveEmptyObject()
  519. {
  520. $document = (object) [];
  521. $this->getCollection()->save($document);
  522. $this->assertSame(1, $this->getCollection()->count());
  523. }
  524. public function testGetDBRef()
  525. {
  526. $collection = $this->getCollection();
  527. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  528. $collection->insert($insertDocument);
  529. $document = $collection->getDBRef([
  530. '$ref' => 'test',
  531. '$id' => 1,
  532. ]);
  533. $this->assertEquals($insertDocument, $document);
  534. }
  535. public function testCreateDBRef()
  536. {
  537. $collection = $this->getCollection();
  538. $reference = $collection->createDBRef(['_id' => 'foo']);
  539. $this->assertSame(
  540. [
  541. '$ref' => 'test',
  542. '$id' => 'foo',
  543. ],
  544. $reference
  545. );
  546. }
  547. public function testCreateIndex()
  548. {
  549. $expected = [
  550. 'createdCollectionAutomatically' => true,
  551. 'numIndexesBefore' => 1,
  552. 'numIndexesAfter' => 2,
  553. 'ok' => 1.0,
  554. ];
  555. $collection = $this->getCollection();
  556. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  557. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  558. $iterator = $newCollection->listIndexes();
  559. $indexes = iterator_to_array($iterator);
  560. $this->assertCount(2, $indexes);
  561. $index = $indexes[1];
  562. $this->assertSame(['foo' => 1], $index->getKey());
  563. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  564. }
  565. public function testCreateIndexInvalid()
  566. {
  567. $this->setExpectedException('MongoException', 'index specification has no elements');
  568. $this->getCollection()->createIndex([]);
  569. }
  570. public function testCreateIndexTwice()
  571. {
  572. $this->getCollection()->createIndex(['foo' => 1]);
  573. $expected = [
  574. 'createdCollectionAutomatically' => false,
  575. 'numIndexesBefore' => 2,
  576. 'numIndexesAfter' => 2,
  577. 'note' => 'all indexes already exist',
  578. 'ok' => 1.0
  579. ];
  580. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  581. }
  582. public function testCreateIndexesWithDifferentOptions()
  583. {
  584. $this->setExpectedException('MongoResultException');
  585. $this->getCollection()->createIndex(['foo' => 1]);
  586. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  587. }
  588. public function testCreateIndexWithSameName()
  589. {
  590. $this->setExpectedException('MongoResultException');
  591. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  592. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  593. }
  594. public function testEnsureIndex()
  595. {
  596. $expected = [
  597. 'createdCollectionAutomatically' => true,
  598. 'numIndexesBefore' => 1,
  599. 'numIndexesAfter' => 2,
  600. 'ok' => 1.0
  601. ];
  602. $collection = $this->getCollection();
  603. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  604. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  605. $indexes = iterator_to_array($newCollection->listIndexes());
  606. $this->assertCount(2, $indexes);
  607. $index = $indexes[1];
  608. $this->assertSame(['bar' => 1], $index->getKey());
  609. $this->assertTrue($index->isUnique());
  610. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  611. }
  612. public function testDeleteIndexUsingIndexName()
  613. {
  614. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  615. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  616. $expected = [
  617. 'nIndexesWas' => 2,
  618. 'errmsg' => 'index not found with name [bar_1]',
  619. 'ok' => 0.0,
  620. 'code' => 27,
  621. ];
  622. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  623. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  624. }
  625. public function testDeleteIndexUsingField()
  626. {
  627. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  628. $newCollection->createIndex(['bar' => 1]);
  629. $expected = [
  630. 'nIndexesWas' => 2,
  631. 'ok' => 1.0,
  632. ];
  633. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  634. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  635. }
  636. public function testDeleteIndexUsingKeys()
  637. {
  638. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  639. $newCollection->createIndex(['bar' => 1]);
  640. $expected = [
  641. 'nIndexesWas' => 2,
  642. 'ok' => 1.0,
  643. ];
  644. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  645. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  646. }
  647. public function testDeleteIndexes()
  648. {
  649. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  650. $newCollection->createIndex(['bar' => 1]);
  651. $expected = [
  652. 'nIndexesWas' => 2,
  653. 'msg' => 'non-_id indexes dropped for collection',
  654. 'ok' => 1.0,
  655. ];
  656. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  657. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  658. }
  659. public function testGetIndexInfo()
  660. {
  661. $collection = $this->getCollection();
  662. $collection->createIndex(['foo' => 1]);
  663. $expected = [
  664. [
  665. 'v' => 1,
  666. 'key' => ['_id' => 1],
  667. 'name' => '_id_',
  668. 'ns' => 'mongo-php-adapter.test',
  669. ],
  670. [
  671. 'v' => 1,
  672. 'key' => ['foo' => 1],
  673. 'name' => 'foo_1',
  674. 'ns' => 'mongo-php-adapter.test',
  675. ],
  676. ];
  677. $this->assertSame(
  678. $expected,
  679. $collection->getIndexInfo()
  680. );
  681. }
  682. public function testFindAndModifyUpdate()
  683. {
  684. $id = '54203e08d51d4a1f868b456e';
  685. $collection = $this->getCollection();
  686. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  687. $collection->insert($document);
  688. $document = $collection->findAndModify(
  689. ['_id' => new \MongoId($id)],
  690. ['$set' => ['foo' => 'foo']]
  691. );
  692. $this->assertSame('bar', $document['foo']);
  693. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  694. $this->assertSame(1, $newCollection->count());
  695. $object = $newCollection->findOne();
  696. $this->assertNotNull($object);
  697. $this->assertAttributeSame('foo', 'foo', $object);
  698. }
  699. public function testFindAndModifyUpdateReplace()
  700. {
  701. $id = '54203e08d51d4a1f868b456e';
  702. $collection = $this->getCollection();
  703. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  704. $collection->insert($document);
  705. $document = $collection->findAndModify(
  706. ['_id' => new \MongoId($id)],
  707. ['_id' => new \MongoId($id), 'foo' => 'boo']
  708. );
  709. $this->assertSame('bar', $document['foo']);
  710. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  711. $this->assertSame(1, $newCollection->count());
  712. $object = $newCollection->findOne();
  713. $this->assertNotNull($object);
  714. $this->assertAttributeSame('boo', 'foo', $object);
  715. $this->assertObjectNotHasAttribute('bar', $object);
  716. }
  717. public function testFindAndModifyUpdateReturnNew()
  718. {
  719. $id = '54203e08d51d4a1f868b456e';
  720. $collection = $this->getCollection();
  721. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  722. $collection->insert($document);
  723. $document = $collection->findAndModify(
  724. ['_id' => new \MongoId($id)],
  725. ['$set' => ['foo' => 'foo']],
  726. null,
  727. ['new' => true]
  728. );
  729. $this->assertSame('foo', $document['foo']);
  730. }
  731. public function testFindAndModifyWithFields()
  732. {
  733. $id = '54203e08d51d4a1f868b456e';
  734. $collection = $this->getCollection();
  735. $document = [
  736. '_id' => new \MongoId($id),
  737. 'foo' => 'bar',
  738. 'bar' => 'foo',
  739. ];
  740. $collection->insert($document);
  741. $document = $collection->findAndModify(
  742. ['_id' => new \MongoId($id)],
  743. ['$set' => ['foo' => 'foo']],
  744. ['foo' => true]
  745. );
  746. $this->assertArrayNotHasKey('bar', $document);
  747. $this->assertArrayHasKey('foo', $document);
  748. }
  749. public function testGroup()
  750. {
  751. $collection = $this->getCollection();
  752. $document1 = ['a' => 2];
  753. $collection->insert($document1);
  754. $document2 = ['b' => 5];
  755. $collection->insert($document2);
  756. $document3 = ['a' => 1];
  757. $collection->insert($document3);
  758. $keys = [];
  759. $initial = ["count" => 0];
  760. $reduce = "function (obj, prev) { prev.count++; }";
  761. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  762. $result = $collection->group($keys, $initial, $reduce, $condition);
  763. $this->assertArraySubset(
  764. [
  765. 'retval' => [['count' => 1.0]],
  766. 'count' => 1.0,
  767. 'keys' => 1,
  768. 'ok' => 1.0,
  769. ],
  770. $result
  771. );
  772. }
  773. public function testFindAndModifyResultException()
  774. {
  775. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  776. $collection = $this->getCollection();
  777. $this->setExpectedException('MongoResultException');
  778. $collection->findAndModify(
  779. array("inprogress" => false, "name" => "Next promo"),
  780. array('$unsupportedOperator' => array("tasks" => -1)),
  781. array("tasks" => true),
  782. array("new" => true)
  783. );
  784. }
  785. public function testFindAndModifyExceptionTimeout()
  786. {
  787. $this->failMaxTimeMS();
  788. $id = '54203e08d51d4a1f868b456e';
  789. $collection = $this->getCollection();
  790. $this->setExpectedException('MongoExecutionTimeoutException');
  791. $document = $collection->findAndModify(
  792. ['_id' => new \MongoId($id)],
  793. null,
  794. null,
  795. ['maxTimeMS' => 1, 'remove' => true]
  796. );
  797. }
  798. public function testFindAndModifyRemove()
  799. {
  800. $id = '54203e08d51d4a1f868b456e';
  801. $collection = $this->getCollection();
  802. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  803. $collection->insert($document);
  804. $document = $collection->findAndModify(
  805. ['_id' => new \MongoId($id)],
  806. null,
  807. null,
  808. ['remove' => true]
  809. );
  810. $this->assertEquals('bar', $document['foo']);
  811. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  812. $this->assertSame(0, $newCollection->count());
  813. }
  814. public function testValidate()
  815. {
  816. $collection = $this->getCollection();
  817. $document = ['foo' => 'bar'];
  818. $collection->insert($document);
  819. $result = $collection->validate();
  820. $this->assertArraySubset(
  821. [
  822. 'ns' => 'mongo-php-adapter.test',
  823. 'nrecords' => 1,
  824. 'nIndexes' => 1,
  825. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  826. 'valid' => true,
  827. 'errors' => [],
  828. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  829. 'ok' => 1.0
  830. ],
  831. $result
  832. );
  833. }
  834. public function testDrop()
  835. {
  836. $document = ['foo' => 'bar'];
  837. $this->getCollection()->insert($document);
  838. $expected = [
  839. 'ns' => (string) $this->getCollection(),
  840. 'nIndexesWas' => 1,
  841. 'ok' => 1.0
  842. ];
  843. $this->assertSame($expected, $this->getCollection()->drop());
  844. }
  845. public function testEmptyCollectionName()
  846. {
  847. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  848. new \MongoCollection($this->getDatabase(), '');
  849. }
  850. public function testSelectCollectionWithNullBytes()
  851. {
  852. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  853. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  854. }
  855. public function testSubCollectionWithNullBytes()
  856. {
  857. $collection = $this->getCollection();
  858. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  859. $this->assertSame('test', $collection->getName());
  860. }
  861. }
  862. class PrivatePropertiesStub
  863. {
  864. private $foo = 'bar';
  865. }