MongoCollectionTest.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use ArrayObject;
  4. use MongoDB\BSON\Regex;
  5. use MongoDB\Driver\ReadPreference;
  6. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  7. use MongoId;
  8. use PHPUnit\Framework\Error\Warning;
  9. /**
  10. * @author alcaeus <alcaeus@alcaeus.org>
  11. */
  12. class MongoCollectionTest extends TestCase
  13. {
  14. public function testSerialize()
  15. {
  16. $this->assertInternalType('string', serialize($this->getCollection()));
  17. }
  18. public function testGetNestedCollections()
  19. {
  20. $collection = $this->getCollection()->foo->bar;
  21. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  22. }
  23. public function testCreateRecord()
  24. {
  25. $collection = $this->getCollection();
  26. $expected = [
  27. 'ok' => 1.0,
  28. 'n' => 0,
  29. 'err' => null,
  30. 'errmsg' => null,
  31. ];
  32. $document = ['foo' => 'bar'];
  33. $this->assertEquals($expected, $collection->insert($document));
  34. $this->assertInstanceOf('MongoId', $document['_id']);
  35. $id = (string) $document['_id'];
  36. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  37. $this->assertSame(1, $newCollection->count());
  38. $object = $newCollection->findOne();
  39. $this->assertNotNull($object);
  40. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  41. $this->assertSame($id, (string) $object->_id);
  42. $this->assertObjectHasAttribute('foo', $object);
  43. $this->assertAttributeSame('bar', 'foo', $object);
  44. }
  45. public function testInsertInvalidData()
  46. {
  47. // Dirty hack to support both PHPUnit 5.x and 6.x
  48. $className = class_exists(Warning::class) ? Warning::class : \PHPUnit_Framework_Error_Warning::class;
  49. $this->expectException($className);
  50. $this->expectExceptionMessage('MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
  51. $document = 8;
  52. $this->getCollection()->insert($document);
  53. }
  54. public function testInsertEmptyArray()
  55. {
  56. $document = [];
  57. $this->getCollection()->insert($document);
  58. $this->assertSame(1, $this->getCollection()->count());
  59. }
  60. public function testInsertArrayWithNumericKeys()
  61. {
  62. $document = [1 => 'foo'];
  63. $this->getCollection()->insert($document);
  64. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  65. }
  66. /**
  67. * @dataProvider emptyIdProvider
  68. */
  69. public function testInsertArrayWithEmptyIds($id)
  70. {
  71. $document = ['_id' => $id];
  72. $this->getCollection()->insert($document);
  73. $this->assertSame(1, $this->getCollection()->count(['_id' => $id]));
  74. }
  75. public function emptyIdProvider()
  76. {
  77. return [
  78. 'Zero as string' => ['0'],
  79. 'Zero as int' => [0],
  80. 'Empty string' => [''],
  81. ];
  82. }
  83. public function testInsertArrayWithEmptyId()
  84. {
  85. $document = ['_id' => ''];
  86. $this->getCollection()->insert($document);
  87. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  88. }
  89. public function testInsertEmptyObject()
  90. {
  91. $document = (object) [];
  92. $this->getCollection()->insert($document);
  93. $this->assertSame(1, $this->getCollection()->count());
  94. }
  95. public function testInsertObjectWithPrivateProperties()
  96. {
  97. $this->expectException(\MongoException::class);
  98. $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
  99. $document = new PrivatePropertiesStub();
  100. $this->getCollection()->insert($document);
  101. }
  102. public function testInsertArrayObjectWithProtectedProperties()
  103. {
  104. $document = new ArrayObjectWithProtectedProperties(['foo' => 'bar']);
  105. $this->getCollection()->insert($document);
  106. $this->assertInstanceOf('MongoId', $document['_id']);
  107. $this->assertEquals(['_id' => $document['_id'], 'foo' => 'bar'], $this->getCollection()->findOne(['_id' => $document['_id']]));
  108. }
  109. public function testInsertWithInvalidKey()
  110. {
  111. $document = ['*' => 'foo'];
  112. $this->getCollection()->insert($document);
  113. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  114. }
  115. public function getDocumentsWithEmptyKey()
  116. {
  117. return [
  118. 'array' => [['' => 'foo']],
  119. 'object' => [(object) ['' => 'foo']],
  120. ];
  121. }
  122. /**
  123. * @dataProvider getDocumentsWithEmptyKey
  124. */
  125. public function testInsertWithEmptyKey($document)
  126. {
  127. $this->expectException(\MongoException::class);
  128. $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
  129. $this->getCollection()->insert($document);
  130. }
  131. public function testInsertWithNumericKey()
  132. {
  133. $document = ['foo'];
  134. $this->getCollection()->insert($document);
  135. $this->assertSame(1, $this->getCollection()->count(['foo']));
  136. }
  137. public function testInsertWithAlphaNumericKey()
  138. {
  139. /**
  140. * Force the array to store the key as a string "0".
  141. * Initialising like ['0' => 'foo'] casts the string to an int.
  142. */
  143. $document = new \stdClass();
  144. $document->{'0'} = 'foo';
  145. $document = (array) $document;
  146. $this->getCollection()->insert($document);
  147. $this->assertSame(1, $this->getCollection()->count(['0' => 'foo']));
  148. }
  149. public function testInsertDuplicate()
  150. {
  151. $collection = $this->getCollection();
  152. $collection->createIndex(['foo' => 1], ['unique' => true]);
  153. $document = ['foo' => 'bar'];
  154. $collection->insert($document);
  155. unset($document['_id']);
  156. $this->expectException(\MongoDuplicateKeyException::class);
  157. $this->expectExceptionMessageRegExp('/E11000 duplicate key error .* mongo-php-adapter\.test/');
  158. $this->expectExceptionCode(11000);
  159. $collection->insert($document);
  160. }
  161. public function testUnacknowledgedWrite()
  162. {
  163. $document = ['foo' => 'bar'];
  164. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  165. }
  166. public function testUnacknowledgedWriteWithBooleanValue()
  167. {
  168. $document = ['foo' => 'bar'];
  169. $this->assertTrue($this->getCollection()->insert($document, ['w' => false]));
  170. }
  171. public function testAcknowledgedWriteConcernWithBool()
  172. {
  173. $document = ['foo' => 'bar'];
  174. $this->assertEquals(
  175. [
  176. 'ok' => 1.0,
  177. 'n' => 0,
  178. 'err' => null,
  179. 'errmsg' => null,
  180. ],
  181. $this->getCollection()->insert($document, ['w' => true])
  182. );
  183. }
  184. public function testInsertWriteConcernException()
  185. {
  186. $this->expectException(\MongoWriteConcernException::class);
  187. $this->expectExceptionMessage("cannot use 'w' > 1 when a host is not replicated");
  188. $document = ['foo' => 'bar'];
  189. $this->getCollection()->insert($document, ['w' => 2]);
  190. }
  191. public function testInsertMany()
  192. {
  193. $expected = [
  194. 'ok' => 1.0,
  195. 'n' => 0,
  196. 'syncMillis' => 0,
  197. 'writtenTo' => null,
  198. 'err' => null,
  199. ];
  200. $documents = [
  201. ['foo' => 'bar'],
  202. ['bar' => 'foo']
  203. ];
  204. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  205. foreach ($documents as $document) {
  206. $this->assertInstanceOf('MongoId', $document['_id']);
  207. }
  208. }
  209. public function testInsertManyWithNonNumericKeys()
  210. {
  211. $expected = [
  212. 'ok' => 1.0,
  213. 'n' => 0,
  214. 'syncMillis' => 0,
  215. 'writtenTo' => null,
  216. 'err' => null,
  217. ];
  218. $documents = [
  219. 'a' => ['foo' => 'bar'],
  220. 'b' => ['bar' => 'foo']
  221. ];
  222. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  223. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  224. $this->assertSame(2, $newCollection->count());
  225. }
  226. public function testBatchInsertContinuesOnError()
  227. {
  228. $expected = [
  229. 'ok' => 1.0,
  230. 'n' => 0,
  231. 'syncMillis' => 0,
  232. 'writtenTo' => null,
  233. 'err' => null,
  234. ];
  235. $documents = [
  236. 8,
  237. 'b' => ['bar' => 'foo']
  238. ];
  239. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  240. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  241. $this->assertSame(1, $newCollection->count());
  242. }
  243. public function testBatchInsertException()
  244. {
  245. $id = new \MongoId();
  246. $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
  247. $this->expectException(\MongoDuplicateKeyException::class);
  248. $this->expectExceptionMessageRegExp('/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
  249. $this->expectExceptionCode(11000);
  250. $this->getCollection()->batchInsert($documents);
  251. }
  252. public function testBatchInsertObjectWithPrivateProperties()
  253. {
  254. $this->expectException(\MongoException::class);
  255. $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
  256. $documents = [new PrivatePropertiesStub()];
  257. $this->getCollection()->batchInsert($documents);
  258. }
  259. public function testBatchInsertWithInvalidKey()
  260. {
  261. $documents = [['*' => 'foo']];
  262. $this->getCollection()->batchInsert($documents);
  263. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  264. }
  265. /**
  266. * @dataProvider getDocumentsWithEmptyKey
  267. */
  268. public function testBatchInsertWithEmptyKey($document)
  269. {
  270. $this->expectException(\MongoException::class);
  271. $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
  272. $documents = [$document];
  273. $this->getCollection()->batchInsert($documents);
  274. }
  275. public function testBatchInsertWithNumericKey()
  276. {
  277. $documents = [['foo']];
  278. $this->getCollection()->batchInsert($documents);
  279. $this->assertSame(1, $this->getCollection()->count(['foo']));
  280. }
  281. public function testBatchInsertEmptyBatchException()
  282. {
  283. $this->expectException(\MongoException::class);
  284. $this->expectExceptionMessage('No write ops were included in the batch');
  285. $documents = [];
  286. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  287. }
  288. public function testUpdateWriteConcern()
  289. {
  290. $this->expectException(\MongoWriteConcernException::class);
  291. $this->expectExceptionMessage("cannot use 'w' > 1 when a host is not replicated");
  292. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  293. }
  294. public function testUpdateOne()
  295. {
  296. $document = ['foo' => 'bar'];
  297. $this->getCollection()->insert($document);
  298. // Unset ID to re-insert
  299. unset($document['_id']);
  300. $this->getCollection()->insert($document);
  301. $expected = [
  302. 'ok' => 1.0,
  303. 'nModified' => 1,
  304. 'n' => 1,
  305. 'err' => null,
  306. 'errmsg' => null,
  307. 'updatedExisting' => true,
  308. ];
  309. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  310. $this->assertEquals($expected, $result);
  311. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  312. }
  313. public function testUpdateReplaceOne()
  314. {
  315. $document = ['foo' => 'bar', 'bar' => 'foo'];
  316. $this->getCollection()->insert($document);
  317. // Unset ID to re-insert
  318. unset($document['_id']);
  319. $this->getCollection()->insert($document);
  320. $expected = [
  321. 'ok' => 1.0,
  322. 'nModified' => 1,
  323. 'n' => 1,
  324. 'err' => null,
  325. 'errmsg' => null,
  326. 'updatedExisting' => true,
  327. ];
  328. $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
  329. $this->assertEquals($expected, $result);
  330. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  331. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
  332. }
  333. public function testUpdateReplaceMultiple()
  334. {
  335. $this->expectException(\MongoWriteConcernException::class);
  336. $this->expectExceptionMessageRegExp('/multi update only works with \$ operators/', 9);
  337. $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
  338. }
  339. public function testUpdateDuplicate()
  340. {
  341. $collection = $this->getCollection();
  342. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  343. $document = ['foo' => 'bar'];
  344. $collection->insert($document);
  345. $document = ['foo' => 'foo'];
  346. $collection->insert($document);
  347. $this->expectException(\MongoDuplicateKeyException::class);
  348. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  349. }
  350. public function testUpdateMany()
  351. {
  352. $document = ['change' => true, 'foo' => 'bar'];
  353. $this->getCollection()->insert($document);
  354. unset($document['_id']);
  355. $this->getCollection()->insert($document);
  356. $document = ['change' => true, 'foo' => 'foo'];
  357. $this->getCollection()->insert($document);
  358. $expected = [
  359. 'ok' => 1.0,
  360. 'nModified' => 2,
  361. 'n' => 3,
  362. 'err' => null,
  363. 'errmsg' => null,
  364. 'updatedExisting' => true,
  365. ];
  366. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  367. $this->assertEquals($expected, $result);
  368. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  369. }
  370. public function testUpdateWhichDoesntMatchQuery()
  371. {
  372. $document = ['foo' => 'bar'];
  373. $this->getCollection()->insert($document);
  374. $expected = [
  375. 'ok' => 1.0,
  376. 'nModified' => 0,
  377. 'n' => 0,
  378. 'err' => null,
  379. 'errmsg' => null,
  380. 'updatedExisting' => false,
  381. ];
  382. $result = $this->getCollection()->update(['foo' => 'bar22'], ['$set' => ['foo' => 'foo']]);
  383. $this->assertEquals($expected, $result);
  384. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'bar']));
  385. }
  386. public function testUnacknowledgedUpdate()
  387. {
  388. $document = ['foo' => 'bar'];
  389. $this->getCollection()->insert($document);
  390. unset($document['_id']);
  391. $this->getCollection()->insert($document);
  392. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  393. }
  394. public function testUpdateWithInvalidKey()
  395. {
  396. $document = ['foo' => 'bar'];
  397. $this->getCollection()->insert($document);
  398. $update_document = ['*' => 'foo'];
  399. $this->getCollection()->update($document, $update_document);
  400. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  401. }
  402. /**
  403. * @dataProvider getDocumentsWithEmptyKey
  404. */
  405. public function testUpdateWithEmptyKey($updateDocument)
  406. {
  407. $document = ['foo' => 'bar'];
  408. $this->getCollection()->insert($document);
  409. $this->expectException(\MongoException::class);
  410. $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
  411. $this->getCollection()->update($document, $updateDocument);
  412. }
  413. /**
  414. * @dataProvider getDocumentsWithEmptyKey
  415. */
  416. public function testAtomicUpdateWithEmptyKey($updateDocument)
  417. {
  418. $document = ['foo' => 'bar'];
  419. $this->getCollection()->insert($document);
  420. $this->expectException(\MongoException::class);
  421. $this->expectExceptionMessage('zero-length keys are not allowed, did you use $ with double quotes?');
  422. $this->getCollection()->update($document, ['$set' => $updateDocument]);
  423. }
  424. public function testRemoveMultiple()
  425. {
  426. $document = ['change' => true, 'foo' => 'bar'];
  427. $this->getCollection()->insert($document);
  428. unset($document['_id']);
  429. $this->getCollection()->insert($document);
  430. $document = ['change' => true, 'foo' => 'foo'];
  431. $this->getCollection()->insert($document);
  432. $expected = [
  433. 'ok' => 1.0,
  434. 'n' => 2,
  435. 'err' => null,
  436. 'errmsg' => null,
  437. ];
  438. $result = $this->getCollection()->remove(['foo' => 'bar']);
  439. $this->assertEquals($expected, $result);
  440. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  441. }
  442. public function testRemoveSingle()
  443. {
  444. $document = ['change' => true, 'foo' => 'bar'];
  445. $this->getCollection()->insert($document);
  446. unset($document['_id']);
  447. $this->getCollection()->insert($document);
  448. unset($document['_id']);
  449. $this->getCollection()->insert($document);
  450. $expected = [
  451. 'ok' => 1.0,
  452. 'n' => 1,
  453. 'err' => null,
  454. 'errmsg' => null,
  455. ];
  456. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  457. $this->assertEquals($expected, $result);
  458. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  459. }
  460. public function testRemoveUnacknowledged()
  461. {
  462. $document = ['change' => true, 'foo' => 'bar'];
  463. $this->getCollection()->insert($document);
  464. unset($document['_id']);
  465. $this->getCollection()->insert($document);
  466. unset($document['_id']);
  467. $this->getCollection()->insert($document);
  468. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  469. }
  470. public function testFindReturnsCursor()
  471. {
  472. $this->prepareData();
  473. $collection = $this->getCollection();
  474. $this->assertInstanceOf('MongoCursor', $collection->find());
  475. }
  476. /**
  477. * @dataProvider dataFindWithProjection
  478. */
  479. public function testFindWithProjection($projection)
  480. {
  481. $document = ['foo' => 'foo', 'bar' => 'bar'];
  482. $this->getCollection()->insert($document);
  483. unset($document['_id']);
  484. $this->getCollection()->insert($document);
  485. $cursor = $this->getCollection()->find(['foo' => 'foo'], $projection);
  486. foreach ($cursor as $document) {
  487. $this->assertCount(2, $document);
  488. $this->assertArrayHasKey('_id', $document);
  489. $this->assertArraySubset(['bar' => 'bar'], $document);
  490. }
  491. }
  492. public static function dataFindWithProjection()
  493. {
  494. return [
  495. 'projection' => [['bar' => true]],
  496. 'intProjection' => [['bar' => 1]],
  497. 'legacyProjection' => [['bar']],
  498. ];
  499. }
  500. /**
  501. * @dataProvider dataFindWithProjectionAndNumericKeys
  502. */
  503. public function testFindWithProjectionAndNumericKeys($data, $projection, $expected)
  504. {
  505. $this->getCollection()->insert($data);
  506. $document = $this->getCollection()->findOne([], $projection);
  507. unset($document['_id']);
  508. $this->assertSame($expected, $document);
  509. }
  510. public static function dataFindWithProjectionAndNumericKeys()
  511. {
  512. return [
  513. 'sequentialIntegersStartingWithOne' => [
  514. ['0' => 'foo', '1' => 'bar', '2' => 'foobar'],
  515. [1 => true, 2 => true],
  516. ['1' => 'bar', '2' => 'foobar'],
  517. ],
  518. 'nonSequentialIntegers' => [
  519. ['0' => 'foo', '1' => 'bar', '2' => 'foobar', '3' => 'barfoo'],
  520. [1 => true, 3 => true],
  521. ['1' => 'bar', '3' => 'barfoo'],
  522. ]
  523. ];
  524. }
  525. public function testFindWithProjectionAndSequentialNumericKeys()
  526. {
  527. $this->expectException(\MongoException::class);
  528. $this->expectExceptionMessage('field names must be strings', 8);
  529. $this->getCollection()->findOne([], [true, false]);
  530. }
  531. /**
  532. * @dataProvider dataFindWithProjectionExcludeId
  533. */
  534. public function testFindWithProjectionExcludeId($projection)
  535. {
  536. $document = ['foo' => 'foo', 'bar' => 'bar'];
  537. $this->getCollection()->insert($document);
  538. unset($document['_id']);
  539. $this->getCollection()->insert($document);
  540. $cursor = $this->getCollection()->find(['foo' => 'foo'], $projection);
  541. foreach ($cursor as $document) {
  542. $this->assertCount(1, $document);
  543. $this->assertArrayNotHasKey('_id', $document);
  544. $this->assertArraySubset(['bar' => 'bar'], $document);
  545. }
  546. }
  547. public static function dataFindWithProjectionExcludeId()
  548. {
  549. return [
  550. 'projection' => [['_id' => false, 'bar' => true]],
  551. 'intProjection' => [['_id' => 0, 'bar' => 1]],
  552. ];
  553. }
  554. public function testCount()
  555. {
  556. $this->prepareData();
  557. $collection = $this->getCollection();
  558. $this->assertSame(3, $collection->count());
  559. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  560. }
  561. public function testCountWithLimit()
  562. {
  563. $this->prepareData();
  564. $collection = $this->getCollection();
  565. $this->assertSame(2, $collection->count([], ['limit' => 2]));
  566. $this->assertSame(1, $collection->count(['foo' => 'bar'], ['limit' => 1]));
  567. }
  568. public function testCountWithLimitLegacy()
  569. {
  570. $this->prepareData();
  571. $collection = $this->getCollection();
  572. $this->assertSame(2, $collection->count([], 2));
  573. $this->assertSame(1, $collection->count(['foo' => 'bar'], 1));
  574. }
  575. public function testCountWithSkip()
  576. {
  577. $this->prepareData();
  578. $collection = $this->getCollection();
  579. $this->assertSame(2, $collection->count([], ['skip' => 1]));
  580. $this->assertSame(1, $collection->count(['foo' => 'bar'], ['skip' => 1]));
  581. }
  582. public function testCountWithSkipLegacy()
  583. {
  584. $this->prepareData();
  585. $collection = $this->getCollection();
  586. $this->assertSame(2, $collection->count([], null, 1));
  587. $this->assertSame(1, $collection->count(['foo' => 'bar'], null, 1));
  588. }
  589. public function testCountWithLimitAndSkip()
  590. {
  591. $this->prepareData();
  592. $collection = $this->getCollection();
  593. $this->assertSame(2, $collection->count([], ['skip' => 1, 'limit' => 2]));
  594. $this->assertSame(1, $collection->count([], ['skip' => 1, 'limit' => 1]));
  595. }
  596. public function testCountWithLimitAndSkipLegacy()
  597. {
  598. $this->prepareData();
  599. $collection = $this->getCollection();
  600. $this->assertSame(2, $collection->count([], 2, 1));
  601. $this->assertSame(1, $collection->count([], 1, 1));
  602. }
  603. public function testCountTimeout()
  604. {
  605. $this->failMaxTimeMS();
  606. $this->expectException(\MongoExecutionTimeoutException::class);
  607. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  608. }
  609. public function testFindOne()
  610. {
  611. $this->prepareData();
  612. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  613. $this->assertSame(['foo' => 'foo'], $document);
  614. }
  615. public function testFindOneWithProjection()
  616. {
  617. $document = ['foo' => 'foo', 'bar' => 'bar'];
  618. $this->getCollection()->insert($document);
  619. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['bar' => true]);
  620. $this->assertCount(2, $document);
  621. $this->assertArraySubset(['bar' => 'bar'], $document);
  622. }
  623. public function testFindOneWithLegacyProjection()
  624. {
  625. $document = ['foo' => 'foo', 'bar' => 'bar'];
  626. $this->getCollection()->insert($document);
  627. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['bar']);
  628. $this->assertCount(2, $document);
  629. $this->assertArraySubset(['bar' => 'bar'], $document);
  630. }
  631. public function testFindOneNotFound()
  632. {
  633. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  634. $this->assertNull($document);
  635. }
  636. public function testFindOneConnectionIssue()
  637. {
  638. $this->expectException(\MongoConnectionException::class);
  639. $client = $this->getClient([], 'mongodb://localhost:28888/?connectTimeoutMS=1');
  640. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  641. $collection->findOne();
  642. }
  643. public function testDistinct()
  644. {
  645. $this->prepareData();
  646. $values = $this->getCollection()->distinct('foo');
  647. $this->assertInternalType('array', $values);
  648. sort($values);
  649. $this->assertEquals(['bar', 'foo'], $values);
  650. }
  651. public function testDistinctWithQuery()
  652. {
  653. $this->prepareData();
  654. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  655. $this->assertInternalType('array', $values);
  656. $this->assertEquals(['bar'], $values);
  657. }
  658. public function testDistinctWithIdQuery()
  659. {
  660. $document1 = ['foo' => 'bar'];
  661. $document2 = ['foo' => 'bar'];
  662. $document3 = ['foo' => 'foo'];
  663. $collection = $this->getCollection();
  664. $collection->insert($document1);
  665. $collection->insert($document2);
  666. $collection->insert($document3);
  667. $this->assertSame(
  668. ['bar'],
  669. $collection->distinct('foo', ['_id' => [
  670. '$in' => [$document1['_id'], $document2['_id']]
  671. ]])
  672. );
  673. $this->assertEquals(
  674. ['bar', 'foo'],
  675. $collection->distinct('foo', ['_id' => [
  676. '$in' => [$document1['_id'], $document3['_id']]
  677. ]])
  678. );
  679. }
  680. public function testAggregate()
  681. {
  682. $collection = $this->getCollection();
  683. $this->prepareData();
  684. $pipeline = [
  685. [
  686. '$group' => [
  687. '_id' => '$foo',
  688. 'count' => [ '$sum' => 1 ],
  689. ],
  690. ],
  691. [
  692. '$sort' => ['_id' => 1]
  693. ]
  694. ];
  695. $result = $collection->aggregate($pipeline);
  696. $this->assertInternalType('array', $result);
  697. $this->assertArrayHasKey('result', $result);
  698. $this->assertEquals([
  699. ['_id' => 'bar', 'count' => 2],
  700. ['_id' => 'foo', 'count' => 1],
  701. ], $result['result']);
  702. }
  703. public function testAggregateWithMultiplePilelineOperatorsAsArguments()
  704. {
  705. $collection = $this->getCollection();
  706. $this->prepareData();
  707. try {
  708. $result = $collection->aggregate(
  709. [
  710. '$group' => [
  711. '_id' => '$foo',
  712. 'count' => [ '$sum' => 1 ],
  713. ],
  714. ],
  715. [
  716. '$sort' => ['_id' => 1]
  717. ]
  718. );
  719. } catch (\MongoResultException $ex) {
  720. $msg = 'MongoCollection::aggregate ( array $op [, array $op [, array $... ]] ) should accept variable amount of pipeline operators as argument'
  721. . "\n"
  722. . $ex;
  723. $this->fail($msg);
  724. }
  725. $this->assertInternalType('array', $result);
  726. $this->assertArrayHasKey('result', $result);
  727. $this->assertEquals([
  728. ['_id' => 'bar', 'count' => 2],
  729. ['_id' => 'foo', 'count' => 1],
  730. ], $result['result']);
  731. }
  732. public function testAggregateInvalidPipeline()
  733. {
  734. $collection = $this->getCollection();
  735. $pipeline = [
  736. [
  737. '$invalid' => []
  738. ],
  739. ];
  740. $this->expectException(\MongoResultException::class);
  741. $this->expectExceptionMessage('Unrecognized pipeline stage name');
  742. $collection->aggregate($pipeline);
  743. }
  744. public function testAggregateTimeoutException()
  745. {
  746. $collection = $this->getCollection();
  747. $this->failMaxTimeMS();
  748. $this->expectException(\MongoExecutionTimeoutException::class);
  749. $pipeline = [
  750. [
  751. '$group' => [
  752. '_id' => '$foo',
  753. 'count' => [ '$sum' => 1 ],
  754. ],
  755. ],
  756. [
  757. '$sort' => ['_id' => 1]
  758. ]
  759. ];
  760. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  761. }
  762. public function testAggregateCursor()
  763. {
  764. $collection = $this->getCollection();
  765. $this->prepareData();
  766. $pipeline = [
  767. [
  768. '$group' => [
  769. '_id' => '$foo',
  770. 'count' => [ '$sum' => 1 ],
  771. ],
  772. ],
  773. [
  774. '$sort' => ['_id' => 1]
  775. ]
  776. ];
  777. $cursor = $collection->aggregateCursor($pipeline);
  778. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  779. $this->assertEquals([
  780. ['_id' => 'bar', 'count' => 2],
  781. ['_id' => 'foo', 'count' => 1],
  782. ], iterator_to_array($cursor));
  783. }
  784. public function testReadPreference()
  785. {
  786. $collection = $this->getCollection();
  787. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  788. $this->assertFalse($collection->getSlaveOkay());
  789. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  790. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  791. $this->assertTrue($collection->getSlaveOkay());
  792. $this->assertTrue($collection->setSlaveOkay(true));
  793. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  794. $this->assertTrue($collection->setSlaveOkay(false));
  795. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  796. }
  797. public function testReadPreferenceIsSetInDriver()
  798. {
  799. $this->skipTestIf(extension_loaded('mongo'));
  800. $collection = $this->getCollection();
  801. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  802. // Only way to check whether options are passed down is through debugInfo
  803. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  804. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  805. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  806. }
  807. public function testReadPreferenceIsInherited()
  808. {
  809. $database = $this->getDatabase();
  810. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  811. $collection = $database->selectCollection('test');
  812. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  813. }
  814. public function testWriteConcern()
  815. {
  816. $collection = $this->getCollection();
  817. $this->assertTrue($collection->setWriteConcern('majority', 100));
  818. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  819. }
  820. public function testWriteConcernIsSetInDriver()
  821. {
  822. $this->skipTestIf(extension_loaded('mongo'));
  823. $collection = $this->getCollection();
  824. $this->assertTrue($collection->setWriteConcern(2, 100));
  825. // Only way to check whether options are passed down is through debugInfo
  826. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  827. $this->assertSame(2, $writeConcern->getW());
  828. $this->assertSame(100, $writeConcern->getWtimeout());
  829. }
  830. public function testWriteConcernIsInherited()
  831. {
  832. $database = $this->getDatabase();
  833. $database->setWriteConcern('majority', 100);
  834. $collection = $database->selectCollection('test');
  835. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  836. }
  837. public function testSaveInsert()
  838. {
  839. $id = '54203e08d51d4a1f868b456e';
  840. $collection = $this->getCollection();
  841. $objectId = new \MongoId($id);
  842. $expected = [
  843. 'ok' => 1.0,
  844. 'nModified' => 0,
  845. 'n' => 1,
  846. 'err' => null,
  847. 'errmsg' => null,
  848. 'upserted' => $objectId,
  849. 'updatedExisting' => false,
  850. ];
  851. $document = ['_id' => $objectId, 'foo' => 'bar'];
  852. $this->assertEquals($expected, $collection->save($document));
  853. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  854. $this->assertSame(1, $newCollection->count());
  855. $object = $newCollection->findOne();
  856. $this->assertNotNull($object);
  857. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  858. $this->assertSame($id, (string) $object->_id);
  859. $this->assertObjectHasAttribute('foo', $object);
  860. $this->assertAttributeSame('bar', 'foo', $object);
  861. }
  862. public function testRemoveOne()
  863. {
  864. $id = '54203e08d51d4a1f868b456e';
  865. $collection = $this->getCollection();
  866. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  867. $collection->insert($document);
  868. $collection->remove(['_id' => new \MongoId($id)]);
  869. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  870. $this->assertSame(0, $newCollection->count());
  871. }
  872. public function testSaveUpdate()
  873. {
  874. $expected = [
  875. 'ok' => 1.0,
  876. 'nModified' => 1,
  877. 'n' => 1,
  878. 'err' => null,
  879. 'errmsg' => null,
  880. 'updatedExisting' => true,
  881. ];
  882. $id = '54203e08d51d4a1f868b456e';
  883. $collection = $this->getCollection();
  884. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  885. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  886. $collection->insert($insertDocument);
  887. $this->assertEquals($expected, $collection->save($saveDocument));
  888. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  889. $this->assertSame(1, $newCollection->count());
  890. $object = $newCollection->findOne();
  891. $this->assertNotNull($object);
  892. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  893. $this->assertSame($id, (string) $object->_id);
  894. $this->assertObjectHasAttribute('foo', $object);
  895. $this->assertAttributeSame('foo', 'foo', $object);
  896. }
  897. public function testSavingShouldReplaceTheWholeDocument()
  898. {
  899. $id = '54203e08d51d4a1f868b456e';
  900. $collection = $this->getCollection();
  901. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  902. $saveDocument = ['_id' => new \MongoId($id)];
  903. $collection->insert($insertDocument);
  904. $collection->save($saveDocument);
  905. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  906. $this->assertSame(1, $newCollection->count());
  907. $object = $newCollection->findOne();
  908. $this->assertNotNull($object);
  909. $this->assertObjectNotHasAttribute('foo', $object);
  910. }
  911. public function testSaveDuplicate()
  912. {
  913. $collection = $this->getCollection();
  914. $collection->createIndex(['foo' => 1], ['unique' => true]);
  915. $document = ['foo' => 'bar'];
  916. $collection->save($document);
  917. $this->expectException(\MongoDuplicateKeyException::class);
  918. unset($document['_id']);
  919. $collection->save($document);
  920. }
  921. public function testSaveEmptyKeys()
  922. {
  923. $document = [];
  924. $this->getCollection()->save($document);
  925. $this->assertSame(1, $this->getCollection()->count());
  926. }
  927. public function testSaveEmptyObject()
  928. {
  929. $document = (object) [];
  930. $this->getCollection()->save($document);
  931. $this->assertSame(1, $this->getCollection()->count());
  932. }
  933. public function testGetDBRef()
  934. {
  935. $collection = $this->getCollection();
  936. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  937. $collection->insert($insertDocument);
  938. $document = $collection->getDBRef([
  939. '$ref' => 'test',
  940. '$id' => 1,
  941. ]);
  942. $this->assertEquals($insertDocument, $document);
  943. }
  944. public function testCreateDBRef()
  945. {
  946. $collection = $this->getCollection();
  947. $reference = $collection->createDBRef(['_id' => 'foo']);
  948. $this->assertSame(
  949. [
  950. '$ref' => 'test',
  951. '$id' => 'foo',
  952. ],
  953. $reference
  954. );
  955. }
  956. public function testCreateIndex()
  957. {
  958. $expected = [
  959. 'createdCollectionAutomatically' => true,
  960. 'numIndexesBefore' => 1,
  961. 'numIndexesAfter' => 2,
  962. 'ok' => 1.0,
  963. ];
  964. $collection = $this->getCollection();
  965. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  966. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  967. $iterator = $newCollection->listIndexes();
  968. $indexes = iterator_to_array($iterator);
  969. $this->assertCount(2, $indexes);
  970. $index = $indexes[1];
  971. $this->assertSame(['foo' => 1], $index->getKey());
  972. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  973. }
  974. public function testCreateIndexInvalid()
  975. {
  976. $this->expectException(\MongoException::class);
  977. $this->expectExceptionMessage('index specification has no elements');
  978. $this->getCollection()->createIndex([]);
  979. }
  980. public function testCreateIndexTwice()
  981. {
  982. $this->getCollection()->createIndex(['foo' => 1]);
  983. $expected = [
  984. 'createdCollectionAutomatically' => false,
  985. 'numIndexesBefore' => 2,
  986. 'numIndexesAfter' => 2,
  987. 'note' => 'all indexes already exist',
  988. 'ok' => 1.0
  989. ];
  990. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  991. }
  992. public function testCreateIndexWithDeprecatedOptions()
  993. {
  994. $this->getCollection()->createIndex(['foo' => 1], ['w' => 1]);
  995. $expected = [
  996. 'createdCollectionAutomatically' => false,
  997. 'numIndexesBefore' => 2,
  998. 'numIndexesAfter' => 2,
  999. 'note' => 'all indexes already exist',
  1000. 'ok' => 1.0
  1001. ];
  1002. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  1003. }
  1004. public function testCreateIndexTwiceWithSameName()
  1005. {
  1006. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  1007. $expected = [
  1008. 'createdCollectionAutomatically' => false,
  1009. 'numIndexesBefore' => 2,
  1010. 'numIndexesAfter' => 2,
  1011. 'note' => 'all indexes already exist',
  1012. 'ok' => 1.0
  1013. ];
  1014. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']));
  1015. }
  1016. public function testCreateIndexTwiceWithDifferentName()
  1017. {
  1018. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  1019. $expected = [
  1020. 'createdCollectionAutomatically' => false,
  1021. 'numIndexesBefore' => 2,
  1022. 'numIndexesAfter' => 2,
  1023. 'note' => 'all indexes already exist',
  1024. 'ok' => 1.0
  1025. ];
  1026. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'index_test']));
  1027. }
  1028. public function testCreateIndexTwiceWithDifferentOrder()
  1029. {
  1030. $this->getCollection()->createIndex(['foo' => 1, 'bar' => 1]);
  1031. $expected = [
  1032. 'createdCollectionAutomatically' => false,
  1033. 'numIndexesBefore' => 2,
  1034. 'numIndexesAfter' => 3,
  1035. 'ok' => 1.0
  1036. ];
  1037. $this->assertSame($expected, $this->getCollection()->createIndex(['bar' => 1, 'foo' => 1]));
  1038. }
  1039. public function testCreateIndexesWithDifferentOptions()
  1040. {
  1041. $this->expectException(\MongoResultException::class);
  1042. $this->getCollection()->createIndex(['foo' => 1]);
  1043. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  1044. }
  1045. /**
  1046. * @dataProvider createIndexIgnoredOptions
  1047. */
  1048. public function testCreateIndexesWithIgnoredOptions($option)
  1049. {
  1050. $this->getCollection()->createIndex(['foo' => 1]);
  1051. $expected = [
  1052. 'createdCollectionAutomatically' => false,
  1053. 'numIndexesBefore' => 2,
  1054. 'numIndexesAfter' => 2,
  1055. 'note' => 'all indexes already exist',
  1056. 'ok' => 1.0
  1057. ];
  1058. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], [$option => true]));
  1059. }
  1060. public static function createIndexIgnoredOptions()
  1061. {
  1062. return [
  1063. 'background' => ['background'],
  1064. 'dropDups' => ['dropDups'],
  1065. ];
  1066. }
  1067. public function testCreateIndexWithSameNameAndDifferentOptions()
  1068. {
  1069. $this->expectException(\MongoResultException::class);
  1070. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  1071. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  1072. }
  1073. public function testEnsureIndex()
  1074. {
  1075. $expected = [
  1076. 'createdCollectionAutomatically' => true,
  1077. 'numIndexesBefore' => 1,
  1078. 'numIndexesAfter' => 2,
  1079. 'ok' => 1.0
  1080. ];
  1081. $collection = $this->getCollection();
  1082. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  1083. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1084. $indexes = iterator_to_array($newCollection->listIndexes());
  1085. $this->assertCount(2, $indexes);
  1086. $index = $indexes[1];
  1087. $this->assertSame(['bar' => 1], $index->getKey());
  1088. $this->assertTrue($index->isUnique());
  1089. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  1090. }
  1091. public function testEnsureIndexAlreadyExists()
  1092. {
  1093. $collection = $this->getCollection();
  1094. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  1095. $expected = [
  1096. 'createdCollectionAutomatically' => false,
  1097. 'numIndexesBefore' => 2,
  1098. 'numIndexesAfter' => 2,
  1099. 'ok' => 1.0,
  1100. 'note' => 'all indexes already exist',
  1101. ];
  1102. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  1103. }
  1104. public function testEnsureIndexAlreadyExistsWithDifferentOptions()
  1105. {
  1106. $collection = $this->getCollection();
  1107. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  1108. $this->expectException(\MongoResultException::class);
  1109. $this->expectExceptionMessage('Index with name: bar_1 already exists with different options');
  1110. $collection->ensureIndex(['bar' => 1]);
  1111. }
  1112. public function testDeleteIndexUsingIndexName()
  1113. {
  1114. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1115. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  1116. $expected = [
  1117. 'nIndexesWas' => 2,
  1118. 'errmsg' => 'index not found with name [bar_1]',
  1119. 'ok' => 0.0,
  1120. ];
  1121. if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
  1122. $expected['code'] = 27;
  1123. }
  1124. // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
  1125. $this->assertArraySubset($expected, $this->getCollection()->deleteIndex('bar'));
  1126. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  1127. }
  1128. public function testDeleteIndexUsingField()
  1129. {
  1130. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1131. $newCollection->createIndex(['bar' => 1]);
  1132. $expected = [
  1133. 'nIndexesWas' => 2,
  1134. 'ok' => 1.0,
  1135. ];
  1136. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  1137. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  1138. }
  1139. public function testDeleteIndexUsingKeys()
  1140. {
  1141. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1142. $newCollection->createIndex(['bar' => 1]);
  1143. $expected = [
  1144. 'nIndexesWas' => 2,
  1145. 'ok' => 1.0,
  1146. ];
  1147. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  1148. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  1149. }
  1150. public function testDeleteIndexes()
  1151. {
  1152. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1153. $newCollection->createIndex(['bar' => 1]);
  1154. $expected = [
  1155. 'nIndexesWas' => 2,
  1156. 'msg' => 'non-_id indexes dropped for collection',
  1157. 'ok' => 1.0,
  1158. ];
  1159. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  1160. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  1161. }
  1162. public function testDeleteIndexesForNonExistingCollection()
  1163. {
  1164. $expected = [
  1165. 'ok' => 0.0,
  1166. 'errmsg' => 'ns not found',
  1167. ];
  1168. if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
  1169. $expected['code'] = 26;
  1170. }
  1171. // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
  1172. $this->assertArraySubset($expected, $this->getCollection('nonExisting')->deleteIndexes());
  1173. }
  1174. public function dataGetIndexInfo()
  1175. {
  1176. $indexVersion = $this->getDefaultIndexVersion();
  1177. return [
  1178. 'plainIndex' => [
  1179. 'expectedIndex' => [
  1180. 'v' => $indexVersion,
  1181. 'key' => ['foo' => 1],
  1182. 'name' => 'foo_1',
  1183. 'ns' => 'mongo-php-adapter.test',
  1184. ],
  1185. 'fields' => ['foo' => 1],
  1186. 'options' => [],
  1187. ],
  1188. 'uniqueIndex' => [
  1189. 'expectedIndex' => [
  1190. 'v' => $indexVersion,
  1191. 'key' => ['foo' => 1],
  1192. 'name' => 'foo_1',
  1193. 'ns' => 'mongo-php-adapter.test',
  1194. 'unique' => true,
  1195. ],
  1196. 'fields' => ['foo' => 1],
  1197. 'options' => ['unique' => true],
  1198. ],
  1199. 'sparseIndex' => [
  1200. 'expectedIndex' => [
  1201. 'v' => $indexVersion,
  1202. 'key' => ['foo' => 1],
  1203. 'name' => 'foo_1',
  1204. 'ns' => 'mongo-php-adapter.test',
  1205. 'sparse' => true,
  1206. ],
  1207. 'fields' => ['foo' => 1],
  1208. 'options' => ['sparse' => true],
  1209. ],
  1210. 'ttlIndex' => [
  1211. 'expectedIndex' => [
  1212. 'v' => $indexVersion,
  1213. 'key' => ['foo' => 1],
  1214. 'name' => 'foo_1',
  1215. 'ns' => 'mongo-php-adapter.test',
  1216. 'expireAfterSeconds' => 86400,
  1217. ],
  1218. 'fields' => ['foo' => 1],
  1219. 'options' => ['expireAfterSeconds' => 86400],
  1220. ],
  1221. 'textIndex' => [
  1222. 'expectedIndex' => [
  1223. 'v' => $indexVersion,
  1224. 'key' => [
  1225. '_fts' => 'text',
  1226. '_ftsx' => 1,
  1227. ],
  1228. 'name' => 'foo_text',
  1229. 'ns' => 'mongo-php-adapter.test',
  1230. 'weights' => [
  1231. 'foo' => 1,
  1232. ],
  1233. 'default_language' => 'english',
  1234. 'language_override' => 'language',
  1235. 'textIndexVersion' => version_compare($this->getServerVersion(), '3.2.0', '>=') ? 3 : 2,
  1236. ],
  1237. 'fields' => ['foo' => 'text'],
  1238. 'options' => [],
  1239. ],
  1240. 'partialFilterExpression' => [
  1241. 'expectedIndex' => [
  1242. 'v' => $indexVersion,
  1243. 'key' => ['foo' => 1],
  1244. 'name' => 'foo_1',
  1245. 'ns' => 'mongo-php-adapter.test',
  1246. 'partialFilterExpression' => [
  1247. 'bar' => ['$gt' => 1],
  1248. ],
  1249. ],
  1250. 'fields' => ['foo' => 1],
  1251. 'options' => [
  1252. 'partialFilterExpression' => ['bar' => ['$gt' => 1]],
  1253. ],
  1254. ],
  1255. 'geoSpatial' => [
  1256. 'expectedIndex' => [
  1257. 'v' => $indexVersion,
  1258. 'key' => ['foo' => '2dsphere'],
  1259. 'name' => 'foo_2dsphere',
  1260. 'ns' => 'mongo-php-adapter.test',
  1261. '2dsphereIndexVersion' => version_compare($this->getServerVersion(), '3.2.0', '>=') ? 3 : 2,
  1262. ],
  1263. 'fields' => ['foo' => '2dsphere'],
  1264. 'options' => [],
  1265. ],
  1266. 'geoHaystack' => [
  1267. 'expectedIndex' => [
  1268. 'v' => $indexVersion,
  1269. 'key' => ['foo' => 'geoHaystack', 'bar' => 1],
  1270. 'name' => 'foo_geoHaystack_bar_1',
  1271. 'ns' => 'mongo-php-adapter.test',
  1272. 'bucketSize' => 10,
  1273. ],
  1274. 'fields' => ['foo' => 'geoHaystack', 'bar' => 1],
  1275. 'options' => ['bucketSize' => 10],
  1276. ],
  1277. ];
  1278. }
  1279. /**
  1280. * @dataProvider dataGetIndexInfo
  1281. */
  1282. public function testGetIndexInfo($expectedIndex, $fields, $options)
  1283. {
  1284. $idIndex = [
  1285. 'v' => $this->getDefaultIndexVersion(),
  1286. 'key' => ['_id' => 1],
  1287. 'name' => '_id_',
  1288. 'ns' => 'mongo-php-adapter.test',
  1289. ];
  1290. $expectedIndexInfo = [$idIndex, $expectedIndex];
  1291. $collection = $this->getCollection();
  1292. $collection->createIndex($fields, $options);
  1293. $this->assertEquals(
  1294. $expectedIndexInfo,
  1295. $collection->getIndexInfo()
  1296. );
  1297. }
  1298. public function testFindAndModifyUpdate()
  1299. {
  1300. $id = '54203e08d51d4a1f868b456e';
  1301. $collection = $this->getCollection();
  1302. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1303. $collection->insert($document);
  1304. $document = $collection->findAndModify(
  1305. ['_id' => new \MongoId($id)],
  1306. ['$set' => ['foo' => 'foo']]
  1307. );
  1308. $this->assertSame('bar', $document['foo']);
  1309. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1310. $this->assertSame(1, $newCollection->count());
  1311. $object = $newCollection->findOne();
  1312. $this->assertNotNull($object);
  1313. $this->assertAttributeSame('foo', 'foo', $object);
  1314. }
  1315. public function testFindAndModifyUpdateWithUpdateOptions()
  1316. {
  1317. $id = '54203e08d51d4a1f868b456e';
  1318. $collection = $this->getCollection();
  1319. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1320. $collection->insert($document);
  1321. $document = $collection->findAndModify(
  1322. ['_id' => new \MongoId($id)],
  1323. [],
  1324. [],
  1325. [
  1326. 'update' => ['bar' => 'foo']
  1327. ]
  1328. );
  1329. $this->assertSame('bar', $document['foo']);
  1330. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1331. $this->assertSame(1, $newCollection->count());
  1332. $object = $newCollection->findOne();
  1333. $this->assertNotNull($object);
  1334. $this->assertAttributeSame('foo', 'bar', $object);
  1335. $this->assertObjectNotHasAttribute('foo', $object);
  1336. }
  1337. public function testFindAndModifyWithUpdateParamAndOption()
  1338. {
  1339. $id = '54203e08d51d4a1f868b456e';
  1340. $collection = $this->getCollection();
  1341. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1342. $collection->insert($document);
  1343. $data = ['foo' => 'foo', 'bar' => 'bar'];
  1344. $this->getCollection()->findAndModify(
  1345. ['_id' => new \MongoId($id)],
  1346. [$data],
  1347. [],
  1348. [
  1349. 'update' => ['$set' => ['foo' => 'foobar']],
  1350. 'upsert' => true,
  1351. ]
  1352. );
  1353. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1354. $this->assertSame(1, $newCollection->count());
  1355. $object = $newCollection->findOne();
  1356. $this->assertNotNull($object);
  1357. $this->assertAttributeSame('foobar', 'foo', $object);
  1358. $this->assertObjectNotHasAttribute('bar', $object);
  1359. }
  1360. public function testFindAndModifyUpdateReplace()
  1361. {
  1362. $id = '54203e08d51d4a1f868b456e';
  1363. $collection = $this->getCollection();
  1364. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1365. $collection->insert($document);
  1366. $document = $collection->findAndModify(
  1367. ['_id' => new \MongoId($id)],
  1368. ['_id' => new \MongoId($id), 'foo' => 'boo']
  1369. );
  1370. $this->assertSame('bar', $document['foo']);
  1371. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1372. $this->assertSame(1, $newCollection->count());
  1373. $object = $newCollection->findOne();
  1374. $this->assertNotNull($object);
  1375. $this->assertAttributeSame('boo', 'foo', $object);
  1376. $this->assertObjectNotHasAttribute('bar', $object);
  1377. }
  1378. public function testFindAndModifyUpdateReturnNew()
  1379. {
  1380. $id = '54203e08d51d4a1f868b456e';
  1381. $collection = $this->getCollection();
  1382. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1383. $collection->insert($document);
  1384. $document = $collection->findAndModify(
  1385. ['_id' => new \MongoId($id)],
  1386. ['$set' => ['foo' => 'foo']],
  1387. null,
  1388. ['new' => true]
  1389. );
  1390. $this->assertSame('foo', $document['foo']);
  1391. }
  1392. public function testFindAndModifyWithFields()
  1393. {
  1394. $id = '54203e08d51d4a1f868b456e';
  1395. $collection = $this->getCollection();
  1396. $document = [
  1397. '_id' => new \MongoId($id),
  1398. 'foo' => 'bar',
  1399. 'bar' => 'foo',
  1400. ];
  1401. $collection->insert($document);
  1402. $document = $collection->findAndModify(
  1403. ['_id' => new \MongoId($id)],
  1404. ['$set' => ['foo' => 'foo']],
  1405. ['foo' => true]
  1406. );
  1407. $this->assertArrayNotHasKey('bar', $document);
  1408. $this->assertArrayHasKey('foo', $document);
  1409. }
  1410. public function testGroup()
  1411. {
  1412. $collection = $this->getCollection();
  1413. $document1 = ['a' => 2];
  1414. $collection->insert($document1);
  1415. $document2 = ['b' => 5];
  1416. $collection->insert($document2);
  1417. $document3 = ['a' => 1];
  1418. $collection->insert($document3);
  1419. $keys = [];
  1420. $initial = ["count" => 0];
  1421. $reduce = "function (obj, prev) { prev.count++; }";
  1422. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  1423. $result = $collection->group($keys, $initial, $reduce, $condition);
  1424. $this->assertArraySubset(
  1425. [
  1426. 'retval' => [['count' => 1.0]],
  1427. 'count' => 1.0,
  1428. 'keys' => 1,
  1429. 'ok' => 1.0,
  1430. ],
  1431. $result
  1432. );
  1433. }
  1434. public function testMapReduce()
  1435. {
  1436. $data = array(
  1437. array(
  1438. 'username' => 'jones',
  1439. 'likes' => 20.0,
  1440. 'text' => 'Hello world!'
  1441. ),
  1442. array(
  1443. 'username' => 'bob',
  1444. 'likes' => 100.0,
  1445. 'text' => 'Hello world!'
  1446. ),
  1447. array(
  1448. 'username' => 'bob',
  1449. 'likes' => 100.0,
  1450. 'text' => 'Hello world!'
  1451. ),
  1452. );
  1453. $collection = $this->getCollection();
  1454. $collection->batchInsert($data);
  1455. $map = 'function() {
  1456. emit(this.username, { count: 1, likes: this.likes });
  1457. }';
  1458. $reduce = 'function(key, values) {
  1459. var result = {count: 0, likes: 0};
  1460. values.forEach(function(value) {
  1461. result.count += value.count;
  1462. result.likes += value.likes;
  1463. });
  1464. return result;
  1465. }';
  1466. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  1467. $command = [
  1468. 'mapreduce' => $this->getCollection()->getName(),
  1469. 'map' => new \MongoCode($map),
  1470. 'reduce' => new \MongoCode($reduce),
  1471. 'query' => (object) [],
  1472. 'out' => ['inline' => true],
  1473. 'finalize' => new \MongoCode($finalize),
  1474. ];
  1475. $result = $this->getDatabase()->command($command);
  1476. $expected = [
  1477. [
  1478. '_id' => 'bob',
  1479. 'value' => [
  1480. 'count' => 2.0,
  1481. 'likes' => 200.0,
  1482. 'test' => 'test',
  1483. ],
  1484. ],
  1485. [
  1486. '_id' => 'jones',
  1487. 'value' => [
  1488. 'count' => 1.0,
  1489. 'likes' => 20.0,
  1490. 'test' => 'test',
  1491. ],
  1492. ],
  1493. ];
  1494. $this->assertSame(1.0, $result['ok']);
  1495. $this->assertSame($expected, $result['results']);
  1496. }
  1497. public function testFindAndModifyResultException()
  1498. {
  1499. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  1500. $collection = $this->getCollection();
  1501. $this->expectException(\MongoResultException::class);
  1502. $collection->findAndModify(
  1503. array("inprogress" => false, "name" => "Next promo"),
  1504. array('$unsupportedOperator' => array("tasks" => -1)),
  1505. array("tasks" => true),
  1506. array("new" => true)
  1507. );
  1508. }
  1509. public function testFindAndModifyExceptionTimeout()
  1510. {
  1511. $this->failMaxTimeMS();
  1512. $id = '54203e08d51d4a1f868b456e';
  1513. $collection = $this->getCollection();
  1514. $this->expectException(\MongoExecutionTimeoutException::class);
  1515. $document = $collection->findAndModify(
  1516. ['_id' => new \MongoId($id)],
  1517. null,
  1518. null,
  1519. ['maxTimeMS' => 1, 'remove' => true]
  1520. );
  1521. }
  1522. public function testFindAndModifyRemove()
  1523. {
  1524. $id = '54203e08d51d4a1f868b456e';
  1525. $collection = $this->getCollection();
  1526. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1527. $collection->insert($document);
  1528. $document = $collection->findAndModify(
  1529. ['_id' => new \MongoId($id)],
  1530. null,
  1531. null,
  1532. ['remove' => true]
  1533. );
  1534. $this->assertEquals('bar', $document['foo']);
  1535. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1536. $this->assertSame(0, $newCollection->count());
  1537. }
  1538. public function testValidate()
  1539. {
  1540. $collection = $this->getCollection();
  1541. $document = ['foo' => 'bar'];
  1542. $collection->insert($document);
  1543. $result = $collection->validate();
  1544. $this->assertArraySubset(
  1545. [
  1546. 'ns' => 'mongo-php-adapter.test',
  1547. 'nrecords' => 1,
  1548. 'nIndexes' => 1,
  1549. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  1550. 'valid' => true,
  1551. 'errors' => [],
  1552. ],
  1553. $result
  1554. );
  1555. }
  1556. public function testDrop()
  1557. {
  1558. $document = ['foo' => 'bar'];
  1559. $this->getCollection()->insert($document);
  1560. $expected = [
  1561. 'ns' => (string) $this->getCollection(),
  1562. 'nIndexesWas' => 1,
  1563. 'ok' => 1.0
  1564. ];
  1565. $this->assertSame($expected, $this->getCollection()->drop());
  1566. }
  1567. public function testEmptyCollectionName()
  1568. {
  1569. $this->expectException(\Exception::class);
  1570. $this->expectExceptionMessage('Collection name cannot be empty');
  1571. new \MongoCollection($this->getDatabase(), '');
  1572. }
  1573. public function testSelectCollectionWithNullBytes()
  1574. {
  1575. $this->expectException(\Exception::class);
  1576. $this->expectExceptionMessage('Collection name cannot contain null bytes');
  1577. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1578. }
  1579. public function testSubCollectionWithNullBytes()
  1580. {
  1581. $collection = $this->getCollection();
  1582. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1583. $this->assertSame('test', $collection->getName());
  1584. }
  1585. public function testSelectCollectionWithDatabaseObject()
  1586. {
  1587. $client = $this->getClient();
  1588. $database = $this->getDatabase($client);
  1589. $collection = $client->selectCollection($database, 'test');
  1590. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  1591. }
  1592. public function testHasNextLoop()
  1593. {
  1594. $collection = $this->getCollection();
  1595. for ($i = 0; $i < 5; $i++) {
  1596. $document = ['i' => $i];
  1597. $collection->insert($document);
  1598. }
  1599. $cursor = $collection->find()->sort(['i' => 1]);
  1600. $data = [];
  1601. $i = 0;
  1602. while ($cursor->hasNext()) {
  1603. $this->assertSame($i < 5, $cursor->hasNext());
  1604. $row = $cursor->getNext();
  1605. $this->assertSame($i, $row['i']);
  1606. $data[] = $row;
  1607. $i++;
  1608. }
  1609. $this->assertCount(5, $data);
  1610. }
  1611. public function testProjectionWithBSONTypes()
  1612. {
  1613. $collection = $this->getCollection();
  1614. $id = new \MongoId();
  1615. $referencedId = new \MongoId();
  1616. $data = [
  1617. '_id' => $id,
  1618. 'loveItems' => [
  1619. [
  1620. 'sellable' => [
  1621. '$ref' => 'sellables',
  1622. '$id' => $referencedId,
  1623. ]
  1624. ],
  1625. [
  1626. 'sellable' => [
  1627. '$ref' => 'sellables',
  1628. '$id' => new \MongoId(),
  1629. ]
  1630. ]
  1631. ]
  1632. ];
  1633. $collection->insert($data);
  1634. $item = $collection->findOne(
  1635. ['_id' => $id],
  1636. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1637. );
  1638. $this->assertArrayHasKey('loveItems', $item);
  1639. $this->assertCount(1, $item['loveItems']);
  1640. $cursor = $collection->find(
  1641. ['_id' => $id],
  1642. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1643. );
  1644. $items = iterator_to_array($cursor, false);
  1645. $this->assertCount(1, $items);
  1646. $this->assertCount(1, $items[0]['loveItems']);
  1647. }
  1648. public static function dataFindWithRegex()
  1649. {
  1650. return [
  1651. 'MongoRegex' => [new \MongoRegex('/^foo.*/i')],
  1652. 'BSONRegex' => [new Regex('^foo.*', 'i')],
  1653. ];
  1654. }
  1655. /**
  1656. * @dataProvider dataFindWithRegex
  1657. */
  1658. public function testFindWithRegex($regex)
  1659. {
  1660. $this->skipTestIf(extension_loaded('mongo'));
  1661. $document = ['name' => 'FOO 123'];
  1662. $this->getCollection()->insert($document);
  1663. $cursor = $this->getCollection()->find(['name' => $regex]);
  1664. $this->assertSame(1, $cursor->count());
  1665. }
  1666. }
  1667. class PrivatePropertiesStub
  1668. {
  1669. private $foo = 'bar';
  1670. }
  1671. class ArrayObjectWithProtectedProperties extends ArrayObject
  1672. {
  1673. protected $something = 'baz';
  1674. }