MongoCollectionTest.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 testFindAndModifyResultException()
  799. {
  800. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  801. $collection = $this->getCollection();
  802. $this->setExpectedException('MongoResultException');
  803. $collection->findAndModify(
  804. array("inprogress" => false, "name" => "Next promo"),
  805. array('$unsupportedOperator' => array("tasks" => -1)),
  806. array("tasks" => true),
  807. array("new" => true)
  808. );
  809. }
  810. public function testFindAndModifyExceptionTimeout()
  811. {
  812. $this->failMaxTimeMS();
  813. $id = '54203e08d51d4a1f868b456e';
  814. $collection = $this->getCollection();
  815. $this->setExpectedException('MongoExecutionTimeoutException');
  816. $document = $collection->findAndModify(
  817. ['_id' => new \MongoId($id)],
  818. null,
  819. null,
  820. ['maxTimeMS' => 1, 'remove' => true]
  821. );
  822. }
  823. public function testFindAndModifyRemove()
  824. {
  825. $id = '54203e08d51d4a1f868b456e';
  826. $collection = $this->getCollection();
  827. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  828. $collection->insert($document);
  829. $document = $collection->findAndModify(
  830. ['_id' => new \MongoId($id)],
  831. null,
  832. null,
  833. ['remove' => true]
  834. );
  835. $this->assertEquals('bar', $document['foo']);
  836. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  837. $this->assertSame(0, $newCollection->count());
  838. }
  839. public function testValidate()
  840. {
  841. $collection = $this->getCollection();
  842. $document = ['foo' => 'bar'];
  843. $collection->insert($document);
  844. $result = $collection->validate();
  845. $this->assertArraySubset(
  846. [
  847. 'ns' => 'mongo-php-adapter.test',
  848. 'nrecords' => 1,
  849. 'nIndexes' => 1,
  850. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  851. 'valid' => true,
  852. 'errors' => [],
  853. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  854. 'ok' => 1.0
  855. ],
  856. $result
  857. );
  858. }
  859. public function testDrop()
  860. {
  861. $document = ['foo' => 'bar'];
  862. $this->getCollection()->insert($document);
  863. $expected = [
  864. 'ns' => (string) $this->getCollection(),
  865. 'nIndexesWas' => 1,
  866. 'ok' => 1.0
  867. ];
  868. $this->assertSame($expected, $this->getCollection()->drop());
  869. }
  870. public function testEmptyCollectionName()
  871. {
  872. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  873. new \MongoCollection($this->getDatabase(), '');
  874. }
  875. public function testSelectCollectionWithNullBytes()
  876. {
  877. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  878. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  879. }
  880. public function testSubCollectionWithNullBytes()
  881. {
  882. $collection = $this->getCollection();
  883. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  884. $this->assertSame('test', $collection->getName());
  885. }
  886. }
  887. class PrivatePropertiesStub
  888. {
  889. private $foo = 'bar';
  890. }