MongoCollectionTest.php 63 KB

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