MongoCollectionTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 testSavingShouldReplaceTheWholeDocument() {
  494. $id = '54203e08d51d4a1f868b456e';
  495. $collection = $this->getCollection();
  496. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  497. $saveDocument = ['_id' => new \MongoId($id)];
  498. $collection->insert($insertDocument);
  499. $collection->save($saveDocument);
  500. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  501. $this->assertSame(1, $newCollection->count());
  502. $object = $newCollection->findOne();
  503. $this->assertNotNull($object);
  504. $this->assertObjectNotHasAttribute('foo', $object);
  505. }
  506. public function testSaveDuplicate()
  507. {
  508. $collection = $this->getCollection();
  509. $collection->createIndex(['foo' => 1], ['unique' => true]);
  510. $document = ['foo' => 'bar'];
  511. $collection->save($document);
  512. $this->setExpectedException('MongoCursorException');
  513. unset($document['_id']);
  514. $this->assertArraySubset(
  515. [
  516. 'ok' => 0.0,
  517. 'nModified' => 0,
  518. 'n' => 0,
  519. 'err' => 11000,
  520. 'updatedExisting' => true,
  521. ],
  522. $collection->save($document)
  523. );
  524. }
  525. public function testSaveEmptyKeys()
  526. {
  527. $this->setExpectedException('MongoException');
  528. $document = [];
  529. $this->getCollection()->save($document);
  530. }
  531. public function testSaveEmptyObject()
  532. {
  533. $this->setExpectedException('MongoException');
  534. $document = (object) [];
  535. $this->getCollection()->save($document);
  536. }
  537. public function testSaveWrite()
  538. {
  539. $this->setExpectedException('MongoConnectionException'); // should be MongoCursorException
  540. $document = ['foo' => 'bar'];
  541. $this->getCollection()->save($document, ['w' => 2, 'wtimeout' => 1000]);
  542. }
  543. public function testGetDBRef()
  544. {
  545. $collection = $this->getCollection();
  546. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  547. $collection->insert($insertDocument);
  548. $document = $collection->getDBRef([
  549. '$ref' => 'test',
  550. '$id' => 1,
  551. ]);
  552. $this->assertEquals($insertDocument, $document);
  553. }
  554. public function testCreateDBRef()
  555. {
  556. $collection = $this->getCollection();
  557. $reference = $collection->createDBRef(['_id' => 'foo']);
  558. $this->assertSame(
  559. [
  560. '$ref' => 'test',
  561. '$id' => 'foo',
  562. ],
  563. $reference
  564. );
  565. }
  566. public function testCreateIndex()
  567. {
  568. $expected = [
  569. 'createdCollectionAutomatically' => true,
  570. 'numIndexesBefore' => 1,
  571. 'numIndexesAfter' => 2,
  572. 'ok' => 1.0,
  573. ];
  574. $collection = $this->getCollection();
  575. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  576. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  577. $iterator = $newCollection->listIndexes();
  578. $indexes = iterator_to_array($iterator);
  579. $this->assertCount(2, $indexes);
  580. $index = $indexes[1];
  581. $this->assertSame(['foo' => 1], $index->getKey());
  582. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  583. }
  584. public function testCreateIndexInvalid()
  585. {
  586. $this->setExpectedException('MongoException', 'keys cannot be empty');
  587. $this->getCollection()->createIndex([]);
  588. }
  589. public function testCreateIndexTwice()
  590. {
  591. $this->getCollection()->createIndex(['foo' => 1]);
  592. $expected = [
  593. 'createdCollectionAutomatically' => false,
  594. 'numIndexesBefore' => 1,
  595. 'numIndexesAfter' => 1,
  596. 'note' => 'all indexes already exist',
  597. 'ok' => 1.0
  598. ];
  599. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  600. }
  601. public function testCreateIndexesWithDifferentOptions()
  602. {
  603. $this->setExpectedException('MongoResultException');
  604. $this->getCollection()->createIndex(['foo' => 1]);
  605. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  606. }
  607. public function testCreateIndexWithSameName()
  608. {
  609. $this->setExpectedException('MongoResultException');
  610. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  611. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  612. }
  613. public function testEnsureIndex()
  614. {
  615. $collection = $this->getCollection();
  616. $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
  617. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  618. $indexes = iterator_to_array($newCollection->listIndexes());
  619. $this->assertCount(2, $indexes);
  620. $index = $indexes[1];
  621. $this->assertSame(['bar' => 1], $index->getKey());
  622. $this->assertTrue($index->isUnique());
  623. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  624. }
  625. public function testDeleteIndexUsingIndexName()
  626. {
  627. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  628. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  629. $expected = [
  630. 'nIndexesWas' => 2,
  631. 'ok' => 1.0,
  632. ];
  633. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  634. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  635. }
  636. public function testDeleteIndexUsingKeys()
  637. {
  638. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  639. $newCollection->createIndex(['bar' => 1]);
  640. $expected = [
  641. 'nIndexesWas' => 2,
  642. 'ok' => 1.0,
  643. ];
  644. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  645. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  646. }
  647. public function testDeleteIndexes()
  648. {
  649. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  650. $newCollection->createIndex(['bar' => 1]);
  651. $expected = [
  652. 'nIndexesWas' => 2,
  653. 'msg' => 'non-_id indexes dropped for collection',
  654. 'ok' => 1.0,
  655. ];
  656. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  657. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  658. }
  659. public function testGetIndexInfo()
  660. {
  661. $collection = $this->getCollection();
  662. $collection->createIndex(['foo' => 1]);
  663. $expected = [
  664. [
  665. 'v' => 1,
  666. 'key' => ['_id' => 1],
  667. 'name' => '_id_',
  668. 'ns' => 'mongo-php-adapter.test',
  669. ],
  670. [
  671. 'v' => 1,
  672. 'key' => ['foo' => 1],
  673. 'name' => 'foo_1',
  674. 'ns' => 'mongo-php-adapter.test',
  675. ],
  676. ];
  677. $this->assertSame(
  678. $expected,
  679. $collection->getIndexInfo()
  680. );
  681. }
  682. public function testFindAndModifyUpdate()
  683. {
  684. $id = '54203e08d51d4a1f868b456e';
  685. $collection = $this->getCollection();
  686. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  687. $collection->insert($document);
  688. $document = $collection->findAndModify(
  689. ['_id' => new \MongoId($id)],
  690. ['$set' => ['foo' => 'foo']]
  691. );
  692. $this->assertSame('bar', $document['foo']);
  693. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  694. $this->assertSame(1, $newCollection->count());
  695. $object = $newCollection->findOne();
  696. $this->assertNotNull($object);
  697. $this->assertAttributeSame('foo', 'foo', $object);
  698. }
  699. public function testFindAndModifyUpdateReturnNew()
  700. {
  701. $id = '54203e08d51d4a1f868b456e';
  702. $collection = $this->getCollection();
  703. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  704. $collection->insert($document);
  705. $document = $collection->findAndModify(
  706. ['_id' => new \MongoId($id)],
  707. ['$set' => ['foo' => 'foo']],
  708. null,
  709. ['new' => true]
  710. );
  711. $this->assertSame('foo', $document['foo']);
  712. }
  713. public function testFindAndModifyWithFields()
  714. {
  715. $id = '54203e08d51d4a1f868b456e';
  716. $collection = $this->getCollection();
  717. $document = [
  718. '_id' => new \MongoId($id),
  719. 'foo' => 'bar',
  720. 'bar' => 'foo',
  721. ];
  722. $collection->insert($document);
  723. $document = $collection->findAndModify(
  724. ['_id' => new \MongoId($id)],
  725. ['$set' => ['foo' => 'foo']],
  726. ['foo' => true]
  727. );
  728. $this->assertArrayNotHasKey('bar', $document);
  729. $this->assertArrayHasKey('foo', $document);
  730. }
  731. public function testGroup()
  732. {
  733. $collection = $this->getCollection();
  734. $document1 = ['a' => 2];
  735. $collection->insert($document1);
  736. $document2 = ['b' => 5];
  737. $collection->insert($document2);
  738. $document3 = ['a' => 1];
  739. $collection->insert($document3);
  740. $keys = [];
  741. $initial = ["count" => 0];
  742. $reduce = "function (obj, prev) { prev.count++; }";
  743. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  744. $result = $collection->group($keys, $initial, $reduce, $condition);
  745. $this->assertArraySubset(
  746. [
  747. 'retval' => [['count' => 1.0]],
  748. 'count' => 1.0,
  749. 'keys' => 1,
  750. 'ok' => 1.0,
  751. ],
  752. $result
  753. );
  754. }
  755. public function testFindAndModifyResultException()
  756. {
  757. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  758. $collection = $this->getCollection();
  759. $this->setExpectedException('MongoResultException');
  760. $collection->findAndModify(
  761. array("inprogress" => false, "name" => "Next promo"),
  762. array('$unsupportedOperator' => array("tasks" => -1)),
  763. array("tasks" => true),
  764. array("new" => true)
  765. );
  766. }
  767. public function testFindAndModifyExceptionTimeout()
  768. {
  769. $this->failMaxTimeMS();
  770. $id = '54203e08d51d4a1f868b456e';
  771. $collection = $this->getCollection();
  772. $this->setExpectedException('MongoExecutionTimeoutException');
  773. $document = $collection->findAndModify(
  774. ['_id' => new \MongoId($id)],
  775. null,
  776. null,
  777. ['maxTimeMS' => 1, 'remove' => true]
  778. );
  779. }
  780. public function testFindAndModifyRemove()
  781. {
  782. $id = '54203e08d51d4a1f868b456e';
  783. $collection = $this->getCollection();
  784. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  785. $collection->insert($document);
  786. $document = $collection->findAndModify(
  787. ['_id' => new \MongoId($id)],
  788. null,
  789. null,
  790. ['remove' => true]
  791. );
  792. $this->assertEquals('bar', $document['foo']);
  793. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  794. $this->assertSame(0, $newCollection->count());
  795. }
  796. public function testValidate()
  797. {
  798. $collection = $this->getCollection();
  799. $document = ['foo' => 'bar'];
  800. $collection->insert($document);
  801. $result = $collection->validate();
  802. $this->assertArraySubset(
  803. [
  804. 'ns' => 'mongo-php-adapter.test',
  805. 'nrecords' => 1,
  806. 'nIndexes' => 1,
  807. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  808. 'valid' => true,
  809. 'errors' => [],
  810. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  811. 'ok' => 1.0
  812. ],
  813. $result
  814. );
  815. }
  816. public function testDrop()
  817. {
  818. $document = ['foo' => 'bar'];
  819. $this->getCollection()->insert($document);
  820. $expected = [
  821. 'ns' => (string) $this->getCollection(),
  822. 'nIndexesWas' => 1,
  823. 'ok' => 1.0
  824. ];
  825. $this->assertSame($expected, $this->getCollection()->drop());
  826. }
  827. public function testEmptyCollectionName()
  828. {
  829. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  830. new \MongoCollection($this->getDatabase(), '');
  831. }
  832. public function testSelectCollectionWithNullBytes()
  833. {
  834. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  835. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  836. }
  837. public function testSubCollectionWithNullBytes()
  838. {
  839. $collection = $this->getCollection();
  840. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  841. $collection->{'foo' . chr(0)};
  842. }
  843. }