MongoCollectionTest.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use MongoDB\Driver\ReadPreference;
  4. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  5. /**
  6. * @author alcaeus <alcaeus@alcaeus.org>
  7. */
  8. class MongoCollectionTest extends TestCase
  9. {
  10. public function testSerialize()
  11. {
  12. $this->assertInternalType('string', serialize($this->getCollection()));
  13. }
  14. public function testGetNestedCollections()
  15. {
  16. $collection = $this->getCollection()->foo->bar;
  17. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  18. }
  19. public function testCreateRecord()
  20. {
  21. $collection = $this->getCollection();
  22. $expected = [
  23. 'ok' => 1.0,
  24. 'n' => 0,
  25. 'err' => null,
  26. 'errmsg' => null,
  27. ];
  28. $document = ['foo' => 'bar'];
  29. $this->assertSame($expected, $collection->insert($document));
  30. $this->assertInstanceOf('MongoId', $document['_id']);
  31. $id = (string) $document['_id'];
  32. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  33. $this->assertSame(1, $newCollection->count());
  34. $object = $newCollection->findOne();
  35. $this->assertNotNull($object);
  36. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  37. $this->assertSame($id, (string) $object->_id);
  38. $this->assertObjectHasAttribute('foo', $object);
  39. $this->assertAttributeSame('bar', 'foo', $object);
  40. }
  41. public function testInsertInvalidData()
  42. {
  43. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
  44. $document = 8;
  45. $this->getCollection()->insert($document);
  46. }
  47. public function testInsertEmptyArray()
  48. {
  49. $document = [];
  50. $this->getCollection()->insert($document);
  51. $this->assertSame(1, $this->getCollection()->count());
  52. }
  53. public function testInsertArrayWithNumericKeys()
  54. {
  55. $document = [1 => 'foo'];
  56. $this->getCollection()->insert($document);
  57. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  58. }
  59. public function testInsertEmptyObject()
  60. {
  61. $document = (object) [];
  62. $this->getCollection()->insert($document);
  63. $this->assertSame(1, $this->getCollection()->count());
  64. }
  65. public function testInsertObjectWithPrivateProperties()
  66. {
  67. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  68. $document = new PrivatePropertiesStub();
  69. $this->getCollection()->insert($document);
  70. }
  71. public function testInsertWithInvalidKey()
  72. {
  73. $document = ['*' => 'foo'];
  74. $this->getCollection()->insert($document);
  75. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  76. }
  77. public function testInsertWithEmptyKey()
  78. {
  79. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  80. $document = ['' => 'foo'];
  81. $this->getCollection()->insert($document);
  82. }
  83. public function testInsertWithNumericKey()
  84. {
  85. $document = ['foo'];
  86. $this->getCollection()->insert($document);
  87. $this->assertSame(1, $this->getCollection()->count(['foo']));
  88. }
  89. public function testInsertDuplicate()
  90. {
  91. $collection = $this->getCollection();
  92. $collection->createIndex(['foo' => 1], ['unique' => true]);
  93. $document = ['foo' => 'bar'];
  94. $collection->insert($document);
  95. unset($document['_id']);
  96. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
  97. $collection->insert($document);
  98. }
  99. public function testUnacknowledgedWrite()
  100. {
  101. $document = ['foo' => 'bar'];
  102. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  103. }
  104. public function testInsertWriteConcernException()
  105. {
  106. $this->setExpectedException(
  107. 'MongoWriteConcernException',
  108. "cannot use 'w' > 1 when a host is not replicated"
  109. );
  110. $document = ['foo' => 'bar'];
  111. $this->getCollection()->insert($document, ['w' => 2]);
  112. }
  113. public function testInsertMany()
  114. {
  115. $expected = [
  116. 'ok' => 1.0,
  117. 'n' => 0,
  118. 'syncMillis' => 0,
  119. 'writtenTo' => null,
  120. 'err' => null,
  121. ];
  122. $documents = [
  123. ['foo' => 'bar'],
  124. ['bar' => 'foo']
  125. ];
  126. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  127. foreach ($documents as $document) {
  128. $this->assertInstanceOf('MongoId', $document['_id']);
  129. }
  130. }
  131. public function testInsertManyWithNonNumericKeys()
  132. {
  133. $expected = [
  134. 'ok' => 1.0,
  135. 'n' => 0,
  136. 'syncMillis' => 0,
  137. 'writtenTo' => null,
  138. 'err' => null,
  139. ];
  140. $documents = [
  141. 'a' => ['foo' => 'bar'],
  142. 'b' => ['bar' => 'foo']
  143. ];
  144. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  145. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  146. $this->assertSame(2, $newCollection->count());
  147. }
  148. public function testBatchInsertContinuesOnError()
  149. {
  150. $expected = [
  151. 'ok' => 1.0,
  152. 'n' => 0,
  153. 'syncMillis' => 0,
  154. 'writtenTo' => null,
  155. 'err' => null,
  156. ];
  157. $documents = [
  158. 8,
  159. 'b' => ['bar' => 'foo']
  160. ];
  161. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  162. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  163. $this->assertSame(1, $newCollection->count());
  164. }
  165. public function testBatchInsertException()
  166. {
  167. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
  168. $id = new \MongoId();
  169. $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
  170. $this->getCollection()->batchInsert($documents);
  171. }
  172. public function testBatchInsertObjectWithPrivateProperties()
  173. {
  174. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  175. $documents = [new PrivatePropertiesStub()];
  176. $this->getCollection()->batchInsert($documents);
  177. }
  178. public function testBatchInsertWithInvalidKey()
  179. {
  180. $documents = [['*' => 'foo']];
  181. $this->getCollection()->batchInsert($documents);
  182. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  183. }
  184. public function testBatchInsertWithEmptyKey()
  185. {
  186. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  187. $documents = [['' => 'foo']];
  188. $this->getCollection()->batchInsert($documents);
  189. }
  190. public function testBatchInsertWithNumericKey()
  191. {
  192. $documents = [['foo']];
  193. $this->getCollection()->batchInsert($documents);
  194. $this->assertSame(1, $this->getCollection()->count(['foo']));
  195. }
  196. public function testBatchInsertEmptyBatchException()
  197. {
  198. $this->setExpectedException('MongoException', 'No write ops were included in the batch');
  199. $documents = [];
  200. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  201. }
  202. public function testUpdateWriteConcern()
  203. {
  204. $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
  205. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  206. }
  207. public function testUpdateOne()
  208. {
  209. $document = ['foo' => 'bar'];
  210. $this->getCollection()->insert($document);
  211. // Unset ID to re-insert
  212. unset($document['_id']);
  213. $this->getCollection()->insert($document);
  214. $expected = [
  215. 'ok' => 1.0,
  216. 'nModified' => 1,
  217. 'n' => 1,
  218. 'err' => null,
  219. 'errmsg' => null,
  220. 'updatedExisting' => true,
  221. ];
  222. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  223. $this->assertSame($expected, $result);
  224. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  225. }
  226. public function testUpdateReplaceOne()
  227. {
  228. $document = ['foo' => 'bar', 'bar' => 'foo'];
  229. $this->getCollection()->insert($document);
  230. // Unset ID to re-insert
  231. unset($document['_id']);
  232. $this->getCollection()->insert($document);
  233. $expected = [
  234. 'ok' => 1.0,
  235. 'nModified' => 1,
  236. 'n' => 1,
  237. 'err' => null,
  238. 'errmsg' => null,
  239. 'updatedExisting' => true,
  240. ];
  241. $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
  242. $this->assertSame($expected, $result);
  243. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  244. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
  245. }
  246. public function testUpdateReplaceMultiple()
  247. {
  248. $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
  249. $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
  250. }
  251. public function testUpdateDuplicate()
  252. {
  253. $collection = $this->getCollection();
  254. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  255. $document = ['foo' => 'bar'];
  256. $collection->insert($document);
  257. $document = ['foo' => 'foo'];
  258. $collection->insert($document);
  259. $this->setExpectedException('MongoDuplicateKeyException');
  260. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  261. }
  262. public function testUpdateMany()
  263. {
  264. $document = ['change' => true, 'foo' => 'bar'];
  265. $this->getCollection()->insert($document);
  266. unset($document['_id']);
  267. $this->getCollection()->insert($document);
  268. $document = ['change' => true, 'foo' => 'foo'];
  269. $this->getCollection()->insert($document);
  270. $expected = [
  271. 'ok' => 1.0,
  272. 'nModified' => 2,
  273. 'n' => 3,
  274. 'err' => null,
  275. 'errmsg' => null,
  276. 'updatedExisting' => true,
  277. ];
  278. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  279. $this->assertSame($expected, $result);
  280. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  281. }
  282. public function testUnacknowledgedUpdate()
  283. {
  284. $document = ['foo' => 'bar'];
  285. $this->getCollection()->insert($document);
  286. unset($document['_id']);
  287. $this->getCollection()->insert($document);
  288. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  289. }
  290. public function testRemoveMultiple()
  291. {
  292. $document = ['change' => true, 'foo' => 'bar'];
  293. $this->getCollection()->insert($document);
  294. unset($document['_id']);
  295. $this->getCollection()->insert($document);
  296. $document = ['change' => true, 'foo' => 'foo'];
  297. $this->getCollection()->insert($document);
  298. $expected = [
  299. 'ok' => 1.0,
  300. 'n' => 2,
  301. 'err' => null,
  302. 'errmsg' => null,
  303. ];
  304. $result = $this->getCollection()->remove(['foo' => 'bar']);
  305. $this->assertSame($expected, $result);
  306. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  307. }
  308. public function testRemoveSingle()
  309. {
  310. $document = ['change' => true, 'foo' => 'bar'];
  311. $this->getCollection()->insert($document);
  312. unset($document['_id']);
  313. $this->getCollection()->insert($document);
  314. unset($document['_id']);
  315. $this->getCollection()->insert($document);
  316. $expected = [
  317. 'ok' => 1.0,
  318. 'n' => 1,
  319. 'err' => null,
  320. 'errmsg' => null,
  321. ];
  322. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  323. $this->assertSame($expected, $result);
  324. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  325. }
  326. public function testRemoveUnacknowledged()
  327. {
  328. $document = ['change' => true, 'foo' => 'bar'];
  329. $this->getCollection()->insert($document);
  330. unset($document['_id']);
  331. $this->getCollection()->insert($document);
  332. unset($document['_id']);
  333. $this->getCollection()->insert($document);
  334. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  335. }
  336. public function testFindReturnsCursor()
  337. {
  338. $this->prepareData();
  339. $collection = $this->getCollection();
  340. $this->assertInstanceOf('MongoCursor', $collection->find());
  341. }
  342. public function testCount()
  343. {
  344. $this->prepareData();
  345. $collection = $this->getCollection();
  346. $this->assertSame(3, $collection->count());
  347. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  348. }
  349. public function testCountWithLimit()
  350. {
  351. $this->prepareData();
  352. $collection = $this->getCollection();
  353. $this->assertSame(2, $collection->count([], ['limit' => 2]));
  354. $this->assertSame(1, $collection->count(['foo' => 'bar'], ['limit' => 1]));
  355. }
  356. public function testCountWithLimitLegacy()
  357. {
  358. $this->prepareData();
  359. $collection = $this->getCollection();
  360. $this->assertSame(2, $collection->count([], 2));
  361. $this->assertSame(1, $collection->count(['foo' => 'bar'], 1));
  362. }
  363. public function testCountWithSkip()
  364. {
  365. $this->prepareData();
  366. $collection = $this->getCollection();
  367. $this->assertSame(2, $collection->count([], ['skip' => 1]));
  368. $this->assertSame(1, $collection->count(['foo' => 'bar'], ['skip' => 1]));
  369. }
  370. public function testCountWithSkipLegacy()
  371. {
  372. $this->prepareData();
  373. $collection = $this->getCollection();
  374. $this->assertSame(2, $collection->count([], null, 1));
  375. $this->assertSame(1, $collection->count(['foo' => 'bar'], null, 1));
  376. }
  377. public function testCountWithLimitAndSkip()
  378. {
  379. $this->prepareData();
  380. $collection = $this->getCollection();
  381. $this->assertSame(2, $collection->count([], ['skip' => 1, 'limit' => 2]));
  382. $this->assertSame(1, $collection->count([], ['skip' => 1, 'limit' => 1]));
  383. }
  384. public function testCountWithLimitAndSkipLegacy()
  385. {
  386. $this->prepareData();
  387. $collection = $this->getCollection();
  388. $this->assertSame(2, $collection->count([], 2, 1));
  389. $this->assertSame(1, $collection->count([], 1, 1));
  390. }
  391. public function testCountTimeout()
  392. {
  393. $this->failMaxTimeMS();
  394. $this->setExpectedException('MongoExecutionTimeoutException');
  395. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  396. }
  397. public function testFindOne()
  398. {
  399. $this->prepareData();
  400. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  401. $this->assertSame(['foo' => 'foo'], $document);
  402. }
  403. public function testFindOneNotFound()
  404. {
  405. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  406. $this->assertNull($document);
  407. }
  408. public function testFindOneConnectionIssue()
  409. {
  410. $this->setExpectedException('MongoConnectionException');
  411. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  412. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  413. $collection->findOne();
  414. }
  415. public function testDistinct()
  416. {
  417. $this->prepareData();
  418. $values = $this->getCollection()->distinct('foo');
  419. $this->assertInternalType('array', $values);
  420. sort($values);
  421. $this->assertEquals(['bar', 'foo'], $values);
  422. }
  423. public function testDistinctWithQuery()
  424. {
  425. $this->prepareData();
  426. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  427. $this->assertInternalType('array', $values);
  428. $this->assertEquals(['bar'], $values);
  429. }
  430. public function testDistinctWithIdQuery()
  431. {
  432. $document1 = ['foo' => 'bar'];
  433. $document2 = ['foo' => 'bar'];
  434. $document3 = ['foo' => 'foo'];
  435. $collection = $this->getCollection();
  436. $collection->insert($document1);
  437. $collection->insert($document2);
  438. $collection->insert($document3);
  439. $this->assertSame(
  440. ['bar'],
  441. $collection->distinct('foo', ['_id' => [
  442. '$in' => [$document1['_id'], $document2['_id']]
  443. ]])
  444. );
  445. $this->assertEquals(
  446. ['bar', 'foo'],
  447. $collection->distinct('foo', ['_id' => [
  448. '$in' => [$document1['_id'], $document3['_id']]
  449. ]])
  450. );
  451. }
  452. public function testAggregate()
  453. {
  454. $collection = $this->getCollection();
  455. $this->prepareData();
  456. $pipeline = [
  457. [
  458. '$group' => [
  459. '_id' => '$foo',
  460. 'count' => [ '$sum' => 1 ],
  461. ],
  462. ],
  463. [
  464. '$sort' => ['_id' => 1]
  465. ]
  466. ];
  467. $result = $collection->aggregate($pipeline);
  468. $this->assertInternalType('array', $result);
  469. $this->assertArrayHasKey('result', $result);
  470. $this->assertEquals([
  471. ['_id' => 'bar', 'count' => 2],
  472. ['_id' => 'foo', 'count' => 1],
  473. ], $result['result']);
  474. }
  475. public function testAggregateInvalidPipeline()
  476. {
  477. $collection = $this->getCollection();
  478. $pipeline = [
  479. [
  480. '$invalid' => []
  481. ],
  482. ];
  483. $this->setExpectedException('MongoResultException', 'Unrecognized pipeline stage name');
  484. $collection->aggregate($pipeline);
  485. }
  486. public function testAggregateTimeoutException()
  487. {
  488. $collection = $this->getCollection();
  489. $this->failMaxTimeMS();
  490. $this->setExpectedException('MongoExecutionTimeoutException');
  491. $pipeline = [
  492. [
  493. '$group' => [
  494. '_id' => '$foo',
  495. 'count' => [ '$sum' => 1 ],
  496. ],
  497. ],
  498. [
  499. '$sort' => ['_id' => 1]
  500. ]
  501. ];
  502. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  503. }
  504. public function testAggregateCursor()
  505. {
  506. $collection = $this->getCollection();
  507. $this->prepareData();
  508. $pipeline = [
  509. [
  510. '$group' => [
  511. '_id' => '$foo',
  512. 'count' => [ '$sum' => 1 ],
  513. ],
  514. ],
  515. [
  516. '$sort' => ['_id' => 1]
  517. ]
  518. ];
  519. $cursor = $collection->aggregateCursor($pipeline);
  520. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  521. $this->assertEquals([
  522. ['_id' => 'bar', 'count' => 2],
  523. ['_id' => 'foo', 'count' => 1],
  524. ], iterator_to_array($cursor));
  525. }
  526. public function testReadPreference()
  527. {
  528. $collection = $this->getCollection();
  529. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  530. $this->assertFalse($collection->getSlaveOkay());
  531. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  532. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  533. $this->assertTrue($collection->getSlaveOkay());
  534. $this->assertTrue($collection->setSlaveOkay(true));
  535. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  536. $this->assertTrue($collection->setSlaveOkay(false));
  537. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  538. }
  539. public function testReadPreferenceIsSetInDriver()
  540. {
  541. $this->skipTestIf(extension_loaded('mongo'));
  542. $collection = $this->getCollection();
  543. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  544. // Only way to check whether options are passed down is through debugInfo
  545. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  546. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  547. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  548. }
  549. public function testReadPreferenceIsInherited()
  550. {
  551. $database = $this->getDatabase();
  552. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  553. $collection = $database->selectCollection('test');
  554. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  555. }
  556. public function testWriteConcern()
  557. {
  558. $collection = $this->getCollection();
  559. $this->assertTrue($collection->setWriteConcern('majority', 100));
  560. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  561. }
  562. public function testWriteConcernIsSetInDriver()
  563. {
  564. $this->skipTestIf(extension_loaded('mongo'));
  565. $collection = $this->getCollection();
  566. $this->assertTrue($collection->setWriteConcern(2, 100));
  567. // Only way to check whether options are passed down is through debugInfo
  568. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  569. $this->assertSame(2, $writeConcern->getW());
  570. $this->assertSame(100, $writeConcern->getWtimeout());
  571. }
  572. public function testWriteConcernIsInherited()
  573. {
  574. $database = $this->getDatabase();
  575. $database->setWriteConcern('majority', 100);
  576. $collection = $database->selectCollection('test');
  577. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  578. }
  579. public function testSaveInsert()
  580. {
  581. $id = '54203e08d51d4a1f868b456e';
  582. $collection = $this->getCollection();
  583. $objectId = new \MongoId($id);
  584. $expected = [
  585. 'ok' => 1.0,
  586. 'nModified' => 0,
  587. 'n' => 1,
  588. 'err' => null,
  589. 'errmsg' => null,
  590. 'upserted' => $objectId,
  591. 'updatedExisting' => false,
  592. ];
  593. $document = ['_id' => $objectId, 'foo' => 'bar'];
  594. $this->assertEquals($expected, $collection->save($document));
  595. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  596. $this->assertSame(1, $newCollection->count());
  597. $object = $newCollection->findOne();
  598. $this->assertNotNull($object);
  599. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  600. $this->assertSame($id, (string) $object->_id);
  601. $this->assertObjectHasAttribute('foo', $object);
  602. $this->assertAttributeSame('bar', 'foo', $object);
  603. }
  604. public function testRemoveOne()
  605. {
  606. $id = '54203e08d51d4a1f868b456e';
  607. $collection = $this->getCollection();
  608. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  609. $collection->insert($document);
  610. $collection->remove(['_id' => new \MongoId($id)]);
  611. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  612. $this->assertSame(0, $newCollection->count());
  613. }
  614. public function testSaveUpdate()
  615. {
  616. $expected = [
  617. 'ok' => 1.0,
  618. 'nModified' => 1,
  619. 'n' => 1,
  620. 'err' => null,
  621. 'errmsg' => null,
  622. 'updatedExisting' => true,
  623. ];
  624. $id = '54203e08d51d4a1f868b456e';
  625. $collection = $this->getCollection();
  626. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  627. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  628. $collection->insert($insertDocument);
  629. $this->assertSame($expected, $collection->save($saveDocument));
  630. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  631. $this->assertSame(1, $newCollection->count());
  632. $object = $newCollection->findOne();
  633. $this->assertNotNull($object);
  634. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  635. $this->assertSame($id, (string) $object->_id);
  636. $this->assertObjectHasAttribute('foo', $object);
  637. $this->assertAttributeSame('foo', 'foo', $object);
  638. }
  639. public function testSavingShouldReplaceTheWholeDocument() {
  640. $id = '54203e08d51d4a1f868b456e';
  641. $collection = $this->getCollection();
  642. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  643. $saveDocument = ['_id' => new \MongoId($id)];
  644. $collection->insert($insertDocument);
  645. $collection->save($saveDocument);
  646. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  647. $this->assertSame(1, $newCollection->count());
  648. $object = $newCollection->findOne();
  649. $this->assertNotNull($object);
  650. $this->assertObjectNotHasAttribute('foo', $object);
  651. }
  652. public function testSaveDuplicate()
  653. {
  654. $collection = $this->getCollection();
  655. $collection->createIndex(['foo' => 1], ['unique' => true]);
  656. $document = ['foo' => 'bar'];
  657. $collection->save($document);
  658. $this->setExpectedException('MongoDuplicateKeyException');
  659. unset($document['_id']);
  660. $collection->save($document);
  661. }
  662. public function testSaveEmptyKeys()
  663. {
  664. $document = [];
  665. $this->getCollection()->save($document);
  666. $this->assertSame(1, $this->getCollection()->count());
  667. }
  668. public function testSaveEmptyObject()
  669. {
  670. $document = (object) [];
  671. $this->getCollection()->save($document);
  672. $this->assertSame(1, $this->getCollection()->count());
  673. }
  674. public function testGetDBRef()
  675. {
  676. $collection = $this->getCollection();
  677. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  678. $collection->insert($insertDocument);
  679. $document = $collection->getDBRef([
  680. '$ref' => 'test',
  681. '$id' => 1,
  682. ]);
  683. $this->assertEquals($insertDocument, $document);
  684. }
  685. public function testCreateDBRef()
  686. {
  687. $collection = $this->getCollection();
  688. $reference = $collection->createDBRef(['_id' => 'foo']);
  689. $this->assertSame(
  690. [
  691. '$ref' => 'test',
  692. '$id' => 'foo',
  693. ],
  694. $reference
  695. );
  696. }
  697. public function testCreateIndex()
  698. {
  699. $expected = [
  700. 'createdCollectionAutomatically' => true,
  701. 'numIndexesBefore' => 1,
  702. 'numIndexesAfter' => 2,
  703. 'ok' => 1.0,
  704. ];
  705. $collection = $this->getCollection();
  706. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  707. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  708. $iterator = $newCollection->listIndexes();
  709. $indexes = iterator_to_array($iterator);
  710. $this->assertCount(2, $indexes);
  711. $index = $indexes[1];
  712. $this->assertSame(['foo' => 1], $index->getKey());
  713. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  714. }
  715. public function testCreateIndexInvalid()
  716. {
  717. $this->setExpectedException('MongoException', 'index specification has no elements');
  718. $this->getCollection()->createIndex([]);
  719. }
  720. public function testCreateIndexTwice()
  721. {
  722. $this->getCollection()->createIndex(['foo' => 1]);
  723. $expected = [
  724. 'createdCollectionAutomatically' => false,
  725. 'numIndexesBefore' => 2,
  726. 'numIndexesAfter' => 2,
  727. 'note' => 'all indexes already exist',
  728. 'ok' => 1.0
  729. ];
  730. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  731. }
  732. public function testCreateIndexWithDeprecatedOptions()
  733. {
  734. $this->getCollection()->createIndex(['foo' => 1], ['w' => 1]);
  735. $expected = [
  736. 'createdCollectionAutomatically' => false,
  737. 'numIndexesBefore' => 2,
  738. 'numIndexesAfter' => 2,
  739. 'note' => 'all indexes already exist',
  740. 'ok' => 1.0
  741. ];
  742. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  743. }
  744. public function testCreateIndexTwiceWithSameName()
  745. {
  746. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  747. $expected = [
  748. 'createdCollectionAutomatically' => false,
  749. 'numIndexesBefore' => 2,
  750. 'numIndexesAfter' => 2,
  751. 'note' => 'all indexes already exist',
  752. 'ok' => 1.0
  753. ];
  754. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']));
  755. }
  756. public function testCreateIndexTwiceWithDifferentName()
  757. {
  758. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  759. $expected = [
  760. 'createdCollectionAutomatically' => false,
  761. 'numIndexesBefore' => 2,
  762. 'numIndexesAfter' => 2,
  763. 'note' => 'all indexes already exist',
  764. 'ok' => 1.0
  765. ];
  766. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'index_test']));
  767. }
  768. public function testCreateIndexTwiceWithDifferentOrder()
  769. {
  770. $this->getCollection()->createIndex(['foo' => 1, 'bar' => 1]);
  771. $expected = [
  772. 'createdCollectionAutomatically' => false,
  773. 'numIndexesBefore' => 2,
  774. 'numIndexesAfter' => 3,
  775. 'ok' => 1.0
  776. ];
  777. $this->assertSame($expected, $this->getCollection()->createIndex(['bar' => 1, 'foo' => 1]));
  778. }
  779. public function testCreateIndexesWithDifferentOptions()
  780. {
  781. $this->setExpectedException('MongoResultException');
  782. $this->getCollection()->createIndex(['foo' => 1]);
  783. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  784. }
  785. /**
  786. * @dataProvider createIndexIgnoredOptions
  787. */
  788. public function testCreateIndexesWithIgnoredOptions($option)
  789. {
  790. $this->getCollection()->createIndex(['foo' => 1]);
  791. $expected = [
  792. 'createdCollectionAutomatically' => false,
  793. 'numIndexesBefore' => 2,
  794. 'numIndexesAfter' => 2,
  795. 'note' => 'all indexes already exist',
  796. 'ok' => 1.0
  797. ];
  798. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], [$option => true]));
  799. }
  800. public static function createIndexIgnoredOptions()
  801. {
  802. return [
  803. 'background' => ['background'],
  804. 'dropDups' => ['dropDups'],
  805. ];
  806. }
  807. public function testCreateIndexWithSameNameAndDifferentOptions()
  808. {
  809. $this->setExpectedException('MongoResultException');
  810. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  811. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  812. }
  813. public function testEnsureIndex()
  814. {
  815. $expected = [
  816. 'createdCollectionAutomatically' => true,
  817. 'numIndexesBefore' => 1,
  818. 'numIndexesAfter' => 2,
  819. 'ok' => 1.0
  820. ];
  821. $collection = $this->getCollection();
  822. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  823. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  824. $indexes = iterator_to_array($newCollection->listIndexes());
  825. $this->assertCount(2, $indexes);
  826. $index = $indexes[1];
  827. $this->assertSame(['bar' => 1], $index->getKey());
  828. $this->assertTrue($index->isUnique());
  829. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  830. }
  831. public function testEnsureIndexAlreadyExists()
  832. {
  833. $collection = $this->getCollection();
  834. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  835. $expected = [
  836. 'createdCollectionAutomatically' => false,
  837. 'numIndexesBefore' => 2,
  838. 'numIndexesAfter' => 2,
  839. 'ok' => 1.0,
  840. 'note' => 'all indexes already exist',
  841. ];
  842. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  843. }
  844. public function testEnsureIndexAlreadyExistsWithDifferentOptions()
  845. {
  846. $collection = $this->getCollection();
  847. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  848. $this->setExpectedException('MongoResultException', 'Index with name: bar_1 already exists with different options');
  849. $collection->ensureIndex(['bar' => 1]);
  850. }
  851. public function testDeleteIndexUsingIndexName()
  852. {
  853. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  854. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  855. $expected = [
  856. 'nIndexesWas' => 2,
  857. 'errmsg' => 'index not found with name [bar_1]',
  858. 'ok' => 0.0,
  859. 'code' => 27,
  860. ];
  861. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  862. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  863. }
  864. public function testDeleteIndexUsingField()
  865. {
  866. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  867. $newCollection->createIndex(['bar' => 1]);
  868. $expected = [
  869. 'nIndexesWas' => 2,
  870. 'ok' => 1.0,
  871. ];
  872. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  873. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  874. }
  875. public function testDeleteIndexUsingKeys()
  876. {
  877. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  878. $newCollection->createIndex(['bar' => 1]);
  879. $expected = [
  880. 'nIndexesWas' => 2,
  881. 'ok' => 1.0,
  882. ];
  883. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  884. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  885. }
  886. public function testDeleteIndexes()
  887. {
  888. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  889. $newCollection->createIndex(['bar' => 1]);
  890. $expected = [
  891. 'nIndexesWas' => 2,
  892. 'msg' => 'non-_id indexes dropped for collection',
  893. 'ok' => 1.0,
  894. ];
  895. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  896. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  897. }
  898. public function testDeleteIndexesForNonExistingCollection()
  899. {
  900. $expected = [
  901. 'ok' => 0.0,
  902. 'errmsg' => 'ns not found',
  903. 'code' => 26,
  904. ];
  905. $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
  906. }
  907. public function testGetIndexInfo()
  908. {
  909. $collection = $this->getCollection();
  910. $collection->createIndex(['foo' => 1]);
  911. $expected = [
  912. [
  913. 'v' => 1,
  914. 'key' => ['_id' => 1],
  915. 'name' => '_id_',
  916. 'ns' => 'mongo-php-adapter.test',
  917. ],
  918. [
  919. 'v' => 1,
  920. 'key' => ['foo' => 1],
  921. 'name' => 'foo_1',
  922. 'ns' => 'mongo-php-adapter.test',
  923. ],
  924. ];
  925. $this->assertSame(
  926. $expected,
  927. $collection->getIndexInfo()
  928. );
  929. }
  930. public function testFindAndModifyUpdate()
  931. {
  932. $id = '54203e08d51d4a1f868b456e';
  933. $collection = $this->getCollection();
  934. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  935. $collection->insert($document);
  936. $document = $collection->findAndModify(
  937. ['_id' => new \MongoId($id)],
  938. ['$set' => ['foo' => 'foo']]
  939. );
  940. $this->assertSame('bar', $document['foo']);
  941. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  942. $this->assertSame(1, $newCollection->count());
  943. $object = $newCollection->findOne();
  944. $this->assertNotNull($object);
  945. $this->assertAttributeSame('foo', 'foo', $object);
  946. }
  947. public function testFindAndModifyUpdateWithUpdateOptions()
  948. {
  949. $id = '54203e08d51d4a1f868b456e';
  950. $collection = $this->getCollection();
  951. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  952. $collection->insert($document);
  953. $document = $collection->findAndModify(
  954. ['_id' => new \MongoId($id)],
  955. [],
  956. [],
  957. [
  958. 'update' => ['bar' => 'foo']
  959. ]
  960. );
  961. $this->assertSame('bar', $document['foo']);
  962. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  963. $this->assertSame(1, $newCollection->count());
  964. $object = $newCollection->findOne();
  965. $this->assertNotNull($object);
  966. $this->assertAttributeSame('foo', 'bar', $object);
  967. $this->assertObjectNotHasAttribute('foo', $object);
  968. }
  969. public function testFindAndModifyUpdateReplace()
  970. {
  971. $id = '54203e08d51d4a1f868b456e';
  972. $collection = $this->getCollection();
  973. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  974. $collection->insert($document);
  975. $document = $collection->findAndModify(
  976. ['_id' => new \MongoId($id)],
  977. ['_id' => new \MongoId($id), 'foo' => 'boo']
  978. );
  979. $this->assertSame('bar', $document['foo']);
  980. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  981. $this->assertSame(1, $newCollection->count());
  982. $object = $newCollection->findOne();
  983. $this->assertNotNull($object);
  984. $this->assertAttributeSame('boo', 'foo', $object);
  985. $this->assertObjectNotHasAttribute('bar', $object);
  986. }
  987. public function testFindAndModifyUpdateReturnNew()
  988. {
  989. $id = '54203e08d51d4a1f868b456e';
  990. $collection = $this->getCollection();
  991. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  992. $collection->insert($document);
  993. $document = $collection->findAndModify(
  994. ['_id' => new \MongoId($id)],
  995. ['$set' => ['foo' => 'foo']],
  996. null,
  997. ['new' => true]
  998. );
  999. $this->assertSame('foo', $document['foo']);
  1000. }
  1001. public function testFindAndModifyWithFields()
  1002. {
  1003. $id = '54203e08d51d4a1f868b456e';
  1004. $collection = $this->getCollection();
  1005. $document = [
  1006. '_id' => new \MongoId($id),
  1007. 'foo' => 'bar',
  1008. 'bar' => 'foo',
  1009. ];
  1010. $collection->insert($document);
  1011. $document = $collection->findAndModify(
  1012. ['_id' => new \MongoId($id)],
  1013. ['$set' => ['foo' => 'foo']],
  1014. ['foo' => true]
  1015. );
  1016. $this->assertArrayNotHasKey('bar', $document);
  1017. $this->assertArrayHasKey('foo', $document);
  1018. }
  1019. public function testGroup()
  1020. {
  1021. $collection = $this->getCollection();
  1022. $document1 = ['a' => 2];
  1023. $collection->insert($document1);
  1024. $document2 = ['b' => 5];
  1025. $collection->insert($document2);
  1026. $document3 = ['a' => 1];
  1027. $collection->insert($document3);
  1028. $keys = [];
  1029. $initial = ["count" => 0];
  1030. $reduce = "function (obj, prev) { prev.count++; }";
  1031. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  1032. $result = $collection->group($keys, $initial, $reduce, $condition);
  1033. $this->assertArraySubset(
  1034. [
  1035. 'retval' => [['count' => 1.0]],
  1036. 'count' => 1.0,
  1037. 'keys' => 1,
  1038. 'ok' => 1.0,
  1039. ],
  1040. $result
  1041. );
  1042. }
  1043. public function testMapReduce()
  1044. {
  1045. $data = array(
  1046. array(
  1047. 'username' => 'jones',
  1048. 'likes' => 20.0,
  1049. 'text' => 'Hello world!'
  1050. ),
  1051. array(
  1052. 'username' => 'bob',
  1053. 'likes' => 100.0,
  1054. 'text' => 'Hello world!'
  1055. ),
  1056. array(
  1057. 'username' => 'bob',
  1058. 'likes' => 100.0,
  1059. 'text' => 'Hello world!'
  1060. ),
  1061. );
  1062. $collection = $this->getCollection();
  1063. $collection->batchInsert($data);
  1064. $map = 'function() {
  1065. emit(this.username, { count: 1, likes: this.likes });
  1066. }';
  1067. $reduce = 'function(key, values) {
  1068. var result = {count: 0, likes: 0};
  1069. values.forEach(function(value) {
  1070. result.count += value.count;
  1071. result.likes += value.likes;
  1072. });
  1073. return result;
  1074. }';
  1075. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  1076. $command = [
  1077. 'mapreduce' => $this->getCollection()->getName(),
  1078. 'map' => new \MongoCode($map),
  1079. 'reduce' => new \MongoCode($reduce),
  1080. 'query' => (object) [],
  1081. 'out' => ['inline' => true],
  1082. 'finalize' => new \MongoCode($finalize),
  1083. ];
  1084. $result = $this->getDatabase()->command($command);
  1085. $expected = [
  1086. [
  1087. '_id' => 'bob',
  1088. 'value' => [
  1089. 'count' => 2.0,
  1090. 'likes' => 200.0,
  1091. 'test' => 'test',
  1092. ],
  1093. ],
  1094. [
  1095. '_id' => 'jones',
  1096. 'value' => [
  1097. 'count' => 1.0,
  1098. 'likes' => 20.0,
  1099. 'test' => 'test',
  1100. ],
  1101. ],
  1102. ];
  1103. $this->assertSame(1.0, $result['ok']);
  1104. $this->assertSame($expected, $result['results']);
  1105. }
  1106. public function testFindAndModifyResultException()
  1107. {
  1108. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  1109. $collection = $this->getCollection();
  1110. $this->setExpectedException('MongoResultException');
  1111. $collection->findAndModify(
  1112. array("inprogress" => false, "name" => "Next promo"),
  1113. array('$unsupportedOperator' => array("tasks" => -1)),
  1114. array("tasks" => true),
  1115. array("new" => true)
  1116. );
  1117. }
  1118. public function testFindAndModifyExceptionTimeout()
  1119. {
  1120. $this->failMaxTimeMS();
  1121. $id = '54203e08d51d4a1f868b456e';
  1122. $collection = $this->getCollection();
  1123. $this->setExpectedException('MongoExecutionTimeoutException');
  1124. $document = $collection->findAndModify(
  1125. ['_id' => new \MongoId($id)],
  1126. null,
  1127. null,
  1128. ['maxTimeMS' => 1, 'remove' => true]
  1129. );
  1130. }
  1131. public function testFindAndModifyRemove()
  1132. {
  1133. $id = '54203e08d51d4a1f868b456e';
  1134. $collection = $this->getCollection();
  1135. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1136. $collection->insert($document);
  1137. $document = $collection->findAndModify(
  1138. ['_id' => new \MongoId($id)],
  1139. null,
  1140. null,
  1141. ['remove' => true]
  1142. );
  1143. $this->assertEquals('bar', $document['foo']);
  1144. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1145. $this->assertSame(0, $newCollection->count());
  1146. }
  1147. public function testValidate()
  1148. {
  1149. $collection = $this->getCollection();
  1150. $document = ['foo' => 'bar'];
  1151. $collection->insert($document);
  1152. $result = $collection->validate();
  1153. $this->assertArraySubset(
  1154. [
  1155. 'ns' => 'mongo-php-adapter.test',
  1156. 'nrecords' => 1,
  1157. 'nIndexes' => 1,
  1158. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  1159. 'valid' => true,
  1160. 'errors' => [],
  1161. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  1162. 'ok' => 1.0
  1163. ],
  1164. $result
  1165. );
  1166. }
  1167. public function testDrop()
  1168. {
  1169. $document = ['foo' => 'bar'];
  1170. $this->getCollection()->insert($document);
  1171. $expected = [
  1172. 'ns' => (string) $this->getCollection(),
  1173. 'nIndexesWas' => 1,
  1174. 'ok' => 1.0
  1175. ];
  1176. $this->assertSame($expected, $this->getCollection()->drop());
  1177. }
  1178. public function testEmptyCollectionName()
  1179. {
  1180. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  1181. new \MongoCollection($this->getDatabase(), '');
  1182. }
  1183. public function testSelectCollectionWithNullBytes()
  1184. {
  1185. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  1186. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1187. }
  1188. public function testSubCollectionWithNullBytes()
  1189. {
  1190. $collection = $this->getCollection();
  1191. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1192. $this->assertSame('test', $collection->getName());
  1193. }
  1194. public function testSelectCollectionWithDatabaseObject()
  1195. {
  1196. $client = $this->getClient();
  1197. $database = $this->getDatabase($client);
  1198. $collection = $client->selectCollection($database, 'test');
  1199. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  1200. }
  1201. public function testHasNextLoop()
  1202. {
  1203. $collection = $this->getCollection();
  1204. for ($i = 0; $i < 5; $i++) {
  1205. $document = ['i' => $i];
  1206. $collection->insert($document);
  1207. }
  1208. $cursor = $collection->find()->sort(['i' => 1]);
  1209. $data = [];
  1210. $i = 0;
  1211. while ($cursor->hasNext()) {
  1212. $this->assertSame($i < 5, $cursor->hasNext());
  1213. $row = $cursor->getNext();
  1214. $this->assertSame($i, $row['i']);
  1215. $data[] = $row;
  1216. $i++;
  1217. }
  1218. $this->assertCount(5, $data);
  1219. }
  1220. public function testProjectionWithBSONTypes()
  1221. {
  1222. $collection = $this->getCollection();
  1223. $id = new \MongoId();
  1224. $referencedId = new \MongoId();
  1225. $data = [
  1226. '_id' => $id,
  1227. 'loveItems' => [
  1228. [
  1229. 'sellable' => [
  1230. '$ref' => 'sellables',
  1231. '$id' => $referencedId,
  1232. ]
  1233. ],
  1234. [
  1235. 'sellable' => [
  1236. '$ref' => 'sellables',
  1237. '$id' => new \MongoId(),
  1238. ]
  1239. ]
  1240. ]
  1241. ];
  1242. $collection->insert($data);
  1243. $item = $collection->findOne(
  1244. ['_id' => $id],
  1245. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1246. );
  1247. $this->assertArrayHasKey('loveItems', $item);
  1248. $this->assertCount(1, $item['loveItems']);
  1249. $cursor = $collection->find(
  1250. ['_id' => $id],
  1251. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1252. );
  1253. $items = iterator_to_array($cursor, false);
  1254. $this->assertCount(1, $items);
  1255. $this->assertCount(1, $items[0]['loveItems']);
  1256. }
  1257. }
  1258. class PrivatePropertiesStub
  1259. {
  1260. private $foo = 'bar';
  1261. }