MongoCollectionTest.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  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. 'valid' => true,
  1555. 'errors' => [],
  1556. ],
  1557. $result
  1558. );
  1559. }
  1560. public function testDrop()
  1561. {
  1562. $document = ['foo' => 'bar'];
  1563. $this->getCollection()->insert($document);
  1564. $expected = [
  1565. 'ns' => (string) $this->getCollection(),
  1566. 'nIndexesWas' => 1,
  1567. 'ok' => 1.0
  1568. ];
  1569. $this->assertEquals($expected, $this->getCollection()->drop());
  1570. }
  1571. public function testEmptyCollectionName()
  1572. {
  1573. $this->expectException(\Exception::class);
  1574. $this->expectExceptionMessage('Collection name cannot be empty');
  1575. new \MongoCollection($this->getDatabase(), '');
  1576. }
  1577. public function testSelectCollectionWithNullBytes()
  1578. {
  1579. $this->expectException(\Exception::class);
  1580. $this->expectExceptionMessage('Collection name cannot contain null bytes');
  1581. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1582. }
  1583. public function testSubCollectionWithNullBytes()
  1584. {
  1585. $collection = $this->getCollection();
  1586. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1587. $this->assertSame('test', $collection->getName());
  1588. }
  1589. public function testSelectCollectionWithDatabaseObject()
  1590. {
  1591. $client = $this->getClient();
  1592. $database = $this->getDatabase($client);
  1593. $collection = $client->selectCollection($database, 'test');
  1594. $this->assertSame('mongo-php-adapter.test', (string) $collection);
  1595. }
  1596. public function testHasNextLoop()
  1597. {
  1598. $collection = $this->getCollection();
  1599. for ($i = 0; $i < 5; $i++) {
  1600. $document = ['i' => $i];
  1601. $collection->insert($document);
  1602. }
  1603. $cursor = $collection->find()->sort(['i' => 1]);
  1604. $data = [];
  1605. $i = 0;
  1606. while ($cursor->hasNext()) {
  1607. $this->assertSame($i < 5, $cursor->hasNext());
  1608. $row = $cursor->getNext();
  1609. $this->assertSame($i, $row['i']);
  1610. $data[] = $row;
  1611. $i++;
  1612. }
  1613. $this->assertCount(5, $data);
  1614. }
  1615. public function testProjectionWithBSONTypes()
  1616. {
  1617. $collection = $this->getCollection();
  1618. $id = new \MongoId();
  1619. $referencedId = new \MongoId();
  1620. $data = [
  1621. '_id' => $id,
  1622. 'loveItems' => [
  1623. [
  1624. 'sellable' => [
  1625. '$ref' => 'sellables',
  1626. '$id' => $referencedId,
  1627. ]
  1628. ],
  1629. [
  1630. 'sellable' => [
  1631. '$ref' => 'sellables',
  1632. '$id' => new \MongoId(),
  1633. ]
  1634. ]
  1635. ]
  1636. ];
  1637. $collection->insert($data);
  1638. $item = $collection->findOne(
  1639. ['_id' => $id],
  1640. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1641. );
  1642. $this->assertArrayHasKey('loveItems', $item);
  1643. $this->assertCount(1, $item['loveItems']);
  1644. $cursor = $collection->find(
  1645. ['_id' => $id],
  1646. ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
  1647. );
  1648. $items = iterator_to_array($cursor, false);
  1649. $this->assertCount(1, $items);
  1650. $this->assertCount(1, $items[0]['loveItems']);
  1651. }
  1652. public static function dataFindWithRegex()
  1653. {
  1654. return [
  1655. 'MongoRegex' => [new \MongoRegex('/^foo.*/i')],
  1656. 'BSONRegex' => [new Regex('^foo.*', 'i')],
  1657. ];
  1658. }
  1659. /**
  1660. * @dataProvider dataFindWithRegex
  1661. */
  1662. public function testFindWithRegex($regex)
  1663. {
  1664. $this->skipTestIf(extension_loaded('mongo'));
  1665. $document = ['name' => 'FOO 123'];
  1666. $this->getCollection()->insert($document);
  1667. $cursor = $this->getCollection()->find(['name' => $regex]);
  1668. $this->assertSame(1, $cursor->count());
  1669. }
  1670. }
  1671. class PrivatePropertiesStub
  1672. {
  1673. private $foo = 'bar';
  1674. }
  1675. class ArrayObjectWithProtectedProperties extends ArrayObject
  1676. {
  1677. protected $something = 'baz';
  1678. }