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. /**
  11. * @author alcaeus <alcaeus@alcaeus.org>
  12. */
  13. class MongoCollectionTest extends TestCase
  14. {
  15. public function testSerialize()
  16. {
  17. $this->assertInternalType('string', serialize($this->getCollection()));
  18. }
  19. public function testGetNestedCollections()
  20. {
  21. $collection = $this->getCollection()->foo->bar;
  22. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  23. }
  24. public function testCreateRecord()
  25. {
  26. $collection = $this->getCollection();
  27. $expected = [
  28. 'ok' => 1.0,
  29. 'n' => 0,
  30. 'err' => null,
  31. 'errmsg' => null,
  32. ];
  33. $document = ['foo' => 'bar'];
  34. $this->assertEquals($expected, $collection->insert($document));
  35. $this->assertInstanceOf('MongoId', $document['_id']);
  36. $id = (string) $document['_id'];
  37. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  38. $this->assertSame(1, $newCollection->count());
  39. $object = $newCollection->findOne();
  40. $this->assertNotNull($object);
  41. $this->assertInstanceOf('MongoDB\BSON\ObjectID', $object->_id);
  42. $this->assertSame($id, (string) $object->_id);
  43. $this->assertNotNull($object->foo);
  44. $this->assertSame('bar', $object->foo);
  45. }
  46. public function testInsertInvalidData()
  47. {
  48. // Dirty hack to support both PHPUnit 5.x and 6.x
  49. $className = class_exists(Warning::class) ? Warning::class : \PHPUnit_Framework_Error_Warning::class;
  50. $this->expectException($className);
  51. $this->expectExceptionMessage('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->assertArraySubset($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->assertArraySubset($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->assertArraySubset($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->assertArraySubset(['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->assertArraySubset(['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->assertArraySubset(['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->assertArraySubset(['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->assertInternalType('array', $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->assertInternalType('array', $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->assertInternalType('array', $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->assertInternalType('array', $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->assertArraySubset(['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 assertArraySubset because newer versions (3.4.7?) also return `codeName`
  1127. $this->assertArraySubset($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. $expected = [
  1167. 'ok' => 0.0,
  1168. 'errmsg' => 'ns not found',
  1169. ];
  1170. if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
  1171. $expected['code'] = 26;
  1172. }
  1173. // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
  1174. $this->assertArraySubset($expected, $this->getCollection('nonExisting')->deleteIndexes());
  1175. }
  1176. public function dataGetIndexInfo()
  1177. {
  1178. $indexVersion = $this->getDefaultIndexVersion();
  1179. return [
  1180. 'plainIndex' => [
  1181. 'expectedIndex' => [
  1182. 'v' => $indexVersion,
  1183. 'key' => ['foo' => 1],
  1184. 'name' => 'foo_1',
  1185. 'ns' => 'mongo-php-adapter.test',
  1186. ],
  1187. 'fields' => ['foo' => 1],
  1188. 'options' => [],
  1189. ],
  1190. 'uniqueIndex' => [
  1191. 'expectedIndex' => [
  1192. 'v' => $indexVersion,
  1193. 'key' => ['foo' => 1],
  1194. 'name' => 'foo_1',
  1195. 'ns' => 'mongo-php-adapter.test',
  1196. 'unique' => true,
  1197. ],
  1198. 'fields' => ['foo' => 1],
  1199. 'options' => ['unique' => true],
  1200. ],
  1201. 'sparseIndex' => [
  1202. 'expectedIndex' => [
  1203. 'v' => $indexVersion,
  1204. 'key' => ['foo' => 1],
  1205. 'name' => 'foo_1',
  1206. 'ns' => 'mongo-php-adapter.test',
  1207. 'sparse' => true,
  1208. ],
  1209. 'fields' => ['foo' => 1],
  1210. 'options' => ['sparse' => true],
  1211. ],
  1212. 'ttlIndex' => [
  1213. 'expectedIndex' => [
  1214. 'v' => $indexVersion,
  1215. 'key' => ['foo' => 1],
  1216. 'name' => 'foo_1',
  1217. 'ns' => 'mongo-php-adapter.test',
  1218. 'expireAfterSeconds' => 86400,
  1219. ],
  1220. 'fields' => ['foo' => 1],
  1221. 'options' => ['expireAfterSeconds' => 86400],
  1222. ],
  1223. 'textIndex' => [
  1224. 'expectedIndex' => [
  1225. 'v' => $indexVersion,
  1226. 'key' => [
  1227. '_fts' => 'text',
  1228. '_ftsx' => 1,
  1229. ],
  1230. 'name' => 'foo_text',
  1231. 'ns' => 'mongo-php-adapter.test',
  1232. 'weights' => [
  1233. 'foo' => 1,
  1234. ],
  1235. 'default_language' => 'english',
  1236. 'language_override' => 'language',
  1237. 'textIndexVersion' => version_compare($this->getServerVersion(), '3.2.0', '>=') ? 3 : 2,
  1238. ],
  1239. 'fields' => ['foo' => 'text'],
  1240. 'options' => [],
  1241. ],
  1242. 'partialFilterExpression' => [
  1243. 'expectedIndex' => [
  1244. 'v' => $indexVersion,
  1245. 'key' => ['foo' => 1],
  1246. 'name' => 'foo_1',
  1247. 'ns' => 'mongo-php-adapter.test',
  1248. 'partialFilterExpression' => [
  1249. 'bar' => ['$gt' => 1],
  1250. ],
  1251. ],
  1252. 'fields' => ['foo' => 1],
  1253. 'options' => [
  1254. 'partialFilterExpression' => ['bar' => ['$gt' => 1]],
  1255. ],
  1256. ],
  1257. 'geoSpatial' => [
  1258. 'expectedIndex' => [
  1259. 'v' => $indexVersion,
  1260. 'key' => ['foo' => '2dsphere'],
  1261. 'name' => 'foo_2dsphere',
  1262. 'ns' => 'mongo-php-adapter.test',
  1263. '2dsphereIndexVersion' => version_compare($this->getServerVersion(), '3.2.0', '>=') ? 3 : 2,
  1264. ],
  1265. 'fields' => ['foo' => '2dsphere'],
  1266. 'options' => [],
  1267. ],
  1268. 'geoHaystack' => [
  1269. 'expectedIndex' => [
  1270. 'v' => $indexVersion,
  1271. 'key' => ['foo' => 'geoHaystack', 'bar' => 1],
  1272. 'name' => 'foo_geoHaystack_bar_1',
  1273. 'ns' => 'mongo-php-adapter.test',
  1274. 'bucketSize' => 10,
  1275. ],
  1276. 'fields' => ['foo' => 'geoHaystack', 'bar' => 1],
  1277. 'options' => ['bucketSize' => 10],
  1278. ],
  1279. ];
  1280. }
  1281. /**
  1282. * @dataProvider dataGetIndexInfo
  1283. */
  1284. public function testGetIndexInfo($expectedIndex, $fields, $options)
  1285. {
  1286. $idIndex = [
  1287. 'v' => $this->getDefaultIndexVersion(),
  1288. 'key' => ['_id' => 1],
  1289. 'name' => '_id_',
  1290. 'ns' => 'mongo-php-adapter.test',
  1291. ];
  1292. $expectedIndexInfo = [$idIndex, $expectedIndex];
  1293. $collection = $this->getCollection();
  1294. $collection->createIndex($fields, $options);
  1295. $this->assertEquals(
  1296. $expectedIndexInfo,
  1297. $collection->getIndexInfo()
  1298. );
  1299. }
  1300. public function testFindAndModifyUpdate()
  1301. {
  1302. $id = '54203e08d51d4a1f868b456e';
  1303. $collection = $this->getCollection();
  1304. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1305. $collection->insert($document);
  1306. $document = $collection->findAndModify(
  1307. ['_id' => new \MongoId($id)],
  1308. ['$set' => ['foo' => 'foo']]
  1309. );
  1310. $this->assertSame('bar', $document['foo']);
  1311. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1312. $this->assertSame(1, $newCollection->count());
  1313. $object = $newCollection->findOne();
  1314. $this->assertNotNull($object);
  1315. $this->assertSame('foo', $object->foo);
  1316. }
  1317. public function testFindAndModifyUpdateWithUpdateOptions()
  1318. {
  1319. $id = '54203e08d51d4a1f868b456e';
  1320. $collection = $this->getCollection();
  1321. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1322. $collection->insert($document);
  1323. $document = $collection->findAndModify(
  1324. ['_id' => new \MongoId($id)],
  1325. [],
  1326. [],
  1327. [
  1328. 'update' => ['bar' => 'foo']
  1329. ]
  1330. );
  1331. $this->assertSame('bar', $document['foo']);
  1332. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1333. $this->assertSame(1, $newCollection->count());
  1334. $object = $newCollection->findOne();
  1335. $this->assertNotNull($object);
  1336. $this->assertSame('foo', $object->bar);
  1337. $this->assertArrayNotHasKey('foo', $object);
  1338. }
  1339. public function testFindAndModifyWithUpdateParamAndOption()
  1340. {
  1341. $id = '54203e08d51d4a1f868b456e';
  1342. $collection = $this->getCollection();
  1343. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1344. $collection->insert($document);
  1345. $data = ['foo' => 'foo', 'bar' => 'bar'];
  1346. $this->getCollection()->findAndModify(
  1347. ['_id' => new \MongoId($id)],
  1348. [$data],
  1349. [],
  1350. [
  1351. 'update' => ['$set' => ['foo' => 'foobar']],
  1352. 'upsert' => true,
  1353. ]
  1354. );
  1355. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1356. $this->assertSame(1, $newCollection->count());
  1357. $object = $newCollection->findOne();
  1358. $this->assertNotNull($object);
  1359. $this->assertSame('foobar', $object->foo);
  1360. $this->assertArrayNotHasKey('bar', $object);
  1361. }
  1362. public function testFindAndModifyUpdateReplace()
  1363. {
  1364. $id = '54203e08d51d4a1f868b456e';
  1365. $collection = $this->getCollection();
  1366. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1367. $collection->insert($document);
  1368. $document = $collection->findAndModify(
  1369. ['_id' => new \MongoId($id)],
  1370. ['_id' => new \MongoId($id), 'foo' => 'boo']
  1371. );
  1372. $this->assertSame('bar', $document['foo']);
  1373. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1374. $this->assertSame(1, $newCollection->count());
  1375. $object = $newCollection->findOne();
  1376. $this->assertNotNull($object);
  1377. $this->assertSame('boo', $object->foo);
  1378. $this->assertArrayNotHasKey('bar', $object);
  1379. }
  1380. public function testFindAndModifyUpdateReturnNew()
  1381. {
  1382. $id = '54203e08d51d4a1f868b456e';
  1383. $collection = $this->getCollection();
  1384. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1385. $collection->insert($document);
  1386. $document = $collection->findAndModify(
  1387. ['_id' => new \MongoId($id)],
  1388. ['$set' => ['foo' => 'foo']],
  1389. null,
  1390. ['new' => true]
  1391. );
  1392. $this->assertSame('foo', $document['foo']);
  1393. }
  1394. public function testFindAndModifyWithFields()
  1395. {
  1396. $id = '54203e08d51d4a1f868b456e';
  1397. $collection = $this->getCollection();
  1398. $document = [
  1399. '_id' => new \MongoId($id),
  1400. 'foo' => 'bar',
  1401. 'bar' => 'foo',
  1402. ];
  1403. $collection->insert($document);
  1404. $document = $collection->findAndModify(
  1405. ['_id' => new \MongoId($id)],
  1406. ['$set' => ['foo' => 'foo']],
  1407. ['foo' => true]
  1408. );
  1409. $this->assertArrayNotHasKey('bar', $document);
  1410. $this->assertArrayHasKey('foo', $document);
  1411. }
  1412. public function testGroup()
  1413. {
  1414. $this->skipTestIf(version_compare($this->getServerVersion(), '4.2.0', '>='), 'Test does not apply to MongoDB >= 4.2.');
  1415. $collection = $this->getCollection();
  1416. $document1 = ['a' => 2];
  1417. $collection->insert($document1);
  1418. $document2 = ['b' => 5];
  1419. $collection->insert($document2);
  1420. $document3 = ['a' => 1];
  1421. $collection->insert($document3);
  1422. $keys = [];
  1423. $initial = ["count" => 0];
  1424. $reduce = "function (obj, prev) { prev.count++; }";
  1425. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  1426. $result = $collection->group($keys, $initial, $reduce, $condition);
  1427. $this->assertArraySubset(
  1428. [
  1429. 'retval' => [['count' => 1.0]],
  1430. 'count' => 1.0,
  1431. 'keys' => 1,
  1432. 'ok' => 1.0,
  1433. ],
  1434. $result
  1435. );
  1436. }
  1437. public function testMapReduce()
  1438. {
  1439. $data = array(
  1440. array(
  1441. 'username' => 'jones',
  1442. 'likes' => 20.0,
  1443. 'text' => 'Hello world!'
  1444. ),
  1445. array(
  1446. 'username' => 'bob',
  1447. 'likes' => 100.0,
  1448. 'text' => 'Hello world!'
  1449. ),
  1450. array(
  1451. 'username' => 'bob',
  1452. 'likes' => 100.0,
  1453. 'text' => 'Hello world!'
  1454. ),
  1455. );
  1456. $collection = $this->getCollection();
  1457. $collection->batchInsert($data);
  1458. $map = 'function() {
  1459. emit(this.username, { count: 1, likes: this.likes });
  1460. }';
  1461. $reduce = 'function(key, values) {
  1462. var result = {count: 0, likes: 0};
  1463. values.forEach(function(value) {
  1464. result.count += value.count;
  1465. result.likes += value.likes;
  1466. });
  1467. return result;
  1468. }';
  1469. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  1470. $command = [
  1471. 'mapreduce' => $this->getCollection()->getName(),
  1472. 'map' => new \MongoCode($map),
  1473. 'reduce' => new \MongoCode($reduce),
  1474. 'query' => (object) [],
  1475. 'out' => ['inline' => true],
  1476. 'finalize' => new \MongoCode($finalize),
  1477. ];
  1478. $result = $this->getDatabase()->command($command);
  1479. $expected = [
  1480. [
  1481. '_id' => 'bob',
  1482. 'value' => [
  1483. 'count' => 2.0,
  1484. 'likes' => 200.0,
  1485. 'test' => 'test',
  1486. ],
  1487. ],
  1488. [
  1489. '_id' => 'jones',
  1490. 'value' => [
  1491. 'count' => 1.0,
  1492. 'likes' => 20.0,
  1493. 'test' => 'test',
  1494. ],
  1495. ],
  1496. ];
  1497. $this->assertSame(1.0, $result['ok']);
  1498. $this->assertSame($expected, $result['results']);
  1499. }
  1500. public function testFindAndModifyResultException()
  1501. {
  1502. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  1503. $collection = $this->getCollection();
  1504. $this->expectException(\MongoResultException::class);
  1505. $collection->findAndModify(
  1506. array("inprogress" => false, "name" => "Next promo"),
  1507. array('$unsupportedOperator' => array("tasks" => -1)),
  1508. array("tasks" => true),
  1509. array("new" => true)
  1510. );
  1511. }
  1512. public function testFindAndModifyExceptionTimeout()
  1513. {
  1514. $this->failMaxTimeMS();
  1515. $id = '54203e08d51d4a1f868b456e';
  1516. $collection = $this->getCollection();
  1517. $this->expectException(\MongoExecutionTimeoutException::class);
  1518. $document = $collection->findAndModify(
  1519. ['_id' => new \MongoId($id)],
  1520. null,
  1521. null,
  1522. ['maxTimeMS' => 1, 'remove' => true]
  1523. );
  1524. }
  1525. public function testFindAndModifyRemove()
  1526. {
  1527. $id = '54203e08d51d4a1f868b456e';
  1528. $collection = $this->getCollection();
  1529. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1530. $collection->insert($document);
  1531. $document = $collection->findAndModify(
  1532. ['_id' => new \MongoId($id)],
  1533. null,
  1534. null,
  1535. ['remove' => true]
  1536. );
  1537. $this->assertEquals('bar', $document['foo']);
  1538. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1539. $this->assertSame(0, $newCollection->count());
  1540. }
  1541. public function testValidate()
  1542. {
  1543. $collection = $this->getCollection();
  1544. $document = ['foo' => 'bar'];
  1545. $collection->insert($document);
  1546. $result = $collection->validate();
  1547. $this->assertArraySubset(
  1548. [
  1549. 'ns' => 'mongo-php-adapter.test',
  1550. 'nrecords' => 1,
  1551. 'nIndexes' => 1,
  1552. 'valid' => true,
  1553. 'errors' => [],
  1554. ],
  1555. $result
  1556. );
  1557. }
  1558. public function testDrop()
  1559. {
  1560. $document = ['foo' => 'bar'];
  1561. $this->getCollection()->insert($document);
  1562. $expected = [
  1563. 'ns' => (string) $this->getCollection(),
  1564. 'nIndexesWas' => 1,
  1565. 'ok' => 1.0
  1566. ];
  1567. $this->assertEquals($expected, $this->getCollection()->drop());
  1568. }
  1569. public function testEmptyCollectionName()
  1570. {
  1571. $this->expectException(\Exception::class);
  1572. $this->expectExceptionMessage('Collection name cannot be empty');
  1573. new \MongoCollection($this->getDatabase(), '');
  1574. }
  1575. public function testSelectCollectionWithNullBytes()
  1576. {
  1577. $this->expectException(\Exception::class);
  1578. $this->expectExceptionMessage('Collection name cannot contain null bytes');
  1579. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1580. }
  1581. public function testSubCollectionWithNullBytes()
  1582. {
  1583. $collection = $this->getCollection();
  1584. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1585. $this->assertSame('test', $collection->getName());
  1586. }
  1587. public function testSelectCollectionWithDatabaseObject()
  1588. {
  1589. $client = $this->getClient();
  1590. $database = $this->getDatabase($client);
  1591. $collection = $client->selectCollection($database, 'test');
  1592. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  1593. }
  1594. public function testHasNextLoop()
  1595. {
  1596. $collection = $this->getCollection();
  1597. for ($i = 0; $i < 5; $i++) {
  1598. $document = ['i' => $i];
  1599. $collection->insert($document);
  1600. }
  1601. $cursor = $collection->find()->sort(['i' => 1]);
  1602. $data = [];
  1603. $i = 0;
  1604. while ($cursor->hasNext()) {
  1605. $this->assertSame($i < 5, $cursor->hasNext());
  1606. $row = $cursor->getNext();
  1607. $this->assertSame($i, $row['i']);
  1608. $data[] = $row;
  1609. $i++;
  1610. }
  1611. $this->assertCount(5, $data);
  1612. }
  1613. public function testProjectionWithBSONTypes()
  1614. {
  1615. $collection = $this->getCollection();
  1616. $id = new \MongoId();
  1617. $referencedId = new \MongoId();
  1618. $data = [
  1619. '_id' => $id,
  1620. 'loveItems' => [
  1621. [
  1622. 'sellable' => [
  1623. '$ref' => 'sellables',
  1624. '$id' => $referencedId,
  1625. ]
  1626. ],
  1627. [
  1628. 'sellable' => [
  1629. '$ref' => 'sellables',
  1630. '$id' => new \MongoId(),
  1631. ]
  1632. ]
  1633. ]
  1634. ];
  1635. $collection->insert($data);
  1636. $item = $collection->findOne(
  1637. ['_id' => $id],
  1638. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1639. );
  1640. $this->assertArrayHasKey('loveItems', $item);
  1641. $this->assertCount(1, $item['loveItems']);
  1642. $cursor = $collection->find(
  1643. ['_id' => $id],
  1644. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1645. );
  1646. $items = iterator_to_array($cursor, false);
  1647. $this->assertCount(1, $items);
  1648. $this->assertCount(1, $items[0]['loveItems']);
  1649. }
  1650. public static function dataFindWithRegex()
  1651. {
  1652. return [
  1653. 'MongoRegex' => [new \MongoRegex('/^foo.*/i')],
  1654. 'BSONRegex' => [new Regex('^foo.*', 'i')],
  1655. ];
  1656. }
  1657. /**
  1658. * @dataProvider dataFindWithRegex
  1659. */
  1660. public function testFindWithRegex($regex)
  1661. {
  1662. $this->skipTestIf(extension_loaded('mongo'));
  1663. $document = ['name' => 'FOO 123'];
  1664. $this->getCollection()->insert($document);
  1665. $cursor = $this->getCollection()->find(['name' => $regex]);
  1666. $this->assertSame(1, $cursor->count());
  1667. }
  1668. }
  1669. class PrivatePropertiesStub
  1670. {
  1671. private $foo = 'bar';
  1672. }
  1673. class ArrayObjectWithProtectedProperties extends ArrayObject
  1674. {
  1675. protected $something = 'baz';
  1676. }