MongoCollectionTest.php 32 KB

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