MongoCollectionTest.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  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->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  42. $this->assertSame($id, (string) $object->_id);
  43. $this->assertObjectHasAttribute('foo', $object);
  44. $this->assertAttributeSame('bar', 'foo', $object);
  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->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  862. $this->assertSame($id, (string) $object->_id);
  863. $this->assertObjectHasAttribute('foo', $object);
  864. $this->assertAttributeSame('bar', 'foo', $object);
  865. }
  866. public function testRemoveOne()
  867. {
  868. $id = '54203e08d51d4a1f868b456e';
  869. $collection = $this->getCollection();
  870. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  871. $collection->insert($document);
  872. $collection->remove(['_id' => new \MongoId($id)]);
  873. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  874. $this->assertSame(0, $newCollection->count());
  875. }
  876. public function testSaveUpdate()
  877. {
  878. $expected = [
  879. 'ok' => 1.0,
  880. 'nModified' => 1,
  881. 'n' => 1,
  882. 'err' => null,
  883. 'errmsg' => null,
  884. 'updatedExisting' => true,
  885. ];
  886. $id = '54203e08d51d4a1f868b456e';
  887. $collection = $this->getCollection();
  888. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  889. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  890. $collection->insert($insertDocument);
  891. $this->assertEquals($expected, $collection->save($saveDocument));
  892. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  893. $this->assertSame(1, $newCollection->count());
  894. $object = $newCollection->findOne();
  895. $this->assertNotNull($object);
  896. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  897. $this->assertSame($id, (string) $object->_id);
  898. $this->assertObjectHasAttribute('foo', $object);
  899. $this->assertAttributeSame('foo', 'foo', $object);
  900. }
  901. public function testSavingShouldReplaceTheWholeDocument()
  902. {
  903. $id = '54203e08d51d4a1f868b456e';
  904. $collection = $this->getCollection();
  905. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  906. $saveDocument = ['_id' => new \MongoId($id)];
  907. $collection->insert($insertDocument);
  908. $collection->save($saveDocument);
  909. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  910. $this->assertSame(1, $newCollection->count());
  911. $object = $newCollection->findOne();
  912. $this->assertNotNull($object);
  913. $this->assertObjectNotHasAttribute('foo', $object);
  914. }
  915. public function testSaveDuplicate()
  916. {
  917. $collection = $this->getCollection();
  918. $collection->createIndex(['foo' => 1], ['unique' => true]);
  919. $document = ['foo' => 'bar'];
  920. $collection->save($document);
  921. $this->expectException(\MongoDuplicateKeyException::class);
  922. unset($document['_id']);
  923. $collection->save($document);
  924. }
  925. public function testSaveEmptyKeys()
  926. {
  927. $document = [];
  928. $this->getCollection()->save($document);
  929. $this->assertSame(1, $this->getCollection()->count());
  930. }
  931. public function testSaveEmptyObject()
  932. {
  933. $document = (object) [];
  934. $this->getCollection()->save($document);
  935. $this->assertSame(1, $this->getCollection()->count());
  936. }
  937. public function testGetDBRef()
  938. {
  939. $collection = $this->getCollection();
  940. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  941. $collection->insert($insertDocument);
  942. $document = $collection->getDBRef([
  943. '$ref' => 'test',
  944. '$id' => 1,
  945. ]);
  946. $this->assertEquals($insertDocument, $document);
  947. }
  948. public function testCreateDBRef()
  949. {
  950. $collection = $this->getCollection();
  951. $reference = $collection->createDBRef(['_id' => 'foo']);
  952. $this->assertSame(
  953. [
  954. '$ref' => 'test',
  955. '$id' => 'foo',
  956. ],
  957. $reference
  958. );
  959. }
  960. public function testCreateIndex()
  961. {
  962. $expected = [
  963. 'createdCollectionAutomatically' => true,
  964. 'numIndexesBefore' => 1,
  965. 'numIndexesAfter' => 2,
  966. 'ok' => 1.0,
  967. ];
  968. $collection = $this->getCollection();
  969. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  970. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  971. $iterator = $newCollection->listIndexes();
  972. $indexes = iterator_to_array($iterator);
  973. $this->assertCount(2, $indexes);
  974. $index = $indexes[1];
  975. $this->assertSame(['foo' => 1], $index->getKey());
  976. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  977. }
  978. public function testCreateIndexInvalid()
  979. {
  980. $this->expectException(\MongoException::class);
  981. $this->expectExceptionMessage('index specification has no elements');
  982. $this->getCollection()->createIndex([]);
  983. }
  984. public function testCreateIndexTwice()
  985. {
  986. $this->getCollection()->createIndex(['foo' => 1]);
  987. $expected = [
  988. 'createdCollectionAutomatically' => false,
  989. 'numIndexesBefore' => 2,
  990. 'numIndexesAfter' => 2,
  991. 'note' => 'all indexes already exist',
  992. 'ok' => 1.0
  993. ];
  994. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  995. }
  996. public function testCreateIndexWithDeprecatedOptions()
  997. {
  998. $this->getCollection()->createIndex(['foo' => 1], ['w' => 1]);
  999. $expected = [
  1000. 'createdCollectionAutomatically' => false,
  1001. 'numIndexesBefore' => 2,
  1002. 'numIndexesAfter' => 2,
  1003. 'note' => 'all indexes already exist',
  1004. 'ok' => 1.0
  1005. ];
  1006. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  1007. }
  1008. public function testCreateIndexTwiceWithSameName()
  1009. {
  1010. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  1011. $expected = [
  1012. 'createdCollectionAutomatically' => false,
  1013. 'numIndexesBefore' => 2,
  1014. 'numIndexesAfter' => 2,
  1015. 'note' => 'all indexes already exist',
  1016. 'ok' => 1.0
  1017. ];
  1018. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']));
  1019. }
  1020. public function testCreateIndexTwiceWithDifferentName()
  1021. {
  1022. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  1023. $expected = [
  1024. 'createdCollectionAutomatically' => false,
  1025. 'numIndexesBefore' => 2,
  1026. 'numIndexesAfter' => 2,
  1027. 'note' => 'all indexes already exist',
  1028. 'ok' => 1.0
  1029. ];
  1030. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'index_test']));
  1031. }
  1032. public function testCreateIndexTwiceWithDifferentOrder()
  1033. {
  1034. $this->getCollection()->createIndex(['foo' => 1, 'bar' => 1]);
  1035. $expected = [
  1036. 'createdCollectionAutomatically' => false,
  1037. 'numIndexesBefore' => 2,
  1038. 'numIndexesAfter' => 3,
  1039. 'ok' => 1.0
  1040. ];
  1041. $this->assertSame($expected, $this->getCollection()->createIndex(['bar' => 1, 'foo' => 1]));
  1042. }
  1043. public function testCreateIndexesWithDifferentOptions()
  1044. {
  1045. $this->expectException(\MongoResultException::class);
  1046. $this->getCollection()->createIndex(['foo' => 1]);
  1047. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  1048. }
  1049. /**
  1050. * @dataProvider createIndexIgnoredOptions
  1051. */
  1052. public function testCreateIndexesWithIgnoredOptions($option)
  1053. {
  1054. $this->getCollection()->createIndex(['foo' => 1]);
  1055. $expected = [
  1056. 'createdCollectionAutomatically' => false,
  1057. 'numIndexesBefore' => 2,
  1058. 'numIndexesAfter' => 2,
  1059. 'note' => 'all indexes already exist',
  1060. 'ok' => 1.0
  1061. ];
  1062. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], [$option => true]));
  1063. }
  1064. public static function createIndexIgnoredOptions()
  1065. {
  1066. return [
  1067. 'background' => ['background'],
  1068. 'dropDups' => ['dropDups'],
  1069. ];
  1070. }
  1071. public function testCreateIndexWithSameNameAndDifferentOptions()
  1072. {
  1073. $this->expectException(\MongoResultException::class);
  1074. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  1075. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  1076. }
  1077. public function testEnsureIndex()
  1078. {
  1079. $expected = [
  1080. 'createdCollectionAutomatically' => true,
  1081. 'numIndexesBefore' => 1,
  1082. 'numIndexesAfter' => 2,
  1083. 'ok' => 1.0
  1084. ];
  1085. $collection = $this->getCollection();
  1086. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  1087. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1088. $indexes = iterator_to_array($newCollection->listIndexes());
  1089. $this->assertCount(2, $indexes);
  1090. $index = $indexes[1];
  1091. $this->assertSame(['bar' => 1], $index->getKey());
  1092. $this->assertTrue($index->isUnique());
  1093. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  1094. }
  1095. public function testEnsureIndexAlreadyExists()
  1096. {
  1097. $collection = $this->getCollection();
  1098. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  1099. $expected = [
  1100. 'createdCollectionAutomatically' => false,
  1101. 'numIndexesBefore' => 2,
  1102. 'numIndexesAfter' => 2,
  1103. 'ok' => 1.0,
  1104. 'note' => 'all indexes already exist',
  1105. ];
  1106. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  1107. }
  1108. public function testEnsureIndexAlreadyExistsWithDifferentOptions()
  1109. {
  1110. $collection = $this->getCollection();
  1111. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  1112. $this->expectException(\MongoResultException::class);
  1113. $this->expectExceptionMessage('Index with name: bar_1 already exists with different options');
  1114. $collection->ensureIndex(['bar' => 1]);
  1115. }
  1116. public function testDeleteIndexUsingIndexName()
  1117. {
  1118. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1119. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  1120. $expected = [
  1121. 'nIndexesWas' => 2,
  1122. 'errmsg' => 'index not found with name [bar_1]',
  1123. 'ok' => 0.0,
  1124. ];
  1125. if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
  1126. $expected['code'] = 27;
  1127. }
  1128. // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
  1129. $this->assertArraySubset($expected, $this->getCollection()->deleteIndex('bar'));
  1130. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  1131. }
  1132. public function testDeleteIndexUsingField()
  1133. {
  1134. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1135. $newCollection->createIndex(['bar' => 1]);
  1136. $expected = [
  1137. 'nIndexesWas' => 2,
  1138. 'ok' => 1.0,
  1139. ];
  1140. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  1141. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  1142. }
  1143. public function testDeleteIndexUsingKeys()
  1144. {
  1145. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1146. $newCollection->createIndex(['bar' => 1]);
  1147. $expected = [
  1148. 'nIndexesWas' => 2,
  1149. 'ok' => 1.0,
  1150. ];
  1151. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  1152. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  1153. }
  1154. public function testDeleteIndexes()
  1155. {
  1156. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1157. $newCollection->createIndex(['bar' => 1]);
  1158. $expected = [
  1159. 'nIndexesWas' => 2,
  1160. 'msg' => 'non-_id indexes dropped for collection',
  1161. 'ok' => 1.0,
  1162. ];
  1163. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  1164. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  1165. }
  1166. public function testDeleteIndexesForNonExistingCollection()
  1167. {
  1168. $expected = [
  1169. 'ok' => 0.0,
  1170. 'errmsg' => 'ns not found',
  1171. ];
  1172. if (version_compare($this->getServerVersion(), '3.4.0', '>=')) {
  1173. $expected['code'] = 26;
  1174. }
  1175. // Using assertArraySubset because newer versions (3.4.7?) also return `codeName`
  1176. $this->assertArraySubset($expected, $this->getCollection('nonExisting')->deleteIndexes());
  1177. }
  1178. public function dataGetIndexInfo()
  1179. {
  1180. $indexVersion = $this->getDefaultIndexVersion();
  1181. return [
  1182. 'plainIndex' => [
  1183. 'expectedIndex' => [
  1184. 'v' => $indexVersion,
  1185. 'key' => ['foo' => 1],
  1186. 'name' => 'foo_1',
  1187. 'ns' => 'mongo-php-adapter.test',
  1188. ],
  1189. 'fields' => ['foo' => 1],
  1190. 'options' => [],
  1191. ],
  1192. 'uniqueIndex' => [
  1193. 'expectedIndex' => [
  1194. 'v' => $indexVersion,
  1195. 'key' => ['foo' => 1],
  1196. 'name' => 'foo_1',
  1197. 'ns' => 'mongo-php-adapter.test',
  1198. 'unique' => true,
  1199. ],
  1200. 'fields' => ['foo' => 1],
  1201. 'options' => ['unique' => true],
  1202. ],
  1203. 'sparseIndex' => [
  1204. 'expectedIndex' => [
  1205. 'v' => $indexVersion,
  1206. 'key' => ['foo' => 1],
  1207. 'name' => 'foo_1',
  1208. 'ns' => 'mongo-php-adapter.test',
  1209. 'sparse' => true,
  1210. ],
  1211. 'fields' => ['foo' => 1],
  1212. 'options' => ['sparse' => true],
  1213. ],
  1214. 'ttlIndex' => [
  1215. 'expectedIndex' => [
  1216. 'v' => $indexVersion,
  1217. 'key' => ['foo' => 1],
  1218. 'name' => 'foo_1',
  1219. 'ns' => 'mongo-php-adapter.test',
  1220. 'expireAfterSeconds' => 86400,
  1221. ],
  1222. 'fields' => ['foo' => 1],
  1223. 'options' => ['expireAfterSeconds' => 86400],
  1224. ],
  1225. 'textIndex' => [
  1226. 'expectedIndex' => [
  1227. 'v' => $indexVersion,
  1228. 'key' => [
  1229. '_fts' => 'text',
  1230. '_ftsx' => 1,
  1231. ],
  1232. 'name' => 'foo_text',
  1233. 'ns' => 'mongo-php-adapter.test',
  1234. 'weights' => [
  1235. 'foo' => 1,
  1236. ],
  1237. 'default_language' => 'english',
  1238. 'language_override' => 'language',
  1239. 'textIndexVersion' => version_compare($this->getServerVersion(), '3.2.0', '>=') ? 3 : 2,
  1240. ],
  1241. 'fields' => ['foo' => 'text'],
  1242. 'options' => [],
  1243. ],
  1244. 'partialFilterExpression' => [
  1245. 'expectedIndex' => [
  1246. 'v' => $indexVersion,
  1247. 'key' => ['foo' => 1],
  1248. 'name' => 'foo_1',
  1249. 'ns' => 'mongo-php-adapter.test',
  1250. 'partialFilterExpression' => [
  1251. 'bar' => ['$gt' => 1],
  1252. ],
  1253. ],
  1254. 'fields' => ['foo' => 1],
  1255. 'options' => [
  1256. 'partialFilterExpression' => ['bar' => ['$gt' => 1]],
  1257. ],
  1258. ],
  1259. 'geoSpatial' => [
  1260. 'expectedIndex' => [
  1261. 'v' => $indexVersion,
  1262. 'key' => ['foo' => '2dsphere'],
  1263. 'name' => 'foo_2dsphere',
  1264. 'ns' => 'mongo-php-adapter.test',
  1265. '2dsphereIndexVersion' => version_compare($this->getServerVersion(), '3.2.0', '>=') ? 3 : 2,
  1266. ],
  1267. 'fields' => ['foo' => '2dsphere'],
  1268. 'options' => [],
  1269. ],
  1270. 'geoHaystack' => [
  1271. 'expectedIndex' => [
  1272. 'v' => $indexVersion,
  1273. 'key' => ['foo' => 'geoHaystack', 'bar' => 1],
  1274. 'name' => 'foo_geoHaystack_bar_1',
  1275. 'ns' => 'mongo-php-adapter.test',
  1276. 'bucketSize' => 10,
  1277. ],
  1278. 'fields' => ['foo' => 'geoHaystack', 'bar' => 1],
  1279. 'options' => ['bucketSize' => 10],
  1280. ],
  1281. ];
  1282. }
  1283. /**
  1284. * @dataProvider dataGetIndexInfo
  1285. */
  1286. public function testGetIndexInfo($expectedIndex, $fields, $options)
  1287. {
  1288. $idIndex = [
  1289. 'v' => $this->getDefaultIndexVersion(),
  1290. 'key' => ['_id' => 1],
  1291. 'name' => '_id_',
  1292. 'ns' => 'mongo-php-adapter.test',
  1293. ];
  1294. $expectedIndexInfo = [$idIndex, $expectedIndex];
  1295. $collection = $this->getCollection();
  1296. $collection->createIndex($fields, $options);
  1297. $this->assertEquals(
  1298. $expectedIndexInfo,
  1299. $collection->getIndexInfo()
  1300. );
  1301. }
  1302. public function testFindAndModifyUpdate()
  1303. {
  1304. $id = '54203e08d51d4a1f868b456e';
  1305. $collection = $this->getCollection();
  1306. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1307. $collection->insert($document);
  1308. $document = $collection->findAndModify(
  1309. ['_id' => new \MongoId($id)],
  1310. ['$set' => ['foo' => 'foo']]
  1311. );
  1312. $this->assertSame('bar', $document['foo']);
  1313. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1314. $this->assertSame(1, $newCollection->count());
  1315. $object = $newCollection->findOne();
  1316. $this->assertNotNull($object);
  1317. $this->assertAttributeSame('foo', 'foo', $object);
  1318. }
  1319. public function testFindAndModifyUpdateWithUpdateOptions()
  1320. {
  1321. $id = '54203e08d51d4a1f868b456e';
  1322. $collection = $this->getCollection();
  1323. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1324. $collection->insert($document);
  1325. $document = $collection->findAndModify(
  1326. ['_id' => new \MongoId($id)],
  1327. [],
  1328. [],
  1329. [
  1330. 'update' => ['bar' => 'foo']
  1331. ]
  1332. );
  1333. $this->assertSame('bar', $document['foo']);
  1334. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1335. $this->assertSame(1, $newCollection->count());
  1336. $object = $newCollection->findOne();
  1337. $this->assertNotNull($object);
  1338. $this->assertAttributeSame('foo', 'bar', $object);
  1339. $this->assertObjectNotHasAttribute('foo', $object);
  1340. }
  1341. public function testFindAndModifyWithUpdateParamAndOption()
  1342. {
  1343. $id = '54203e08d51d4a1f868b456e';
  1344. $collection = $this->getCollection();
  1345. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1346. $collection->insert($document);
  1347. $data = ['foo' => 'foo', 'bar' => 'bar'];
  1348. $this->getCollection()->findAndModify(
  1349. ['_id' => new \MongoId($id)],
  1350. [$data],
  1351. [],
  1352. [
  1353. 'update' => ['$set' => ['foo' => 'foobar']],
  1354. 'upsert' => true,
  1355. ]
  1356. );
  1357. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1358. $this->assertSame(1, $newCollection->count());
  1359. $object = $newCollection->findOne();
  1360. $this->assertNotNull($object);
  1361. $this->assertAttributeSame('foobar', 'foo', $object);
  1362. $this->assertObjectNotHasAttribute('bar', $object);
  1363. }
  1364. public function testFindAndModifyUpdateReplace()
  1365. {
  1366. $id = '54203e08d51d4a1f868b456e';
  1367. $collection = $this->getCollection();
  1368. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1369. $collection->insert($document);
  1370. $document = $collection->findAndModify(
  1371. ['_id' => new \MongoId($id)],
  1372. ['_id' => new \MongoId($id), 'foo' => 'boo']
  1373. );
  1374. $this->assertSame('bar', $document['foo']);
  1375. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1376. $this->assertSame(1, $newCollection->count());
  1377. $object = $newCollection->findOne();
  1378. $this->assertNotNull($object);
  1379. $this->assertAttributeSame('boo', 'foo', $object);
  1380. $this->assertObjectNotHasAttribute('bar', $object);
  1381. }
  1382. public function testFindAndModifyUpdateReturnNew()
  1383. {
  1384. $id = '54203e08d51d4a1f868b456e';
  1385. $collection = $this->getCollection();
  1386. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1387. $collection->insert($document);
  1388. $document = $collection->findAndModify(
  1389. ['_id' => new \MongoId($id)],
  1390. ['$set' => ['foo' => 'foo']],
  1391. null,
  1392. ['new' => true]
  1393. );
  1394. $this->assertSame('foo', $document['foo']);
  1395. }
  1396. public function testFindAndModifyWithFields()
  1397. {
  1398. $id = '54203e08d51d4a1f868b456e';
  1399. $collection = $this->getCollection();
  1400. $document = [
  1401. '_id' => new \MongoId($id),
  1402. 'foo' => 'bar',
  1403. 'bar' => 'foo',
  1404. ];
  1405. $collection->insert($document);
  1406. $document = $collection->findAndModify(
  1407. ['_id' => new \MongoId($id)],
  1408. ['$set' => ['foo' => 'foo']],
  1409. ['foo' => true]
  1410. );
  1411. $this->assertArrayNotHasKey('bar', $document);
  1412. $this->assertArrayHasKey('foo', $document);
  1413. }
  1414. public function testGroup()
  1415. {
  1416. $this->skipTestIf(version_compare($this->getServerVersion(), '4.2.0', '>='), 'Test does not apply to MongoDB >= 4.2.');
  1417. $collection = $this->getCollection();
  1418. $document1 = ['a' => 2];
  1419. $collection->insert($document1);
  1420. $document2 = ['b' => 5];
  1421. $collection->insert($document2);
  1422. $document3 = ['a' => 1];
  1423. $collection->insert($document3);
  1424. $keys = [];
  1425. $initial = ["count" => 0];
  1426. $reduce = "function (obj, prev) { prev.count++; }";
  1427. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  1428. $result = $collection->group($keys, $initial, $reduce, $condition);
  1429. $this->assertArraySubset(
  1430. [
  1431. 'retval' => [['count' => 1.0]],
  1432. 'count' => 1.0,
  1433. 'keys' => 1,
  1434. 'ok' => 1.0,
  1435. ],
  1436. $result
  1437. );
  1438. }
  1439. public function testMapReduce()
  1440. {
  1441. $data = array(
  1442. array(
  1443. 'username' => 'jones',
  1444. 'likes' => 20.0,
  1445. 'text' => 'Hello world!'
  1446. ),
  1447. array(
  1448. 'username' => 'bob',
  1449. 'likes' => 100.0,
  1450. 'text' => 'Hello world!'
  1451. ),
  1452. array(
  1453. 'username' => 'bob',
  1454. 'likes' => 100.0,
  1455. 'text' => 'Hello world!'
  1456. ),
  1457. );
  1458. $collection = $this->getCollection();
  1459. $collection->batchInsert($data);
  1460. $map = 'function() {
  1461. emit(this.username, { count: 1, likes: this.likes });
  1462. }';
  1463. $reduce = 'function(key, values) {
  1464. var result = {count: 0, likes: 0};
  1465. values.forEach(function(value) {
  1466. result.count += value.count;
  1467. result.likes += value.likes;
  1468. });
  1469. return result;
  1470. }';
  1471. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  1472. $command = [
  1473. 'mapreduce' => $this->getCollection()->getName(),
  1474. 'map' => new \MongoCode($map),
  1475. 'reduce' => new \MongoCode($reduce),
  1476. 'query' => (object) [],
  1477. 'out' => ['inline' => true],
  1478. 'finalize' => new \MongoCode($finalize),
  1479. ];
  1480. $result = $this->getDatabase()->command($command);
  1481. $expected = [
  1482. [
  1483. '_id' => 'bob',
  1484. 'value' => [
  1485. 'count' => 2.0,
  1486. 'likes' => 200.0,
  1487. 'test' => 'test',
  1488. ],
  1489. ],
  1490. [
  1491. '_id' => 'jones',
  1492. 'value' => [
  1493. 'count' => 1.0,
  1494. 'likes' => 20.0,
  1495. 'test' => 'test',
  1496. ],
  1497. ],
  1498. ];
  1499. $this->assertSame(1.0, $result['ok']);
  1500. $this->assertSame($expected, $result['results']);
  1501. }
  1502. public function testFindAndModifyResultException()
  1503. {
  1504. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  1505. $collection = $this->getCollection();
  1506. $this->expectException(\MongoResultException::class);
  1507. $collection->findAndModify(
  1508. array("inprogress" => false, "name" => "Next promo"),
  1509. array('$unsupportedOperator' => array("tasks" => -1)),
  1510. array("tasks" => true),
  1511. array("new" => true)
  1512. );
  1513. }
  1514. public function testFindAndModifyExceptionTimeout()
  1515. {
  1516. $this->failMaxTimeMS();
  1517. $id = '54203e08d51d4a1f868b456e';
  1518. $collection = $this->getCollection();
  1519. $this->expectException(\MongoExecutionTimeoutException::class);
  1520. $document = $collection->findAndModify(
  1521. ['_id' => new \MongoId($id)],
  1522. null,
  1523. null,
  1524. ['maxTimeMS' => 1, 'remove' => true]
  1525. );
  1526. }
  1527. public function testFindAndModifyRemove()
  1528. {
  1529. $id = '54203e08d51d4a1f868b456e';
  1530. $collection = $this->getCollection();
  1531. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1532. $collection->insert($document);
  1533. $document = $collection->findAndModify(
  1534. ['_id' => new \MongoId($id)],
  1535. null,
  1536. null,
  1537. ['remove' => true]
  1538. );
  1539. $this->assertEquals('bar', $document['foo']);
  1540. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1541. $this->assertSame(0, $newCollection->count());
  1542. }
  1543. public function testValidate()
  1544. {
  1545. $collection = $this->getCollection();
  1546. $document = ['foo' => 'bar'];
  1547. $collection->insert($document);
  1548. $result = $collection->validate();
  1549. $this->assertArraySubset(
  1550. [
  1551. 'ns' => 'mongo-php-adapter.test',
  1552. 'nrecords' => 1,
  1553. 'nIndexes' => 1,
  1554. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  1555. 'valid' => true,
  1556. 'errors' => [],
  1557. ],
  1558. $result
  1559. );
  1560. }
  1561. public function testDrop()
  1562. {
  1563. $document = ['foo' => 'bar'];
  1564. $this->getCollection()->insert($document);
  1565. $expected = [
  1566. 'ns' => (string) $this->getCollection(),
  1567. 'nIndexesWas' => 1,
  1568. 'ok' => 1.0
  1569. ];
  1570. $this->assertSame($expected, $this->getCollection()->drop());
  1571. }
  1572. public function testEmptyCollectionName()
  1573. {
  1574. $this->expectException(\Exception::class);
  1575. $this->expectExceptionMessage('Collection name cannot be empty');
  1576. new \MongoCollection($this->getDatabase(), '');
  1577. }
  1578. public function testSelectCollectionWithNullBytes()
  1579. {
  1580. $this->expectException(\Exception::class);
  1581. $this->expectExceptionMessage('Collection name cannot contain null bytes');
  1582. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1583. }
  1584. public function testSubCollectionWithNullBytes()
  1585. {
  1586. $collection = $this->getCollection();
  1587. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1588. $this->assertSame('test', $collection->getName());
  1589. }
  1590. public function testSelectCollectionWithDatabaseObject()
  1591. {
  1592. $client = $this->getClient();
  1593. $database = $this->getDatabase($client);
  1594. $collection = $client->selectCollection($database, 'test');
  1595. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  1596. }
  1597. public function testHasNextLoop()
  1598. {
  1599. $collection = $this->getCollection();
  1600. for ($i = 0; $i < 5; $i++) {
  1601. $document = ['i' => $i];
  1602. $collection->insert($document);
  1603. }
  1604. $cursor = $collection->find()->sort(['i' => 1]);
  1605. $data = [];
  1606. $i = 0;
  1607. while ($cursor->hasNext()) {
  1608. $this->assertSame($i < 5, $cursor->hasNext());
  1609. $row = $cursor->getNext();
  1610. $this->assertSame($i, $row['i']);
  1611. $data[] = $row;
  1612. $i++;
  1613. }
  1614. $this->assertCount(5, $data);
  1615. }
  1616. public function testProjectionWithBSONTypes()
  1617. {
  1618. $collection = $this->getCollection();
  1619. $id = new \MongoId();
  1620. $referencedId = new \MongoId();
  1621. $data = [
  1622. '_id' => $id,
  1623. 'loveItems' => [
  1624. [
  1625. 'sellable' => [
  1626. '$ref' => 'sellables',
  1627. '$id' => $referencedId,
  1628. ]
  1629. ],
  1630. [
  1631. 'sellable' => [
  1632. '$ref' => 'sellables',
  1633. '$id' => new \MongoId(),
  1634. ]
  1635. ]
  1636. ]
  1637. ];
  1638. $collection->insert($data);
  1639. $item = $collection->findOne(
  1640. ['_id' => $id],
  1641. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1642. );
  1643. $this->assertArrayHasKey('loveItems', $item);
  1644. $this->assertCount(1, $item['loveItems']);
  1645. $cursor = $collection->find(
  1646. ['_id' => $id],
  1647. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1648. );
  1649. $items = iterator_to_array($cursor, false);
  1650. $this->assertCount(1, $items);
  1651. $this->assertCount(1, $items[0]['loveItems']);
  1652. }
  1653. public static function dataFindWithRegex()
  1654. {
  1655. return [
  1656. 'MongoRegex' => [new \MongoRegex('/^foo.*/i')],
  1657. 'BSONRegex' => [new Regex('^foo.*', 'i')],
  1658. ];
  1659. }
  1660. /**
  1661. * @dataProvider dataFindWithRegex
  1662. */
  1663. public function testFindWithRegex($regex)
  1664. {
  1665. $this->skipTestIf(extension_loaded('mongo'));
  1666. $document = ['name' => 'FOO 123'];
  1667. $this->getCollection()->insert($document);
  1668. $cursor = $this->getCollection()->find(['name' => $regex]);
  1669. $this->assertSame(1, $cursor->count());
  1670. }
  1671. }
  1672. class PrivatePropertiesStub
  1673. {
  1674. private $foo = 'bar';
  1675. }
  1676. class ArrayObjectWithProtectedProperties extends ArrayObject
  1677. {
  1678. protected $something = 'baz';
  1679. }