MongoCollectionTest.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. // Only way to check whether options are passed down is through debugInfo
  396. $writeConcern = $collection->getCollection()->__debugInfo()['readPreference'];
  397. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  398. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  399. $this->assertTrue($collection->setSlaveOkay(true));
  400. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  401. $this->assertTrue($collection->setSlaveOkay(false));
  402. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  403. }
  404. public function testReadPreferenceIsInherited()
  405. {
  406. $database = $this->getDatabase();
  407. $database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  408. $collection = $database->selectCollection('test');
  409. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  410. }
  411. public function testWriteConcern()
  412. {
  413. $collection = $this->getCollection();
  414. $this->assertSame(['w' => 1, 'wtimeout' => 0], $collection->getWriteConcern());
  415. $this->assertSame(1, $collection->w);
  416. $this->assertSame(0, $collection->wtimeout);
  417. $this->assertTrue($collection->setWriteConcern('majority', 100));
  418. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  419. $collection->w = 2;
  420. $this->assertSame(['w' => 2, 'wtimeout' => 100], $collection->getWriteConcern());
  421. $collection->wtimeout = -1;
  422. $this->assertSame(['w' => 2, 'wtimeout' => 0], $collection->getWriteConcern());
  423. // Only way to check whether options are passed down is through debugInfo
  424. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  425. $this->assertSame(2, $writeConcern->getW());
  426. $this->assertSame(0, $writeConcern->getWtimeout());
  427. }
  428. public function testWriteConcernIsInherited()
  429. {
  430. $database = $this->getDatabase();
  431. $database->setWriteConcern('majority', 100);
  432. $collection = $database->selectCollection('test');
  433. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  434. }
  435. public function testSaveInsert()
  436. {
  437. $id = '54203e08d51d4a1f868b456e';
  438. $collection = $this->getCollection();
  439. $expected = [
  440. 'ok' => 1.0,
  441. 'nModified' => 0,
  442. 'n' => 0,
  443. 'err' => null,
  444. 'errmsg' => null,
  445. 'updatedExisting' => false,
  446. ];
  447. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  448. $this->assertSame($expected, $collection->save($document));
  449. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  450. $this->assertSame(1, $newCollection->count());
  451. $object = $newCollection->findOne();
  452. $this->assertNotNull($object);
  453. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  454. $this->assertSame($id, (string) $object->_id);
  455. $this->assertObjectHasAttribute('foo', $object);
  456. $this->assertAttributeSame('bar', 'foo', $object);
  457. }
  458. public function testRemoveOne()
  459. {
  460. $id = '54203e08d51d4a1f868b456e';
  461. $collection = $this->getCollection();
  462. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  463. $collection->insert($document);
  464. $collection->remove(['_id' => new \MongoId($id)]);
  465. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  466. $this->assertSame(0, $newCollection->count());
  467. }
  468. public function testSaveUpdate()
  469. {
  470. $expected = [
  471. 'ok' => 1.0,
  472. 'nModified' => 1,
  473. 'n' => 1,
  474. 'err' => null,
  475. 'errmsg' => null,
  476. 'updatedExisting' => true,
  477. ];
  478. $id = '54203e08d51d4a1f868b456e';
  479. $collection = $this->getCollection();
  480. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  481. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  482. $collection->insert($insertDocument);
  483. $this->assertSame($expected, $collection->save($saveDocument));
  484. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  485. $this->assertSame(1, $newCollection->count());
  486. $object = $newCollection->findOne();
  487. $this->assertNotNull($object);
  488. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  489. $this->assertSame($id, (string) $object->_id);
  490. $this->assertObjectHasAttribute('foo', $object);
  491. $this->assertAttributeSame('foo', 'foo', $object);
  492. }
  493. public function testSaveDuplicate()
  494. {
  495. $collection = $this->getCollection();
  496. $collection->createIndex(['foo' => 1], ['unique' => true]);
  497. $document = ['foo' => 'bar'];
  498. $collection->save($document);
  499. $this->setExpectedException('MongoCursorException');
  500. unset($document['_id']);
  501. $this->assertArraySubset(
  502. [
  503. 'ok' => 0.0,
  504. 'nModified' => 0,
  505. 'n' => 0,
  506. 'err' => 11000,
  507. 'updatedExisting' => true,
  508. ],
  509. $collection->save($document)
  510. );
  511. }
  512. public function testSaveEmptyKeys()
  513. {
  514. $this->setExpectedException('MongoException');
  515. $document = [];
  516. $this->getCollection()->save($document);
  517. }
  518. public function testSaveEmptyObject()
  519. {
  520. $this->setExpectedException('MongoException');
  521. $document = (object) [];
  522. $this->getCollection()->save($document);
  523. }
  524. public function testSaveWrite()
  525. {
  526. $this->setExpectedException('MongoConnectionException'); // should be MongoCursorException
  527. $document = ['foo' => 'bar'];
  528. $this->getCollection()->save($document, ['w' => 2, 'wtimeout' => 1000]);
  529. }
  530. public function testGetDBRef()
  531. {
  532. $collection = $this->getCollection();
  533. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  534. $collection->insert($insertDocument);
  535. $document = $collection->getDBRef([
  536. '$ref' => 'test',
  537. '$id' => 1,
  538. ]);
  539. $this->assertEquals($insertDocument, $document);
  540. }
  541. public function testCreateDBRef()
  542. {
  543. $collection = $this->getCollection();
  544. $reference = $collection->createDBRef(['_id' => 'foo']);
  545. $this->assertSame(
  546. [
  547. '$ref' => 'test',
  548. '$id' => 'foo',
  549. ],
  550. $reference
  551. );
  552. }
  553. public function testCreateIndex()
  554. {
  555. $expected = [
  556. 'createdCollectionAutomatically' => true,
  557. 'numIndexesBefore' => 1,
  558. 'numIndexesAfter' => 2,
  559. 'ok' => 1.0,
  560. ];
  561. $collection = $this->getCollection();
  562. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  563. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  564. $iterator = $newCollection->listIndexes();
  565. $indexes = iterator_to_array($iterator);
  566. $this->assertCount(2, $indexes);
  567. $index = $indexes[1];
  568. $this->assertSame(['foo' => 1], $index->getKey());
  569. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  570. }
  571. public function testCreateIndexInvalid()
  572. {
  573. $this->setExpectedException('MongoException', 'keys cannot be empty');
  574. $this->getCollection()->createIndex([]);
  575. }
  576. public function testCreateIndexTwice()
  577. {
  578. $this->getCollection()->createIndex(['foo' => 1]);
  579. $expected = [
  580. 'createdCollectionAutomatically' => false,
  581. 'numIndexesBefore' => 1,
  582. 'numIndexesAfter' => 1,
  583. 'note' => 'all indexes already exist',
  584. 'ok' => 1.0
  585. ];
  586. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  587. }
  588. public function testCreateIndexesWithDifferentOptions()
  589. {
  590. $this->setExpectedException('MongoResultException');
  591. $this->getCollection()->createIndex(['foo' => 1]);
  592. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  593. }
  594. public function testCreateIndexWithSameName()
  595. {
  596. $this->setExpectedException('MongoResultException');
  597. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  598. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  599. }
  600. public function testEnsureIndex()
  601. {
  602. $collection = $this->getCollection();
  603. $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
  604. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  605. $indexes = iterator_to_array($newCollection->listIndexes());
  606. $this->assertCount(2, $indexes);
  607. $index = $indexes[1];
  608. $this->assertSame(['bar' => 1], $index->getKey());
  609. $this->assertTrue($index->isUnique());
  610. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  611. }
  612. public function testDeleteIndexUsingIndexName()
  613. {
  614. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  615. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  616. $expected = [
  617. 'nIndexesWas' => 2,
  618. 'ok' => 1.0,
  619. ];
  620. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  621. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  622. }
  623. public function testDeleteIndexUsingKeys()
  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' => 1]));
  632. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  633. }
  634. public function testDeleteIndexes()
  635. {
  636. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  637. $newCollection->createIndex(['bar' => 1]);
  638. $expected = [
  639. 'nIndexesWas' => 2,
  640. 'msg' => 'non-_id indexes dropped for collection',
  641. 'ok' => 1.0,
  642. ];
  643. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  644. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  645. }
  646. public function testGetIndexInfo()
  647. {
  648. $collection = $this->getCollection();
  649. $collection->createIndex(['foo' => 1]);
  650. $expected = [
  651. [
  652. 'v' => 1,
  653. 'key' => ['_id' => 1],
  654. 'name' => '_id_',
  655. 'ns' => 'mongo-php-adapter.test',
  656. ],
  657. [
  658. 'v' => 1,
  659. 'key' => ['foo' => 1],
  660. 'name' => 'foo_1',
  661. 'ns' => 'mongo-php-adapter.test',
  662. ],
  663. ];
  664. $this->assertSame(
  665. $expected,
  666. $collection->getIndexInfo()
  667. );
  668. }
  669. public function testFindAndModifyUpdate()
  670. {
  671. $id = '54203e08d51d4a1f868b456e';
  672. $collection = $this->getCollection();
  673. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  674. $collection->insert($document);
  675. $document = $collection->findAndModify(
  676. ['_id' => new \MongoId($id)],
  677. ['$set' => ['foo' => 'foo']]
  678. );
  679. $this->assertSame('bar', $document['foo']);
  680. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  681. $this->assertSame(1, $newCollection->count());
  682. $object = $newCollection->findOne();
  683. $this->assertNotNull($object);
  684. $this->assertAttributeSame('foo', 'foo', $object);
  685. }
  686. public function testFindAndModifyUpdateReturnNew()
  687. {
  688. $id = '54203e08d51d4a1f868b456e';
  689. $collection = $this->getCollection();
  690. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  691. $collection->insert($document);
  692. $document = $collection->findAndModify(
  693. ['_id' => new \MongoId($id)],
  694. ['$set' => ['foo' => 'foo']],
  695. null,
  696. ['new' => true]
  697. );
  698. $this->assertSame('foo', $document['foo']);
  699. }
  700. public function testFindAndModifyWithFields()
  701. {
  702. $id = '54203e08d51d4a1f868b456e';
  703. $collection = $this->getCollection();
  704. $document = [
  705. '_id' => new \MongoId($id),
  706. 'foo' => 'bar',
  707. 'bar' => 'foo',
  708. ];
  709. $collection->insert($document);
  710. $document = $collection->findAndModify(
  711. ['_id' => new \MongoId($id)],
  712. ['$set' => ['foo' => 'foo']],
  713. ['foo' => true]
  714. );
  715. $this->assertArrayNotHasKey('bar', $document);
  716. $this->assertArrayHasKey('foo', $document);
  717. }
  718. public function testGroup()
  719. {
  720. $collection = $this->getCollection();
  721. $document1 = ['a' => 2];
  722. $collection->insert($document1);
  723. $document2 = ['b' => 5];
  724. $collection->insert($document2);
  725. $document3 = ['a' => 1];
  726. $collection->insert($document3);
  727. $keys = [];
  728. $initial = ["count" => 0];
  729. $reduce = "function (obj, prev) { prev.count++; }";
  730. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  731. $result = $collection->group($keys, $initial, $reduce, $condition);
  732. $this->assertArraySubset(
  733. [
  734. 'retval' => [['count' => 1.0]],
  735. 'count' => 1.0,
  736. 'keys' => 1,
  737. 'ok' => 1.0,
  738. ],
  739. $result
  740. );
  741. }
  742. public function testFindAndModifyResultException()
  743. {
  744. $collection = $this->getCollection();
  745. $this->setExpectedException('MongoResultException');
  746. $collection->findAndModify(
  747. array("inprogress" => false, "name" => "Next promo"),
  748. array('$unsupportedOperator' => array("tasks" => -1)),
  749. array("tasks" => true),
  750. array("new" => true)
  751. );
  752. }
  753. public function testFindAndModifyExceptionTimeout()
  754. {
  755. $this->failMaxTimeMS();
  756. $id = '54203e08d51d4a1f868b456e';
  757. $collection = $this->getCollection();
  758. $this->setExpectedException('MongoExecutionTimeoutException');
  759. $document = $collection->findAndModify(
  760. ['_id' => new \MongoId($id)],
  761. null,
  762. null,
  763. ['maxTimeMS' => 1, 'remove' => true]
  764. );
  765. }
  766. public function testFindAndModifyRemove()
  767. {
  768. $id = '54203e08d51d4a1f868b456e';
  769. $collection = $this->getCollection();
  770. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  771. $collection->insert($document);
  772. $document = $collection->findAndModify(
  773. ['_id' => new \MongoId($id)],
  774. null,
  775. null,
  776. ['remove' => true]
  777. );
  778. $this->assertEquals('bar', $document['foo']);
  779. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  780. $this->assertSame(0, $newCollection->count());
  781. }
  782. public function testValidate()
  783. {
  784. $collection = $this->getCollection();
  785. $document = ['foo' => 'bar'];
  786. $collection->insert($document);
  787. $result = $collection->validate();
  788. $this->assertArraySubset(
  789. [
  790. 'ns' => 'mongo-php-adapter.test',
  791. 'nrecords' => 1,
  792. 'nIndexes' => 1,
  793. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  794. 'valid' => true,
  795. 'errors' => [],
  796. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  797. 'ok' => 1.0
  798. ],
  799. $result
  800. );
  801. }
  802. public function testDrop()
  803. {
  804. $document = ['foo' => 'bar'];
  805. $this->getCollection()->insert($document);
  806. $expected = [
  807. 'ns' => (string) $this->getCollection(),
  808. 'nIndexesWas' => 1,
  809. 'ok' => 1.0
  810. ];
  811. $this->assertSame($expected, $this->getCollection()->drop());
  812. }
  813. public function testEmptyCollectionName()
  814. {
  815. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  816. new \MongoCollection($this->getDatabase(), '');
  817. }
  818. public function testSelectCollectionWithNullBytes()
  819. {
  820. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  821. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  822. }
  823. public function testSubCollectionWithNullBytes()
  824. {
  825. $collection = $this->getCollection();
  826. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  827. $collection->{'foo' . chr(0)};
  828. }
  829. }