MongoCollectionTest.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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 testFindOneConnectionIssue()
  357. {
  358. $this->setExpectedException('MongoConnectionException');
  359. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  360. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  361. $collection->findOne();
  362. }
  363. public function testDistinct()
  364. {
  365. $this->prepareData();
  366. $values = $this->getCollection()->distinct('foo');
  367. $this->assertInternalType('array', $values);
  368. sort($values);
  369. $this->assertEquals(['bar', 'foo'], $values);
  370. }
  371. public function testDistinctWithQuery()
  372. {
  373. $this->prepareData();
  374. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  375. $this->assertInternalType('array', $values);
  376. $this->assertEquals(['bar'], $values);
  377. }
  378. public function testAggregate()
  379. {
  380. $this->skipTestUnless(extension_loaded('mongo'));
  381. $collection = $this->getCollection();
  382. $this->prepareData();
  383. $pipeline = [
  384. [
  385. '$group' => [
  386. '_id' => '$foo',
  387. 'count' => [ '$sum' => 1 ],
  388. ],
  389. ],
  390. [
  391. '$sort' => ['_id' => 1]
  392. ]
  393. ];
  394. $result = $collection->aggregate($pipeline);
  395. $this->assertInternalType('array', $result);
  396. $this->assertArrayHasKey('result', $result);
  397. $this->assertEquals([
  398. ['_id' => 'bar', 'count' => 2],
  399. ['_id' => 'foo', 'count' => 1],
  400. ], $result['result']);
  401. }
  402. public function testAggregateTimeoutException()
  403. {
  404. $this->skipTestUnless(extension_loaded('mongo'));
  405. $collection = $this->getCollection();
  406. $this->failMaxTimeMS();
  407. $this->setExpectedException('MongoExecutionTimeoutException');
  408. $pipeline = [
  409. [
  410. '$group' => [
  411. '_id' => '$foo',
  412. 'count' => [ '$sum' => 1 ],
  413. ],
  414. ],
  415. [
  416. '$sort' => ['_id' => 1]
  417. ]
  418. ];
  419. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  420. }
  421. public function testAggregateCursor()
  422. {
  423. $collection = $this->getCollection();
  424. $this->prepareData();
  425. $pipeline = [
  426. [
  427. '$group' => [
  428. '_id' => '$foo',
  429. 'count' => [ '$sum' => 1 ],
  430. ],
  431. ],
  432. [
  433. '$sort' => ['_id' => 1]
  434. ]
  435. ];
  436. $cursor = $collection->aggregateCursor($pipeline);
  437. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  438. $this->assertEquals([
  439. ['_id' => 'bar', 'count' => 2],
  440. ['_id' => 'foo', 'count' => 1],
  441. ], iterator_to_array($cursor));
  442. }
  443. public function testReadPreference()
  444. {
  445. $collection = $this->getCollection();
  446. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  447. $this->assertFalse($collection->getSlaveOkay());
  448. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  449. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  450. $this->assertTrue($collection->getSlaveOkay());
  451. $this->assertTrue($collection->setSlaveOkay(true));
  452. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  453. $this->assertTrue($collection->setSlaveOkay(false));
  454. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  455. }
  456. public function testReadPreferenceIsSetInDriver()
  457. {
  458. $this->skipTestIf(extension_loaded('mongo'));
  459. $collection = $this->getCollection();
  460. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  461. // Only way to check whether options are passed down is through debugInfo
  462. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  463. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  464. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  465. }
  466. public function testReadPreferenceIsInherited()
  467. {
  468. $database = $this->getDatabase();
  469. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  470. $collection = $database->selectCollection('test');
  471. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  472. }
  473. public function testWriteConcern()
  474. {
  475. $collection = $this->getCollection();
  476. $this->assertTrue($collection->setWriteConcern('majority', 100));
  477. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  478. }
  479. public function testWriteConcernIsSetInDriver()
  480. {
  481. $this->skipTestIf(extension_loaded('mongo'));
  482. $collection = $this->getCollection();
  483. $this->assertTrue($collection->setWriteConcern(2, 100));
  484. // Only way to check whether options are passed down is through debugInfo
  485. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  486. $this->assertSame(2, $writeConcern->getW());
  487. $this->assertSame(100, $writeConcern->getWtimeout());
  488. }
  489. public function testWriteConcernIsInherited()
  490. {
  491. $database = $this->getDatabase();
  492. $database->setWriteConcern('majority', 100);
  493. $collection = $database->selectCollection('test');
  494. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  495. }
  496. public function testSaveInsert()
  497. {
  498. $id = '54203e08d51d4a1f868b456e';
  499. $collection = $this->getCollection();
  500. $objectId = new \MongoId($id);
  501. $expected = [
  502. 'ok' => 1.0,
  503. 'nModified' => 0,
  504. 'n' => 1,
  505. 'err' => null,
  506. 'errmsg' => null,
  507. 'upserted' => $objectId,
  508. 'updatedExisting' => false,
  509. ];
  510. $document = ['_id' => $objectId, 'foo' => 'bar'];
  511. $this->assertEquals($expected, $collection->save($document));
  512. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  513. $this->assertSame(1, $newCollection->count());
  514. $object = $newCollection->findOne();
  515. $this->assertNotNull($object);
  516. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  517. $this->assertSame($id, (string) $object->_id);
  518. $this->assertObjectHasAttribute('foo', $object);
  519. $this->assertAttributeSame('bar', 'foo', $object);
  520. }
  521. public function testRemoveOne()
  522. {
  523. $id = '54203e08d51d4a1f868b456e';
  524. $collection = $this->getCollection();
  525. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  526. $collection->insert($document);
  527. $collection->remove(['_id' => new \MongoId($id)]);
  528. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  529. $this->assertSame(0, $newCollection->count());
  530. }
  531. public function testSaveUpdate()
  532. {
  533. $expected = [
  534. 'ok' => 1.0,
  535. 'nModified' => 1,
  536. 'n' => 1,
  537. 'err' => null,
  538. 'errmsg' => null,
  539. 'updatedExisting' => true,
  540. ];
  541. $id = '54203e08d51d4a1f868b456e';
  542. $collection = $this->getCollection();
  543. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  544. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  545. $collection->insert($insertDocument);
  546. $this->assertSame($expected, $collection->save($saveDocument));
  547. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  548. $this->assertSame(1, $newCollection->count());
  549. $object = $newCollection->findOne();
  550. $this->assertNotNull($object);
  551. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  552. $this->assertSame($id, (string) $object->_id);
  553. $this->assertObjectHasAttribute('foo', $object);
  554. $this->assertAttributeSame('foo', 'foo', $object);
  555. }
  556. public function testSavingShouldReplaceTheWholeDocument() {
  557. $id = '54203e08d51d4a1f868b456e';
  558. $collection = $this->getCollection();
  559. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  560. $saveDocument = ['_id' => new \MongoId($id)];
  561. $collection->insert($insertDocument);
  562. $collection->save($saveDocument);
  563. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  564. $this->assertSame(1, $newCollection->count());
  565. $object = $newCollection->findOne();
  566. $this->assertNotNull($object);
  567. $this->assertObjectNotHasAttribute('foo', $object);
  568. }
  569. public function testSaveDuplicate()
  570. {
  571. $collection = $this->getCollection();
  572. $collection->createIndex(['foo' => 1], ['unique' => true]);
  573. $document = ['foo' => 'bar'];
  574. $collection->save($document);
  575. $this->setExpectedException('MongoDuplicateKeyException');
  576. unset($document['_id']);
  577. $collection->save($document);
  578. }
  579. public function testSaveEmptyKeys()
  580. {
  581. $document = [];
  582. $this->getCollection()->save($document);
  583. $this->assertSame(1, $this->getCollection()->count());
  584. }
  585. public function testSaveEmptyObject()
  586. {
  587. $document = (object) [];
  588. $this->getCollection()->save($document);
  589. $this->assertSame(1, $this->getCollection()->count());
  590. }
  591. public function testGetDBRef()
  592. {
  593. $collection = $this->getCollection();
  594. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  595. $collection->insert($insertDocument);
  596. $document = $collection->getDBRef([
  597. '$ref' => 'test',
  598. '$id' => 1,
  599. ]);
  600. $this->assertEquals($insertDocument, $document);
  601. }
  602. public function testCreateDBRef()
  603. {
  604. $collection = $this->getCollection();
  605. $reference = $collection->createDBRef(['_id' => 'foo']);
  606. $this->assertSame(
  607. [
  608. '$ref' => 'test',
  609. '$id' => 'foo',
  610. ],
  611. $reference
  612. );
  613. }
  614. public function testCreateIndex()
  615. {
  616. $expected = [
  617. 'createdCollectionAutomatically' => true,
  618. 'numIndexesBefore' => 1,
  619. 'numIndexesAfter' => 2,
  620. 'ok' => 1.0,
  621. ];
  622. $collection = $this->getCollection();
  623. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  624. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  625. $iterator = $newCollection->listIndexes();
  626. $indexes = iterator_to_array($iterator);
  627. $this->assertCount(2, $indexes);
  628. $index = $indexes[1];
  629. $this->assertSame(['foo' => 1], $index->getKey());
  630. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  631. }
  632. public function testCreateIndexInvalid()
  633. {
  634. $this->setExpectedException('MongoException', 'index specification has no elements');
  635. $this->getCollection()->createIndex([]);
  636. }
  637. public function testCreateIndexTwice()
  638. {
  639. $this->getCollection()->createIndex(['foo' => 1]);
  640. $expected = [
  641. 'createdCollectionAutomatically' => false,
  642. 'numIndexesBefore' => 2,
  643. 'numIndexesAfter' => 2,
  644. 'note' => 'all indexes already exist',
  645. 'ok' => 1.0
  646. ];
  647. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  648. }
  649. public function testCreateIndexesWithDifferentOptions()
  650. {
  651. $this->setExpectedException('MongoResultException');
  652. $this->getCollection()->createIndex(['foo' => 1]);
  653. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  654. }
  655. public function testCreateIndexWithSameName()
  656. {
  657. $this->setExpectedException('MongoResultException');
  658. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  659. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  660. }
  661. public function testEnsureIndex()
  662. {
  663. $expected = [
  664. 'createdCollectionAutomatically' => true,
  665. 'numIndexesBefore' => 1,
  666. 'numIndexesAfter' => 2,
  667. 'ok' => 1.0
  668. ];
  669. $collection = $this->getCollection();
  670. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  671. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  672. $indexes = iterator_to_array($newCollection->listIndexes());
  673. $this->assertCount(2, $indexes);
  674. $index = $indexes[1];
  675. $this->assertSame(['bar' => 1], $index->getKey());
  676. $this->assertTrue($index->isUnique());
  677. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  678. }
  679. public function testDeleteIndexUsingIndexName()
  680. {
  681. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  682. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  683. $expected = [
  684. 'nIndexesWas' => 2,
  685. 'errmsg' => 'index not found with name [bar_1]',
  686. 'ok' => 0.0,
  687. 'code' => 27,
  688. ];
  689. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  690. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  691. }
  692. public function testDeleteIndexUsingField()
  693. {
  694. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  695. $newCollection->createIndex(['bar' => 1]);
  696. $expected = [
  697. 'nIndexesWas' => 2,
  698. 'ok' => 1.0,
  699. ];
  700. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  701. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  702. }
  703. public function testDeleteIndexUsingKeys()
  704. {
  705. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  706. $newCollection->createIndex(['bar' => 1]);
  707. $expected = [
  708. 'nIndexesWas' => 2,
  709. 'ok' => 1.0,
  710. ];
  711. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  712. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  713. }
  714. public function testDeleteIndexes()
  715. {
  716. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  717. $newCollection->createIndex(['bar' => 1]);
  718. $expected = [
  719. 'nIndexesWas' => 2,
  720. 'msg' => 'non-_id indexes dropped for collection',
  721. 'ok' => 1.0,
  722. ];
  723. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  724. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  725. }
  726. public function testGetIndexInfo()
  727. {
  728. $collection = $this->getCollection();
  729. $collection->createIndex(['foo' => 1]);
  730. $expected = [
  731. [
  732. 'v' => 1,
  733. 'key' => ['_id' => 1],
  734. 'name' => '_id_',
  735. 'ns' => 'mongo-php-adapter.test',
  736. ],
  737. [
  738. 'v' => 1,
  739. 'key' => ['foo' => 1],
  740. 'name' => 'foo_1',
  741. 'ns' => 'mongo-php-adapter.test',
  742. ],
  743. ];
  744. $this->assertSame(
  745. $expected,
  746. $collection->getIndexInfo()
  747. );
  748. }
  749. public function testFindAndModifyUpdate()
  750. {
  751. $id = '54203e08d51d4a1f868b456e';
  752. $collection = $this->getCollection();
  753. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  754. $collection->insert($document);
  755. $document = $collection->findAndModify(
  756. ['_id' => new \MongoId($id)],
  757. ['$set' => ['foo' => 'foo']]
  758. );
  759. $this->assertSame('bar', $document['foo']);
  760. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  761. $this->assertSame(1, $newCollection->count());
  762. $object = $newCollection->findOne();
  763. $this->assertNotNull($object);
  764. $this->assertAttributeSame('foo', 'foo', $object);
  765. }
  766. public function testFindAndModifyUpdateReplace()
  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. ['_id' => new \MongoId($id), 'foo' => 'boo']
  775. );
  776. $this->assertSame('bar', $document['foo']);
  777. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  778. $this->assertSame(1, $newCollection->count());
  779. $object = $newCollection->findOne();
  780. $this->assertNotNull($object);
  781. $this->assertAttributeSame('boo', 'foo', $object);
  782. $this->assertObjectNotHasAttribute('bar', $object);
  783. }
  784. public function testFindAndModifyUpdateReturnNew()
  785. {
  786. $id = '54203e08d51d4a1f868b456e';
  787. $collection = $this->getCollection();
  788. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  789. $collection->insert($document);
  790. $document = $collection->findAndModify(
  791. ['_id' => new \MongoId($id)],
  792. ['$set' => ['foo' => 'foo']],
  793. null,
  794. ['new' => true]
  795. );
  796. $this->assertSame('foo', $document['foo']);
  797. }
  798. public function testFindAndModifyWithFields()
  799. {
  800. $id = '54203e08d51d4a1f868b456e';
  801. $collection = $this->getCollection();
  802. $document = [
  803. '_id' => new \MongoId($id),
  804. 'foo' => 'bar',
  805. 'bar' => 'foo',
  806. ];
  807. $collection->insert($document);
  808. $document = $collection->findAndModify(
  809. ['_id' => new \MongoId($id)],
  810. ['$set' => ['foo' => 'foo']],
  811. ['foo' => true]
  812. );
  813. $this->assertArrayNotHasKey('bar', $document);
  814. $this->assertArrayHasKey('foo', $document);
  815. }
  816. public function testGroup()
  817. {
  818. $collection = $this->getCollection();
  819. $document1 = ['a' => 2];
  820. $collection->insert($document1);
  821. $document2 = ['b' => 5];
  822. $collection->insert($document2);
  823. $document3 = ['a' => 1];
  824. $collection->insert($document3);
  825. $keys = [];
  826. $initial = ["count" => 0];
  827. $reduce = "function (obj, prev) { prev.count++; }";
  828. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  829. $result = $collection->group($keys, $initial, $reduce, $condition);
  830. $this->assertArraySubset(
  831. [
  832. 'retval' => [['count' => 1.0]],
  833. 'count' => 1.0,
  834. 'keys' => 1,
  835. 'ok' => 1.0,
  836. ],
  837. $result
  838. );
  839. }
  840. public function testMapReduce()
  841. {
  842. $data = array(
  843. array(
  844. 'username' => 'jones',
  845. 'likes' => 20.0,
  846. 'text' => 'Hello world!'
  847. ),
  848. array(
  849. 'username' => 'bob',
  850. 'likes' => 100.0,
  851. 'text' => 'Hello world!'
  852. ),
  853. array(
  854. 'username' => 'bob',
  855. 'likes' => 100.0,
  856. 'text' => 'Hello world!'
  857. ),
  858. );
  859. $collection = $this->getCollection();
  860. $collection->batchInsert($data);
  861. $map = 'function() {
  862. emit(this.username, { count: 1, likes: this.likes });
  863. }';
  864. $reduce = 'function(key, values) {
  865. var result = {count: 0, likes: 0};
  866. values.forEach(function(value) {
  867. result.count += value.count;
  868. result.likes += value.likes;
  869. });
  870. return result;
  871. }';
  872. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  873. $command = [
  874. 'mapreduce' => $this->getCollection()->getName(),
  875. 'map' => new \MongoCode($map),
  876. 'reduce' => new \MongoCode($reduce),
  877. 'query' => (object) [],
  878. 'out' => ['inline' => true],
  879. 'finalize' => new \MongoCode($finalize),
  880. ];
  881. $result = $this->getDatabase()->command($command);
  882. $expected = [
  883. [
  884. '_id' => 'bob',
  885. 'value' => [
  886. 'count' => 2.0,
  887. 'likes' => 200.0,
  888. 'test' => 'test',
  889. ],
  890. ],
  891. [
  892. '_id' => 'jones',
  893. 'value' => [
  894. 'count' => 1.0,
  895. 'likes' => 20.0,
  896. 'test' => 'test',
  897. ],
  898. ],
  899. ];
  900. $this->assertSame(1.0, $result['ok']);
  901. $this->assertSame($expected, $result['results']);
  902. }
  903. public function testFindAndModifyResultException()
  904. {
  905. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  906. $collection = $this->getCollection();
  907. $this->setExpectedException('MongoResultException');
  908. $collection->findAndModify(
  909. array("inprogress" => false, "name" => "Next promo"),
  910. array('$unsupportedOperator' => array("tasks" => -1)),
  911. array("tasks" => true),
  912. array("new" => true)
  913. );
  914. }
  915. public function testFindAndModifyExceptionTimeout()
  916. {
  917. $this->failMaxTimeMS();
  918. $id = '54203e08d51d4a1f868b456e';
  919. $collection = $this->getCollection();
  920. $this->setExpectedException('MongoExecutionTimeoutException');
  921. $document = $collection->findAndModify(
  922. ['_id' => new \MongoId($id)],
  923. null,
  924. null,
  925. ['maxTimeMS' => 1, 'remove' => true]
  926. );
  927. }
  928. public function testFindAndModifyRemove()
  929. {
  930. $id = '54203e08d51d4a1f868b456e';
  931. $collection = $this->getCollection();
  932. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  933. $collection->insert($document);
  934. $document = $collection->findAndModify(
  935. ['_id' => new \MongoId($id)],
  936. null,
  937. null,
  938. ['remove' => true]
  939. );
  940. $this->assertEquals('bar', $document['foo']);
  941. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  942. $this->assertSame(0, $newCollection->count());
  943. }
  944. public function testValidate()
  945. {
  946. $collection = $this->getCollection();
  947. $document = ['foo' => 'bar'];
  948. $collection->insert($document);
  949. $result = $collection->validate();
  950. $this->assertArraySubset(
  951. [
  952. 'ns' => 'mongo-php-adapter.test',
  953. 'nrecords' => 1,
  954. 'nIndexes' => 1,
  955. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  956. 'valid' => true,
  957. 'errors' => [],
  958. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  959. 'ok' => 1.0
  960. ],
  961. $result
  962. );
  963. }
  964. public function testDrop()
  965. {
  966. $document = ['foo' => 'bar'];
  967. $this->getCollection()->insert($document);
  968. $expected = [
  969. 'ns' => (string) $this->getCollection(),
  970. 'nIndexesWas' => 1,
  971. 'ok' => 1.0
  972. ];
  973. $this->assertSame($expected, $this->getCollection()->drop());
  974. }
  975. public function testEmptyCollectionName()
  976. {
  977. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  978. new \MongoCollection($this->getDatabase(), '');
  979. }
  980. public function testSelectCollectionWithNullBytes()
  981. {
  982. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  983. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  984. }
  985. public function testSubCollectionWithNullBytes()
  986. {
  987. $collection = $this->getCollection();
  988. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  989. $this->assertSame('test', $collection->getName());
  990. }
  991. }
  992. class PrivatePropertiesStub
  993. {
  994. private $foo = 'bar';
  995. }