MongoCollectionTest.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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 testInsertWithInvalidKey()
  67. {
  68. $document = ['*' => 'foo'];
  69. $this->getCollection()->insert($document);
  70. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  71. }
  72. public function testInsertWithEmptyKey()
  73. {
  74. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  75. $document = ['' => 'foo'];
  76. $this->getCollection()->insert($document);
  77. }
  78. public function testInsertWithNumericKey()
  79. {
  80. $document = ['foo'];
  81. $this->getCollection()->insert($document);
  82. $this->assertSame(1, $this->getCollection()->count(['foo']));
  83. }
  84. public function testInsertDuplicate()
  85. {
  86. $collection = $this->getCollection();
  87. $collection->createIndex(['foo' => 1], ['unique' => true]);
  88. $document = ['foo' => 'bar'];
  89. $collection->insert($document);
  90. unset($document['_id']);
  91. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
  92. $collection->insert($document);
  93. }
  94. public function testUnacknowledgedWrite()
  95. {
  96. $document = ['foo' => 'bar'];
  97. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  98. }
  99. public function testInsertWriteConcernException()
  100. {
  101. $this->setExpectedException(
  102. 'MongoWriteConcernException',
  103. "cannot use 'w' > 1 when a host is not replicated"
  104. );
  105. $document = ['foo' => 'bar'];
  106. $this->getCollection()->insert($document, ['w' => 2]);
  107. }
  108. public function testInsertMany()
  109. {
  110. $expected = [
  111. 'ok' => 1.0,
  112. 'n' => 0,
  113. 'syncMillis' => 0,
  114. 'writtenTo' => null,
  115. 'err' => null,
  116. ];
  117. $documents = [
  118. ['foo' => 'bar'],
  119. ['bar' => 'foo']
  120. ];
  121. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  122. foreach ($documents as $document) {
  123. $this->assertInstanceOf('MongoId', $document['_id']);
  124. }
  125. }
  126. public function testInsertManyWithNonNumericKeys()
  127. {
  128. $expected = [
  129. 'ok' => 1.0,
  130. 'n' => 0,
  131. 'syncMillis' => 0,
  132. 'writtenTo' => null,
  133. 'err' => null,
  134. ];
  135. $documents = [
  136. 'a' => ['foo' => 'bar'],
  137. 'b' => ['bar' => 'foo']
  138. ];
  139. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  140. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  141. $this->assertSame(2, $newCollection->count());
  142. }
  143. public function testBatchInsertContinuesOnError()
  144. {
  145. $expected = [
  146. 'ok' => 1.0,
  147. 'n' => 0,
  148. 'syncMillis' => 0,
  149. 'writtenTo' => null,
  150. 'err' => null,
  151. ];
  152. $documents = [
  153. 8,
  154. 'b' => ['bar' => 'foo']
  155. ];
  156. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  157. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  158. $this->assertSame(1, $newCollection->count());
  159. }
  160. public function testBatchInsertException()
  161. {
  162. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
  163. $id = new \MongoId();
  164. $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
  165. $this->getCollection()->batchInsert($documents);
  166. }
  167. public function testBatchInsertObjectWithPrivateProperties()
  168. {
  169. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  170. $documents = [new PrivatePropertiesStub()];
  171. $this->getCollection()->batchInsert($documents);
  172. }
  173. public function testBatchInsertWithInvalidKey()
  174. {
  175. $documents = [['*' => 'foo']];
  176. $this->getCollection()->batchInsert($documents);
  177. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  178. }
  179. public function testBatchInsertWithEmptyKey()
  180. {
  181. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  182. $documents = [['' => 'foo']];
  183. $this->getCollection()->batchInsert($documents);
  184. }
  185. public function testBatchInsertWithNumericKey()
  186. {
  187. $documents = [['foo']];
  188. $this->getCollection()->batchInsert($documents);
  189. $this->assertSame(1, $this->getCollection()->count(['foo']));
  190. }
  191. public function testBatchInsertEmptyBatchException()
  192. {
  193. $this->setExpectedException('MongoException', 'No write ops were included in the batch');
  194. $documents = [];
  195. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  196. }
  197. public function testUpdateWriteConcern()
  198. {
  199. $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
  200. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  201. }
  202. public function testUpdateOne()
  203. {
  204. $document = ['foo' => 'bar'];
  205. $this->getCollection()->insert($document);
  206. // Unset ID to re-insert
  207. unset($document['_id']);
  208. $this->getCollection()->insert($document);
  209. $expected = [
  210. 'ok' => 1.0,
  211. 'nModified' => 1,
  212. 'n' => 1,
  213. 'err' => null,
  214. 'errmsg' => null,
  215. 'updatedExisting' => true,
  216. ];
  217. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  218. $this->assertSame($expected, $result);
  219. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  220. }
  221. public function testUpdateReplaceOne()
  222. {
  223. $document = ['foo' => 'bar', 'bar' => 'foo'];
  224. $this->getCollection()->insert($document);
  225. // Unset ID to re-insert
  226. unset($document['_id']);
  227. $this->getCollection()->insert($document);
  228. $expected = [
  229. 'ok' => 1.0,
  230. 'nModified' => 1,
  231. 'n' => 1,
  232. 'err' => null,
  233. 'errmsg' => null,
  234. 'updatedExisting' => true,
  235. ];
  236. $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
  237. $this->assertSame($expected, $result);
  238. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  239. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
  240. }
  241. public function testUpdateReplaceMultiple()
  242. {
  243. $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
  244. $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
  245. }
  246. public function testUpdateDuplicate()
  247. {
  248. $collection = $this->getCollection();
  249. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  250. $document = ['foo' => 'bar'];
  251. $collection->insert($document);
  252. $document = ['foo' => 'foo'];
  253. $collection->insert($document);
  254. $this->setExpectedException('MongoDuplicateKeyException');
  255. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  256. }
  257. public function testUpdateMany()
  258. {
  259. $document = ['change' => true, 'foo' => 'bar'];
  260. $this->getCollection()->insert($document);
  261. unset($document['_id']);
  262. $this->getCollection()->insert($document);
  263. $document = ['change' => true, 'foo' => 'foo'];
  264. $this->getCollection()->insert($document);
  265. $expected = [
  266. 'ok' => 1.0,
  267. 'nModified' => 2,
  268. 'n' => 3,
  269. 'err' => null,
  270. 'errmsg' => null,
  271. 'updatedExisting' => true,
  272. ];
  273. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  274. $this->assertSame($expected, $result);
  275. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  276. }
  277. public function testUnacknowledgedUpdate()
  278. {
  279. $document = ['foo' => 'bar'];
  280. $this->getCollection()->insert($document);
  281. unset($document['_id']);
  282. $this->getCollection()->insert($document);
  283. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  284. }
  285. public function testRemoveMultiple()
  286. {
  287. $document = ['change' => true, 'foo' => 'bar'];
  288. $this->getCollection()->insert($document);
  289. unset($document['_id']);
  290. $this->getCollection()->insert($document);
  291. $document = ['change' => true, 'foo' => 'foo'];
  292. $this->getCollection()->insert($document);
  293. $expected = [
  294. 'ok' => 1.0,
  295. 'n' => 2,
  296. 'err' => null,
  297. 'errmsg' => null,
  298. ];
  299. $result = $this->getCollection()->remove(['foo' => 'bar']);
  300. $this->assertSame($expected, $result);
  301. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  302. }
  303. public function testRemoveSingle()
  304. {
  305. $document = ['change' => true, 'foo' => 'bar'];
  306. $this->getCollection()->insert($document);
  307. unset($document['_id']);
  308. $this->getCollection()->insert($document);
  309. unset($document['_id']);
  310. $this->getCollection()->insert($document);
  311. $expected = [
  312. 'ok' => 1.0,
  313. 'n' => 1,
  314. 'err' => null,
  315. 'errmsg' => null,
  316. ];
  317. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  318. $this->assertSame($expected, $result);
  319. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  320. }
  321. public function testRemoveUnacknowledged()
  322. {
  323. $document = ['change' => true, 'foo' => 'bar'];
  324. $this->getCollection()->insert($document);
  325. unset($document['_id']);
  326. $this->getCollection()->insert($document);
  327. unset($document['_id']);
  328. $this->getCollection()->insert($document);
  329. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  330. }
  331. public function testFindReturnsCursor()
  332. {
  333. $this->prepareData();
  334. $collection = $this->getCollection();
  335. $this->assertInstanceOf('MongoCursor', $collection->find());
  336. }
  337. public function testCount()
  338. {
  339. $this->prepareData();
  340. $collection = $this->getCollection();
  341. $this->assertSame(3, $collection->count());
  342. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  343. }
  344. public function testCountTimeout()
  345. {
  346. $this->failMaxTimeMS();
  347. $this->setExpectedException('MongoExecutionTimeoutException');
  348. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  349. }
  350. public function testFindOne()
  351. {
  352. $this->prepareData();
  353. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  354. $this->assertSame(['foo' => 'foo'], $document);
  355. }
  356. public function testFindOneNotFound()
  357. {
  358. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  359. $this->assertNull($document);
  360. }
  361. public function testFindOneConnectionIssue()
  362. {
  363. $this->setExpectedException('MongoConnectionException');
  364. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  365. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  366. $collection->findOne();
  367. }
  368. public function testDistinct()
  369. {
  370. $this->prepareData();
  371. $values = $this->getCollection()->distinct('foo');
  372. $this->assertInternalType('array', $values);
  373. sort($values);
  374. $this->assertEquals(['bar', 'foo'], $values);
  375. }
  376. public function testDistinctWithQuery()
  377. {
  378. $this->prepareData();
  379. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  380. $this->assertInternalType('array', $values);
  381. $this->assertEquals(['bar'], $values);
  382. }
  383. public function testAggregate()
  384. {
  385. $this->skipTestUnless(extension_loaded('mongo'));
  386. $collection = $this->getCollection();
  387. $this->prepareData();
  388. $pipeline = [
  389. [
  390. '$group' => [
  391. '_id' => '$foo',
  392. 'count' => [ '$sum' => 1 ],
  393. ],
  394. ],
  395. [
  396. '$sort' => ['_id' => 1]
  397. ]
  398. ];
  399. $result = $collection->aggregate($pipeline);
  400. $this->assertInternalType('array', $result);
  401. $this->assertArrayHasKey('result', $result);
  402. $this->assertEquals([
  403. ['_id' => 'bar', 'count' => 2],
  404. ['_id' => 'foo', 'count' => 1],
  405. ], $result['result']);
  406. }
  407. public function testAggregateTimeoutException()
  408. {
  409. $this->skipTestUnless(extension_loaded('mongo'));
  410. $collection = $this->getCollection();
  411. $this->failMaxTimeMS();
  412. $this->setExpectedException('MongoExecutionTimeoutException');
  413. $pipeline = [
  414. [
  415. '$group' => [
  416. '_id' => '$foo',
  417. 'count' => [ '$sum' => 1 ],
  418. ],
  419. ],
  420. [
  421. '$sort' => ['_id' => 1]
  422. ]
  423. ];
  424. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  425. }
  426. public function testAggregateCursor()
  427. {
  428. $collection = $this->getCollection();
  429. $this->prepareData();
  430. $pipeline = [
  431. [
  432. '$group' => [
  433. '_id' => '$foo',
  434. 'count' => [ '$sum' => 1 ],
  435. ],
  436. ],
  437. [
  438. '$sort' => ['_id' => 1]
  439. ]
  440. ];
  441. $cursor = $collection->aggregateCursor($pipeline);
  442. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  443. $this->assertEquals([
  444. ['_id' => 'bar', 'count' => 2],
  445. ['_id' => 'foo', 'count' => 1],
  446. ], iterator_to_array($cursor));
  447. }
  448. public function testReadPreference()
  449. {
  450. $collection = $this->getCollection();
  451. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  452. $this->assertFalse($collection->getSlaveOkay());
  453. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  454. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  455. $this->assertTrue($collection->getSlaveOkay());
  456. $this->assertTrue($collection->setSlaveOkay(true));
  457. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  458. $this->assertTrue($collection->setSlaveOkay(false));
  459. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  460. }
  461. public function testReadPreferenceIsSetInDriver()
  462. {
  463. $this->skipTestIf(extension_loaded('mongo'));
  464. $collection = $this->getCollection();
  465. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  466. // Only way to check whether options are passed down is through debugInfo
  467. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  468. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  469. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  470. }
  471. public function testReadPreferenceIsInherited()
  472. {
  473. $database = $this->getDatabase();
  474. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  475. $collection = $database->selectCollection('test');
  476. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  477. }
  478. public function testWriteConcern()
  479. {
  480. $collection = $this->getCollection();
  481. $this->assertTrue($collection->setWriteConcern('majority', 100));
  482. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  483. }
  484. public function testWriteConcernIsSetInDriver()
  485. {
  486. $this->skipTestIf(extension_loaded('mongo'));
  487. $collection = $this->getCollection();
  488. $this->assertTrue($collection->setWriteConcern(2, 100));
  489. // Only way to check whether options are passed down is through debugInfo
  490. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  491. $this->assertSame(2, $writeConcern->getW());
  492. $this->assertSame(100, $writeConcern->getWtimeout());
  493. }
  494. public function testWriteConcernIsInherited()
  495. {
  496. $database = $this->getDatabase();
  497. $database->setWriteConcern('majority', 100);
  498. $collection = $database->selectCollection('test');
  499. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  500. }
  501. public function testSaveInsert()
  502. {
  503. $id = '54203e08d51d4a1f868b456e';
  504. $collection = $this->getCollection();
  505. $objectId = new \MongoId($id);
  506. $expected = [
  507. 'ok' => 1.0,
  508. 'nModified' => 0,
  509. 'n' => 1,
  510. 'err' => null,
  511. 'errmsg' => null,
  512. 'upserted' => $objectId,
  513. 'updatedExisting' => false,
  514. ];
  515. $document = ['_id' => $objectId, 'foo' => 'bar'];
  516. $this->assertEquals($expected, $collection->save($document));
  517. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  518. $this->assertSame(1, $newCollection->count());
  519. $object = $newCollection->findOne();
  520. $this->assertNotNull($object);
  521. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  522. $this->assertSame($id, (string) $object->_id);
  523. $this->assertObjectHasAttribute('foo', $object);
  524. $this->assertAttributeSame('bar', 'foo', $object);
  525. }
  526. public function testRemoveOne()
  527. {
  528. $id = '54203e08d51d4a1f868b456e';
  529. $collection = $this->getCollection();
  530. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  531. $collection->insert($document);
  532. $collection->remove(['_id' => new \MongoId($id)]);
  533. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  534. $this->assertSame(0, $newCollection->count());
  535. }
  536. public function testSaveUpdate()
  537. {
  538. $expected = [
  539. 'ok' => 1.0,
  540. 'nModified' => 1,
  541. 'n' => 1,
  542. 'err' => null,
  543. 'errmsg' => null,
  544. 'updatedExisting' => true,
  545. ];
  546. $id = '54203e08d51d4a1f868b456e';
  547. $collection = $this->getCollection();
  548. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  549. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  550. $collection->insert($insertDocument);
  551. $this->assertSame($expected, $collection->save($saveDocument));
  552. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  553. $this->assertSame(1, $newCollection->count());
  554. $object = $newCollection->findOne();
  555. $this->assertNotNull($object);
  556. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  557. $this->assertSame($id, (string) $object->_id);
  558. $this->assertObjectHasAttribute('foo', $object);
  559. $this->assertAttributeSame('foo', 'foo', $object);
  560. }
  561. public function testSavingShouldReplaceTheWholeDocument() {
  562. $id = '54203e08d51d4a1f868b456e';
  563. $collection = $this->getCollection();
  564. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  565. $saveDocument = ['_id' => new \MongoId($id)];
  566. $collection->insert($insertDocument);
  567. $collection->save($saveDocument);
  568. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  569. $this->assertSame(1, $newCollection->count());
  570. $object = $newCollection->findOne();
  571. $this->assertNotNull($object);
  572. $this->assertObjectNotHasAttribute('foo', $object);
  573. }
  574. public function testSaveDuplicate()
  575. {
  576. $collection = $this->getCollection();
  577. $collection->createIndex(['foo' => 1], ['unique' => true]);
  578. $document = ['foo' => 'bar'];
  579. $collection->save($document);
  580. $this->setExpectedException('MongoDuplicateKeyException');
  581. unset($document['_id']);
  582. $collection->save($document);
  583. }
  584. public function testSaveEmptyKeys()
  585. {
  586. $document = [];
  587. $this->getCollection()->save($document);
  588. $this->assertSame(1, $this->getCollection()->count());
  589. }
  590. public function testSaveEmptyObject()
  591. {
  592. $document = (object) [];
  593. $this->getCollection()->save($document);
  594. $this->assertSame(1, $this->getCollection()->count());
  595. }
  596. public function testGetDBRef()
  597. {
  598. $collection = $this->getCollection();
  599. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  600. $collection->insert($insertDocument);
  601. $document = $collection->getDBRef([
  602. '$ref' => 'test',
  603. '$id' => 1,
  604. ]);
  605. $this->assertEquals($insertDocument, $document);
  606. }
  607. public function testCreateDBRef()
  608. {
  609. $collection = $this->getCollection();
  610. $reference = $collection->createDBRef(['_id' => 'foo']);
  611. $this->assertSame(
  612. [
  613. '$ref' => 'test',
  614. '$id' => 'foo',
  615. ],
  616. $reference
  617. );
  618. }
  619. public function testCreateIndex()
  620. {
  621. $expected = [
  622. 'createdCollectionAutomatically' => true,
  623. 'numIndexesBefore' => 1,
  624. 'numIndexesAfter' => 2,
  625. 'ok' => 1.0,
  626. ];
  627. $collection = $this->getCollection();
  628. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  629. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  630. $iterator = $newCollection->listIndexes();
  631. $indexes = iterator_to_array($iterator);
  632. $this->assertCount(2, $indexes);
  633. $index = $indexes[1];
  634. $this->assertSame(['foo' => 1], $index->getKey());
  635. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  636. }
  637. public function testCreateIndexInvalid()
  638. {
  639. $this->setExpectedException('MongoException', 'index specification has no elements');
  640. $this->getCollection()->createIndex([]);
  641. }
  642. public function testCreateIndexTwice()
  643. {
  644. $this->getCollection()->createIndex(['foo' => 1]);
  645. $expected = [
  646. 'createdCollectionAutomatically' => false,
  647. 'numIndexesBefore' => 2,
  648. 'numIndexesAfter' => 2,
  649. 'note' => 'all indexes already exist',
  650. 'ok' => 1.0
  651. ];
  652. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  653. }
  654. public function testCreateIndexesWithDifferentOptions()
  655. {
  656. $this->setExpectedException('MongoResultException');
  657. $this->getCollection()->createIndex(['foo' => 1]);
  658. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  659. }
  660. public function testCreateIndexWithSameName()
  661. {
  662. $this->setExpectedException('MongoResultException');
  663. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  664. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  665. }
  666. public function testEnsureIndex()
  667. {
  668. $expected = [
  669. 'createdCollectionAutomatically' => true,
  670. 'numIndexesBefore' => 1,
  671. 'numIndexesAfter' => 2,
  672. 'ok' => 1.0
  673. ];
  674. $collection = $this->getCollection();
  675. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  676. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  677. $indexes = iterator_to_array($newCollection->listIndexes());
  678. $this->assertCount(2, $indexes);
  679. $index = $indexes[1];
  680. $this->assertSame(['bar' => 1], $index->getKey());
  681. $this->assertTrue($index->isUnique());
  682. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  683. }
  684. public function testDeleteIndexUsingIndexName()
  685. {
  686. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  687. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  688. $expected = [
  689. 'nIndexesWas' => 2,
  690. 'errmsg' => 'index not found with name [bar_1]',
  691. 'ok' => 0.0,
  692. 'code' => 27,
  693. ];
  694. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  695. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  696. }
  697. public function testDeleteIndexUsingField()
  698. {
  699. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  700. $newCollection->createIndex(['bar' => 1]);
  701. $expected = [
  702. 'nIndexesWas' => 2,
  703. 'ok' => 1.0,
  704. ];
  705. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  706. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  707. }
  708. public function testDeleteIndexUsingKeys()
  709. {
  710. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  711. $newCollection->createIndex(['bar' => 1]);
  712. $expected = [
  713. 'nIndexesWas' => 2,
  714. 'ok' => 1.0,
  715. ];
  716. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  717. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  718. }
  719. public function testDeleteIndexes()
  720. {
  721. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  722. $newCollection->createIndex(['bar' => 1]);
  723. $expected = [
  724. 'nIndexesWas' => 2,
  725. 'msg' => 'non-_id indexes dropped for collection',
  726. 'ok' => 1.0,
  727. ];
  728. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  729. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  730. }
  731. public function testGetIndexInfo()
  732. {
  733. $collection = $this->getCollection();
  734. $collection->createIndex(['foo' => 1]);
  735. $expected = [
  736. [
  737. 'v' => 1,
  738. 'key' => ['_id' => 1],
  739. 'name' => '_id_',
  740. 'ns' => 'mongo-php-adapter.test',
  741. ],
  742. [
  743. 'v' => 1,
  744. 'key' => ['foo' => 1],
  745. 'name' => 'foo_1',
  746. 'ns' => 'mongo-php-adapter.test',
  747. ],
  748. ];
  749. $this->assertSame(
  750. $expected,
  751. $collection->getIndexInfo()
  752. );
  753. }
  754. public function testFindAndModifyUpdate()
  755. {
  756. $id = '54203e08d51d4a1f868b456e';
  757. $collection = $this->getCollection();
  758. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  759. $collection->insert($document);
  760. $document = $collection->findAndModify(
  761. ['_id' => new \MongoId($id)],
  762. ['$set' => ['foo' => 'foo']]
  763. );
  764. $this->assertSame('bar', $document['foo']);
  765. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  766. $this->assertSame(1, $newCollection->count());
  767. $object = $newCollection->findOne();
  768. $this->assertNotNull($object);
  769. $this->assertAttributeSame('foo', 'foo', $object);
  770. }
  771. public function testFindAndModifyUpdateReplace()
  772. {
  773. $id = '54203e08d51d4a1f868b456e';
  774. $collection = $this->getCollection();
  775. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  776. $collection->insert($document);
  777. $document = $collection->findAndModify(
  778. ['_id' => new \MongoId($id)],
  779. ['_id' => new \MongoId($id), 'foo' => 'boo']
  780. );
  781. $this->assertSame('bar', $document['foo']);
  782. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  783. $this->assertSame(1, $newCollection->count());
  784. $object = $newCollection->findOne();
  785. $this->assertNotNull($object);
  786. $this->assertAttributeSame('boo', 'foo', $object);
  787. $this->assertObjectNotHasAttribute('bar', $object);
  788. }
  789. public function testFindAndModifyUpdateReturnNew()
  790. {
  791. $id = '54203e08d51d4a1f868b456e';
  792. $collection = $this->getCollection();
  793. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  794. $collection->insert($document);
  795. $document = $collection->findAndModify(
  796. ['_id' => new \MongoId($id)],
  797. ['$set' => ['foo' => 'foo']],
  798. null,
  799. ['new' => true]
  800. );
  801. $this->assertSame('foo', $document['foo']);
  802. }
  803. public function testFindAndModifyWithFields()
  804. {
  805. $id = '54203e08d51d4a1f868b456e';
  806. $collection = $this->getCollection();
  807. $document = [
  808. '_id' => new \MongoId($id),
  809. 'foo' => 'bar',
  810. 'bar' => 'foo',
  811. ];
  812. $collection->insert($document);
  813. $document = $collection->findAndModify(
  814. ['_id' => new \MongoId($id)],
  815. ['$set' => ['foo' => 'foo']],
  816. ['foo' => true]
  817. );
  818. $this->assertArrayNotHasKey('bar', $document);
  819. $this->assertArrayHasKey('foo', $document);
  820. }
  821. public function testGroup()
  822. {
  823. $collection = $this->getCollection();
  824. $document1 = ['a' => 2];
  825. $collection->insert($document1);
  826. $document2 = ['b' => 5];
  827. $collection->insert($document2);
  828. $document3 = ['a' => 1];
  829. $collection->insert($document3);
  830. $keys = [];
  831. $initial = ["count" => 0];
  832. $reduce = "function (obj, prev) { prev.count++; }";
  833. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  834. $result = $collection->group($keys, $initial, $reduce, $condition);
  835. $this->assertArraySubset(
  836. [
  837. 'retval' => [['count' => 1.0]],
  838. 'count' => 1.0,
  839. 'keys' => 1,
  840. 'ok' => 1.0,
  841. ],
  842. $result
  843. );
  844. }
  845. public function testMapReduce()
  846. {
  847. $data = array(
  848. array(
  849. 'username' => 'jones',
  850. 'likes' => 20.0,
  851. 'text' => 'Hello world!'
  852. ),
  853. array(
  854. 'username' => 'bob',
  855. 'likes' => 100.0,
  856. 'text' => 'Hello world!'
  857. ),
  858. array(
  859. 'username' => 'bob',
  860. 'likes' => 100.0,
  861. 'text' => 'Hello world!'
  862. ),
  863. );
  864. $collection = $this->getCollection();
  865. $collection->batchInsert($data);
  866. $map = 'function() {
  867. emit(this.username, { count: 1, likes: this.likes });
  868. }';
  869. $reduce = 'function(key, values) {
  870. var result = {count: 0, likes: 0};
  871. values.forEach(function(value) {
  872. result.count += value.count;
  873. result.likes += value.likes;
  874. });
  875. return result;
  876. }';
  877. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  878. $command = [
  879. 'mapreduce' => $this->getCollection()->getName(),
  880. 'map' => new \MongoCode($map),
  881. 'reduce' => new \MongoCode($reduce),
  882. 'query' => (object) [],
  883. 'out' => ['inline' => true],
  884. 'finalize' => new \MongoCode($finalize),
  885. ];
  886. $result = $this->getDatabase()->command($command);
  887. $expected = [
  888. [
  889. '_id' => 'bob',
  890. 'value' => [
  891. 'count' => 2.0,
  892. 'likes' => 200.0,
  893. 'test' => 'test',
  894. ],
  895. ],
  896. [
  897. '_id' => 'jones',
  898. 'value' => [
  899. 'count' => 1.0,
  900. 'likes' => 20.0,
  901. 'test' => 'test',
  902. ],
  903. ],
  904. ];
  905. $this->assertSame(1.0, $result['ok']);
  906. $this->assertSame($expected, $result['results']);
  907. }
  908. public function testFindAndModifyResultException()
  909. {
  910. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  911. $collection = $this->getCollection();
  912. $this->setExpectedException('MongoResultException');
  913. $collection->findAndModify(
  914. array("inprogress" => false, "name" => "Next promo"),
  915. array('$unsupportedOperator' => array("tasks" => -1)),
  916. array("tasks" => true),
  917. array("new" => true)
  918. );
  919. }
  920. public function testFindAndModifyExceptionTimeout()
  921. {
  922. $this->failMaxTimeMS();
  923. $id = '54203e08d51d4a1f868b456e';
  924. $collection = $this->getCollection();
  925. $this->setExpectedException('MongoExecutionTimeoutException');
  926. $document = $collection->findAndModify(
  927. ['_id' => new \MongoId($id)],
  928. null,
  929. null,
  930. ['maxTimeMS' => 1, 'remove' => true]
  931. );
  932. }
  933. public function testFindAndModifyRemove()
  934. {
  935. $id = '54203e08d51d4a1f868b456e';
  936. $collection = $this->getCollection();
  937. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  938. $collection->insert($document);
  939. $document = $collection->findAndModify(
  940. ['_id' => new \MongoId($id)],
  941. null,
  942. null,
  943. ['remove' => true]
  944. );
  945. $this->assertEquals('bar', $document['foo']);
  946. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  947. $this->assertSame(0, $newCollection->count());
  948. }
  949. public function testValidate()
  950. {
  951. $collection = $this->getCollection();
  952. $document = ['foo' => 'bar'];
  953. $collection->insert($document);
  954. $result = $collection->validate();
  955. $this->assertArraySubset(
  956. [
  957. 'ns' => 'mongo-php-adapter.test',
  958. 'nrecords' => 1,
  959. 'nIndexes' => 1,
  960. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  961. 'valid' => true,
  962. 'errors' => [],
  963. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  964. 'ok' => 1.0
  965. ],
  966. $result
  967. );
  968. }
  969. public function testDrop()
  970. {
  971. $document = ['foo' => 'bar'];
  972. $this->getCollection()->insert($document);
  973. $expected = [
  974. 'ns' => (string) $this->getCollection(),
  975. 'nIndexesWas' => 1,
  976. 'ok' => 1.0
  977. ];
  978. $this->assertSame($expected, $this->getCollection()->drop());
  979. }
  980. public function testEmptyCollectionName()
  981. {
  982. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  983. new \MongoCollection($this->getDatabase(), '');
  984. }
  985. public function testSelectCollectionWithNullBytes()
  986. {
  987. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  988. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  989. }
  990. public function testSubCollectionWithNullBytes()
  991. {
  992. $collection = $this->getCollection();
  993. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  994. $this->assertSame('test', $collection->getName());
  995. }
  996. }
  997. class PrivatePropertiesStub
  998. {
  999. private $foo = 'bar';
  1000. }