MongoCollectionTest.php 33 KB

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