MongoCollectionTest.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use MongoDB\Driver\ReadPreference;
  4. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  5. /**
  6. * @author alcaeus <alcaeus@alcaeus.org>
  7. */
  8. class MongoCollectionTest extends TestCase
  9. {
  10. public function testSerialize()
  11. {
  12. $this->assertInternalType('string', serialize($this->getCollection()));
  13. }
  14. public function testGetNestedCollections()
  15. {
  16. $collection = $this->getCollection()->foo->bar;
  17. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  18. }
  19. public function testCreateRecord()
  20. {
  21. $collection = $this->getCollection();
  22. $expected = [
  23. 'ok' => 1.0,
  24. 'n' => 0,
  25. 'err' => null,
  26. 'errmsg' => null,
  27. ];
  28. $document = ['foo' => 'bar'];
  29. $this->assertSame($expected, $collection->insert($document));
  30. $this->assertInstanceOf('MongoId', $document['_id']);
  31. $id = (string) $document['_id'];
  32. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  33. $this->assertSame(1, $newCollection->count());
  34. $object = $newCollection->findOne();
  35. $this->assertNotNull($object);
  36. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  37. $this->assertSame($id, (string) $object->_id);
  38. $this->assertObjectHasAttribute('foo', $object);
  39. $this->assertAttributeSame('bar', 'foo', $object);
  40. }
  41. public function testInsertInvalidData()
  42. {
  43. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
  44. $document = 8;
  45. $this->getCollection()->insert($document);
  46. }
  47. public function testInsertEmptyArray()
  48. {
  49. $document = [];
  50. $this->getCollection()->insert($document);
  51. $this->assertSame(1, $this->getCollection()->count());
  52. }
  53. public function testInsertArrayWithNumericKeys()
  54. {
  55. $document = [1 => 'foo'];
  56. $this->getCollection()->insert($document);
  57. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  58. }
  59. public function testInsertEmptyObject()
  60. {
  61. $document = (object) [];
  62. $this->getCollection()->insert($document);
  63. $this->assertSame(1, $this->getCollection()->count());
  64. }
  65. public function testInsertObjectWithPrivateProperties()
  66. {
  67. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  68. $document = new PrivatePropertiesStub();
  69. $this->getCollection()->insert($document);
  70. }
  71. public function testInsertWithInvalidKey()
  72. {
  73. $document = ['*' => 'foo'];
  74. $this->getCollection()->insert($document);
  75. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  76. }
  77. public function testInsertWithEmptyKey()
  78. {
  79. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  80. $document = ['' => 'foo'];
  81. $this->getCollection()->insert($document);
  82. }
  83. public function testInsertWithNumericKey()
  84. {
  85. $document = ['foo'];
  86. $this->getCollection()->insert($document);
  87. $this->assertSame(1, $this->getCollection()->count(['foo']));
  88. }
  89. public function testInsertDuplicate()
  90. {
  91. $collection = $this->getCollection();
  92. $collection->createIndex(['foo' => 1], ['unique' => true]);
  93. $document = ['foo' => 'bar'];
  94. $collection->insert($document);
  95. unset($document['_id']);
  96. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
  97. $collection->insert($document);
  98. }
  99. public function testUnacknowledgedWrite()
  100. {
  101. $document = ['foo' => 'bar'];
  102. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  103. }
  104. public function testInsertWriteConcernException()
  105. {
  106. $this->setExpectedException(
  107. 'MongoWriteConcernException',
  108. "cannot use 'w' > 1 when a host is not replicated"
  109. );
  110. $document = ['foo' => 'bar'];
  111. $this->getCollection()->insert($document, ['w' => 2]);
  112. }
  113. public function testInsertMany()
  114. {
  115. $expected = [
  116. 'ok' => 1.0,
  117. 'n' => 0,
  118. 'syncMillis' => 0,
  119. 'writtenTo' => null,
  120. 'err' => null,
  121. ];
  122. $documents = [
  123. ['foo' => 'bar'],
  124. ['bar' => 'foo']
  125. ];
  126. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  127. foreach ($documents as $document) {
  128. $this->assertInstanceOf('MongoId', $document['_id']);
  129. }
  130. }
  131. public function testInsertManyWithNonNumericKeys()
  132. {
  133. $expected = [
  134. 'ok' => 1.0,
  135. 'n' => 0,
  136. 'syncMillis' => 0,
  137. 'writtenTo' => null,
  138. 'err' => null,
  139. ];
  140. $documents = [
  141. 'a' => ['foo' => 'bar'],
  142. 'b' => ['bar' => 'foo']
  143. ];
  144. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  145. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  146. $this->assertSame(2, $newCollection->count());
  147. }
  148. public function testBatchInsertContinuesOnError()
  149. {
  150. $expected = [
  151. 'ok' => 1.0,
  152. 'n' => 0,
  153. 'syncMillis' => 0,
  154. 'writtenTo' => null,
  155. 'err' => null,
  156. ];
  157. $documents = [
  158. 8,
  159. 'b' => ['bar' => 'foo']
  160. ];
  161. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  162. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  163. $this->assertSame(1, $newCollection->count());
  164. }
  165. public function testBatchInsertException()
  166. {
  167. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
  168. $id = new \MongoId();
  169. $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
  170. $this->getCollection()->batchInsert($documents);
  171. }
  172. public function testBatchInsertObjectWithPrivateProperties()
  173. {
  174. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  175. $documents = [new PrivatePropertiesStub()];
  176. $this->getCollection()->batchInsert($documents);
  177. }
  178. public function testBatchInsertWithInvalidKey()
  179. {
  180. $documents = [['*' => 'foo']];
  181. $this->getCollection()->batchInsert($documents);
  182. $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
  183. }
  184. public function testBatchInsertWithEmptyKey()
  185. {
  186. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  187. $documents = [['' => 'foo']];
  188. $this->getCollection()->batchInsert($documents);
  189. }
  190. public function testBatchInsertWithNumericKey()
  191. {
  192. $documents = [['foo']];
  193. $this->getCollection()->batchInsert($documents);
  194. $this->assertSame(1, $this->getCollection()->count(['foo']));
  195. }
  196. public function testBatchInsertEmptyBatchException()
  197. {
  198. $this->setExpectedException('MongoException', 'No write ops were included in the batch');
  199. $documents = [];
  200. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  201. }
  202. public function testUpdateWriteConcern()
  203. {
  204. $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
  205. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  206. }
  207. public function testUpdateOne()
  208. {
  209. $document = ['foo' => 'bar'];
  210. $this->getCollection()->insert($document);
  211. // Unset ID to re-insert
  212. unset($document['_id']);
  213. $this->getCollection()->insert($document);
  214. $expected = [
  215. 'ok' => 1.0,
  216. 'nModified' => 1,
  217. 'n' => 1,
  218. 'err' => null,
  219. 'errmsg' => null,
  220. 'updatedExisting' => true,
  221. ];
  222. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  223. $this->assertSame($expected, $result);
  224. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  225. }
  226. public function testUpdateReplaceOne()
  227. {
  228. $document = ['foo' => 'bar', 'bar' => 'foo'];
  229. $this->getCollection()->insert($document);
  230. // Unset ID to re-insert
  231. unset($document['_id']);
  232. $this->getCollection()->insert($document);
  233. $expected = [
  234. 'ok' => 1.0,
  235. 'nModified' => 1,
  236. 'n' => 1,
  237. 'err' => null,
  238. 'errmsg' => null,
  239. 'updatedExisting' => true,
  240. ];
  241. $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
  242. $this->assertSame($expected, $result);
  243. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  244. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
  245. }
  246. public function testUpdateReplaceMultiple()
  247. {
  248. $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
  249. $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
  250. }
  251. public function testUpdateDuplicate()
  252. {
  253. $collection = $this->getCollection();
  254. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  255. $document = ['foo' => 'bar'];
  256. $collection->insert($document);
  257. $document = ['foo' => 'foo'];
  258. $collection->insert($document);
  259. $this->setExpectedException('MongoDuplicateKeyException');
  260. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  261. }
  262. public function testUpdateMany()
  263. {
  264. $document = ['change' => true, 'foo' => 'bar'];
  265. $this->getCollection()->insert($document);
  266. unset($document['_id']);
  267. $this->getCollection()->insert($document);
  268. $document = ['change' => true, 'foo' => 'foo'];
  269. $this->getCollection()->insert($document);
  270. $expected = [
  271. 'ok' => 1.0,
  272. 'nModified' => 2,
  273. 'n' => 3,
  274. 'err' => null,
  275. 'errmsg' => null,
  276. 'updatedExisting' => true,
  277. ];
  278. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  279. $this->assertSame($expected, $result);
  280. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  281. }
  282. public function testUnacknowledgedUpdate()
  283. {
  284. $document = ['foo' => 'bar'];
  285. $this->getCollection()->insert($document);
  286. unset($document['_id']);
  287. $this->getCollection()->insert($document);
  288. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  289. }
  290. public function testRemoveMultiple()
  291. {
  292. $document = ['change' => true, 'foo' => 'bar'];
  293. $this->getCollection()->insert($document);
  294. unset($document['_id']);
  295. $this->getCollection()->insert($document);
  296. $document = ['change' => true, 'foo' => 'foo'];
  297. $this->getCollection()->insert($document);
  298. $expected = [
  299. 'ok' => 1.0,
  300. 'n' => 2,
  301. 'err' => null,
  302. 'errmsg' => null,
  303. ];
  304. $result = $this->getCollection()->remove(['foo' => 'bar']);
  305. $this->assertSame($expected, $result);
  306. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  307. }
  308. public function testRemoveSingle()
  309. {
  310. $document = ['change' => true, 'foo' => 'bar'];
  311. $this->getCollection()->insert($document);
  312. unset($document['_id']);
  313. $this->getCollection()->insert($document);
  314. unset($document['_id']);
  315. $this->getCollection()->insert($document);
  316. $expected = [
  317. 'ok' => 1.0,
  318. 'n' => 1,
  319. 'err' => null,
  320. 'errmsg' => null,
  321. ];
  322. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  323. $this->assertSame($expected, $result);
  324. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  325. }
  326. public function testRemoveUnacknowledged()
  327. {
  328. $document = ['change' => true, 'foo' => 'bar'];
  329. $this->getCollection()->insert($document);
  330. unset($document['_id']);
  331. $this->getCollection()->insert($document);
  332. unset($document['_id']);
  333. $this->getCollection()->insert($document);
  334. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  335. }
  336. public function testFindReturnsCursor()
  337. {
  338. $this->prepareData();
  339. $collection = $this->getCollection();
  340. $this->assertInstanceOf('MongoCursor', $collection->find());
  341. }
  342. public function testCount()
  343. {
  344. $this->prepareData();
  345. $collection = $this->getCollection();
  346. $this->assertSame(3, $collection->count());
  347. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  348. }
  349. public function testCountWithLimit()
  350. {
  351. $this->prepareData();
  352. $collection = $this->getCollection();
  353. $this->assertSame(2, $collection->count([], ['limit' => 2]));
  354. $this->assertSame(1, $collection->count(['foo' => 'bar'], ['limit' => 1]));
  355. }
  356. public function testCountWithLimitLegacy()
  357. {
  358. $this->prepareData();
  359. $collection = $this->getCollection();
  360. $this->assertSame(2, $collection->count([], 2));
  361. $this->assertSame(1, $collection->count(['foo' => 'bar'], 1));
  362. }
  363. public function testCountWithSkip()
  364. {
  365. $this->prepareData();
  366. $collection = $this->getCollection();
  367. $this->assertSame(2, $collection->count([], ['skip' => 1]));
  368. $this->assertSame(1, $collection->count(['foo' => 'bar'], ['skip' => 1]));
  369. }
  370. public function testCountWithSkipLegacy()
  371. {
  372. $this->prepareData();
  373. $collection = $this->getCollection();
  374. $this->assertSame(2, $collection->count([], null, 1));
  375. $this->assertSame(1, $collection->count(['foo' => 'bar'], null, 1));
  376. }
  377. public function testCountWithLimitAndSkip()
  378. {
  379. $this->prepareData();
  380. $collection = $this->getCollection();
  381. $this->assertSame(2, $collection->count([], ['skip' => 1, 'limit' => 2]));
  382. $this->assertSame(1, $collection->count([], ['skip' => 1, 'limit' => 1]));
  383. }
  384. public function testCountWithLimitAndSkipLegacy()
  385. {
  386. $this->prepareData();
  387. $collection = $this->getCollection();
  388. $this->assertSame(2, $collection->count([], 2, 1));
  389. $this->assertSame(1, $collection->count([], 1, 1));
  390. }
  391. public function testCountTimeout()
  392. {
  393. $this->failMaxTimeMS();
  394. $this->setExpectedException('MongoExecutionTimeoutException');
  395. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  396. }
  397. public function testFindOne()
  398. {
  399. $this->prepareData();
  400. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  401. $this->assertSame(['foo' => 'foo'], $document);
  402. }
  403. public function testFindOneNotFound()
  404. {
  405. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  406. $this->assertNull($document);
  407. }
  408. public function testFindOneConnectionIssue()
  409. {
  410. $this->setExpectedException('MongoConnectionException');
  411. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  412. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  413. $collection->findOne();
  414. }
  415. public function testDistinct()
  416. {
  417. $this->prepareData();
  418. $values = $this->getCollection()->distinct('foo');
  419. $this->assertInternalType('array', $values);
  420. sort($values);
  421. $this->assertEquals(['bar', 'foo'], $values);
  422. }
  423. public function testDistinctWithQuery()
  424. {
  425. $this->prepareData();
  426. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  427. $this->assertInternalType('array', $values);
  428. $this->assertEquals(['bar'], $values);
  429. }
  430. public function testAggregate()
  431. {
  432. $collection = $this->getCollection();
  433. $this->prepareData();
  434. $pipeline = [
  435. [
  436. '$group' => [
  437. '_id' => '$foo',
  438. 'count' => [ '$sum' => 1 ],
  439. ],
  440. ],
  441. [
  442. '$sort' => ['_id' => 1]
  443. ]
  444. ];
  445. $result = $collection->aggregate($pipeline);
  446. $this->assertInternalType('array', $result);
  447. $this->assertArrayHasKey('result', $result);
  448. $this->assertEquals([
  449. ['_id' => 'bar', 'count' => 2],
  450. ['_id' => 'foo', 'count' => 1],
  451. ], $result['result']);
  452. }
  453. public function testAggregateInvalidPipeline()
  454. {
  455. $collection = $this->getCollection();
  456. $pipeline = [
  457. [
  458. '$invalid' => []
  459. ],
  460. ];
  461. $this->setExpectedException('MongoResultException', 'Unrecognized pipeline stage name');
  462. $collection->aggregate($pipeline);
  463. }
  464. public function testAggregateTimeoutException()
  465. {
  466. $collection = $this->getCollection();
  467. $this->failMaxTimeMS();
  468. $this->setExpectedException('MongoExecutionTimeoutException');
  469. $pipeline = [
  470. [
  471. '$group' => [
  472. '_id' => '$foo',
  473. 'count' => [ '$sum' => 1 ],
  474. ],
  475. ],
  476. [
  477. '$sort' => ['_id' => 1]
  478. ]
  479. ];
  480. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  481. }
  482. public function testAggregateCursor()
  483. {
  484. $collection = $this->getCollection();
  485. $this->prepareData();
  486. $pipeline = [
  487. [
  488. '$group' => [
  489. '_id' => '$foo',
  490. 'count' => [ '$sum' => 1 ],
  491. ],
  492. ],
  493. [
  494. '$sort' => ['_id' => 1]
  495. ]
  496. ];
  497. $cursor = $collection->aggregateCursor($pipeline);
  498. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  499. $this->assertEquals([
  500. ['_id' => 'bar', 'count' => 2],
  501. ['_id' => 'foo', 'count' => 1],
  502. ], iterator_to_array($cursor));
  503. }
  504. public function testReadPreference()
  505. {
  506. $collection = $this->getCollection();
  507. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  508. $this->assertFalse($collection->getSlaveOkay());
  509. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  510. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  511. $this->assertTrue($collection->getSlaveOkay());
  512. $this->assertTrue($collection->setSlaveOkay(true));
  513. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  514. $this->assertTrue($collection->setSlaveOkay(false));
  515. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  516. }
  517. public function testReadPreferenceIsSetInDriver()
  518. {
  519. $this->skipTestIf(extension_loaded('mongo'));
  520. $collection = $this->getCollection();
  521. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  522. // Only way to check whether options are passed down is through debugInfo
  523. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  524. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  525. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  526. }
  527. public function testReadPreferenceIsInherited()
  528. {
  529. $database = $this->getDatabase();
  530. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  531. $collection = $database->selectCollection('test');
  532. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  533. }
  534. public function testWriteConcern()
  535. {
  536. $collection = $this->getCollection();
  537. $this->assertTrue($collection->setWriteConcern('majority', 100));
  538. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  539. }
  540. public function testWriteConcernIsSetInDriver()
  541. {
  542. $this->skipTestIf(extension_loaded('mongo'));
  543. $collection = $this->getCollection();
  544. $this->assertTrue($collection->setWriteConcern(2, 100));
  545. // Only way to check whether options are passed down is through debugInfo
  546. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  547. $this->assertSame(2, $writeConcern->getW());
  548. $this->assertSame(100, $writeConcern->getWtimeout());
  549. }
  550. public function testWriteConcernIsInherited()
  551. {
  552. $database = $this->getDatabase();
  553. $database->setWriteConcern('majority', 100);
  554. $collection = $database->selectCollection('test');
  555. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  556. }
  557. public function testSaveInsert()
  558. {
  559. $id = '54203e08d51d4a1f868b456e';
  560. $collection = $this->getCollection();
  561. $objectId = new \MongoId($id);
  562. $expected = [
  563. 'ok' => 1.0,
  564. 'nModified' => 0,
  565. 'n' => 1,
  566. 'err' => null,
  567. 'errmsg' => null,
  568. 'upserted' => $objectId,
  569. 'updatedExisting' => false,
  570. ];
  571. $document = ['_id' => $objectId, 'foo' => 'bar'];
  572. $this->assertEquals($expected, $collection->save($document));
  573. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  574. $this->assertSame(1, $newCollection->count());
  575. $object = $newCollection->findOne();
  576. $this->assertNotNull($object);
  577. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  578. $this->assertSame($id, (string) $object->_id);
  579. $this->assertObjectHasAttribute('foo', $object);
  580. $this->assertAttributeSame('bar', 'foo', $object);
  581. }
  582. public function testRemoveOne()
  583. {
  584. $id = '54203e08d51d4a1f868b456e';
  585. $collection = $this->getCollection();
  586. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  587. $collection->insert($document);
  588. $collection->remove(['_id' => new \MongoId($id)]);
  589. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  590. $this->assertSame(0, $newCollection->count());
  591. }
  592. public function testSaveUpdate()
  593. {
  594. $expected = [
  595. 'ok' => 1.0,
  596. 'nModified' => 1,
  597. 'n' => 1,
  598. 'err' => null,
  599. 'errmsg' => null,
  600. 'updatedExisting' => true,
  601. ];
  602. $id = '54203e08d51d4a1f868b456e';
  603. $collection = $this->getCollection();
  604. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  605. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  606. $collection->insert($insertDocument);
  607. $this->assertSame($expected, $collection->save($saveDocument));
  608. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  609. $this->assertSame(1, $newCollection->count());
  610. $object = $newCollection->findOne();
  611. $this->assertNotNull($object);
  612. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  613. $this->assertSame($id, (string) $object->_id);
  614. $this->assertObjectHasAttribute('foo', $object);
  615. $this->assertAttributeSame('foo', 'foo', $object);
  616. }
  617. public function testSavingShouldReplaceTheWholeDocument() {
  618. $id = '54203e08d51d4a1f868b456e';
  619. $collection = $this->getCollection();
  620. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  621. $saveDocument = ['_id' => new \MongoId($id)];
  622. $collection->insert($insertDocument);
  623. $collection->save($saveDocument);
  624. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  625. $this->assertSame(1, $newCollection->count());
  626. $object = $newCollection->findOne();
  627. $this->assertNotNull($object);
  628. $this->assertObjectNotHasAttribute('foo', $object);
  629. }
  630. public function testSaveDuplicate()
  631. {
  632. $collection = $this->getCollection();
  633. $collection->createIndex(['foo' => 1], ['unique' => true]);
  634. $document = ['foo' => 'bar'];
  635. $collection->save($document);
  636. $this->setExpectedException('MongoDuplicateKeyException');
  637. unset($document['_id']);
  638. $collection->save($document);
  639. }
  640. public function testSaveEmptyKeys()
  641. {
  642. $document = [];
  643. $this->getCollection()->save($document);
  644. $this->assertSame(1, $this->getCollection()->count());
  645. }
  646. public function testSaveEmptyObject()
  647. {
  648. $document = (object) [];
  649. $this->getCollection()->save($document);
  650. $this->assertSame(1, $this->getCollection()->count());
  651. }
  652. public function testGetDBRef()
  653. {
  654. $collection = $this->getCollection();
  655. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  656. $collection->insert($insertDocument);
  657. $document = $collection->getDBRef([
  658. '$ref' => 'test',
  659. '$id' => 1,
  660. ]);
  661. $this->assertEquals($insertDocument, $document);
  662. }
  663. public function testCreateDBRef()
  664. {
  665. $collection = $this->getCollection();
  666. $reference = $collection->createDBRef(['_id' => 'foo']);
  667. $this->assertSame(
  668. [
  669. '$ref' => 'test',
  670. '$id' => 'foo',
  671. ],
  672. $reference
  673. );
  674. }
  675. public function testCreateIndex()
  676. {
  677. $expected = [
  678. 'createdCollectionAutomatically' => true,
  679. 'numIndexesBefore' => 1,
  680. 'numIndexesAfter' => 2,
  681. 'ok' => 1.0,
  682. ];
  683. $collection = $this->getCollection();
  684. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  685. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  686. $iterator = $newCollection->listIndexes();
  687. $indexes = iterator_to_array($iterator);
  688. $this->assertCount(2, $indexes);
  689. $index = $indexes[1];
  690. $this->assertSame(['foo' => 1], $index->getKey());
  691. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  692. }
  693. public function testCreateIndexInvalid()
  694. {
  695. $this->setExpectedException('MongoException', 'index specification has no elements');
  696. $this->getCollection()->createIndex([]);
  697. }
  698. public function testCreateIndexTwice()
  699. {
  700. $this->getCollection()->createIndex(['foo' => 1]);
  701. $expected = [
  702. 'createdCollectionAutomatically' => false,
  703. 'numIndexesBefore' => 2,
  704. 'numIndexesAfter' => 2,
  705. 'note' => 'all indexes already exist',
  706. 'ok' => 1.0
  707. ];
  708. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  709. }
  710. public function testCreateIndexWithDeprecatedOptions()
  711. {
  712. $this->getCollection()->createIndex(['foo' => 1], ['w' => 1]);
  713. $expected = [
  714. 'createdCollectionAutomatically' => false,
  715. 'numIndexesBefore' => 2,
  716. 'numIndexesAfter' => 2,
  717. 'note' => 'all indexes already exist',
  718. 'ok' => 1.0
  719. ];
  720. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  721. }
  722. public function testCreateIndexTwiceWithSameName()
  723. {
  724. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  725. $expected = [
  726. 'createdCollectionAutomatically' => false,
  727. 'numIndexesBefore' => 2,
  728. 'numIndexesAfter' => 2,
  729. 'note' => 'all indexes already exist',
  730. 'ok' => 1.0
  731. ];
  732. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']));
  733. }
  734. public function testCreateIndexTwiceWithDifferentName()
  735. {
  736. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
  737. $expected = [
  738. 'createdCollectionAutomatically' => false,
  739. 'numIndexesBefore' => 2,
  740. 'numIndexesAfter' => 2,
  741. 'note' => 'all indexes already exist',
  742. 'ok' => 1.0
  743. ];
  744. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'index_test']));
  745. }
  746. public function testCreateIndexTwiceWithDifferentOrder()
  747. {
  748. $this->getCollection()->createIndex(['foo' => 1, 'bar' => 1]);
  749. $expected = [
  750. 'createdCollectionAutomatically' => false,
  751. 'numIndexesBefore' => 2,
  752. 'numIndexesAfter' => 3,
  753. 'ok' => 1.0
  754. ];
  755. $this->assertSame($expected, $this->getCollection()->createIndex(['bar' => 1, 'foo' => 1]));
  756. }
  757. public function testCreateIndexesWithDifferentOptions()
  758. {
  759. $this->setExpectedException('MongoResultException');
  760. $this->getCollection()->createIndex(['foo' => 1]);
  761. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  762. }
  763. /**
  764. * @dataProvider createIndexIgnoredOptions
  765. */
  766. public function testCreateIndexesWithIgnoredOptions($option)
  767. {
  768. $this->getCollection()->createIndex(['foo' => 1]);
  769. $expected = [
  770. 'createdCollectionAutomatically' => false,
  771. 'numIndexesBefore' => 2,
  772. 'numIndexesAfter' => 2,
  773. 'note' => 'all indexes already exist',
  774. 'ok' => 1.0
  775. ];
  776. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], [$option => true]));
  777. }
  778. public static function createIndexIgnoredOptions()
  779. {
  780. return [
  781. 'background' => ['background'],
  782. 'dropDups' => ['dropDups'],
  783. ];
  784. }
  785. public function testCreateIndexWithSameNameAndDifferentOptions()
  786. {
  787. $this->setExpectedException('MongoResultException');
  788. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  789. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  790. }
  791. public function testEnsureIndex()
  792. {
  793. $expected = [
  794. 'createdCollectionAutomatically' => true,
  795. 'numIndexesBefore' => 1,
  796. 'numIndexesAfter' => 2,
  797. 'ok' => 1.0
  798. ];
  799. $collection = $this->getCollection();
  800. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  801. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  802. $indexes = iterator_to_array($newCollection->listIndexes());
  803. $this->assertCount(2, $indexes);
  804. $index = $indexes[1];
  805. $this->assertSame(['bar' => 1], $index->getKey());
  806. $this->assertTrue($index->isUnique());
  807. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  808. }
  809. public function testEnsureIndexAlreadyExists()
  810. {
  811. $collection = $this->getCollection();
  812. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  813. $expected = [
  814. 'createdCollectionAutomatically' => false,
  815. 'numIndexesBefore' => 2,
  816. 'numIndexesAfter' => 2,
  817. 'ok' => 1.0,
  818. 'note' => 'all indexes already exist',
  819. ];
  820. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  821. }
  822. public function testEnsureIndexAlreadyExistsWithDifferentOptions()
  823. {
  824. $collection = $this->getCollection();
  825. $collection->ensureIndex(['bar' => 1], ['unique' => true]);
  826. $this->setExpectedException('MongoResultException', 'Index with name: bar_1 already exists with different options');
  827. $collection->ensureIndex(['bar' => 1]);
  828. }
  829. public function testDeleteIndexUsingIndexName()
  830. {
  831. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  832. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  833. $expected = [
  834. 'nIndexesWas' => 2,
  835. 'errmsg' => 'index not found with name [bar_1]',
  836. 'ok' => 0.0,
  837. 'code' => 27,
  838. ];
  839. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  840. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  841. }
  842. public function testDeleteIndexUsingField()
  843. {
  844. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  845. $newCollection->createIndex(['bar' => 1]);
  846. $expected = [
  847. 'nIndexesWas' => 2,
  848. 'ok' => 1.0,
  849. ];
  850. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  851. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  852. }
  853. public function testDeleteIndexUsingKeys()
  854. {
  855. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  856. $newCollection->createIndex(['bar' => 1]);
  857. $expected = [
  858. 'nIndexesWas' => 2,
  859. 'ok' => 1.0,
  860. ];
  861. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  862. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  863. }
  864. public function testDeleteIndexes()
  865. {
  866. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  867. $newCollection->createIndex(['bar' => 1]);
  868. $expected = [
  869. 'nIndexesWas' => 2,
  870. 'msg' => 'non-_id indexes dropped for collection',
  871. 'ok' => 1.0,
  872. ];
  873. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  874. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  875. }
  876. public function testDeleteIndexesForNonExistingCollection()
  877. {
  878. $expected = [
  879. 'ok' => 0.0,
  880. 'errmsg' => 'ns not found',
  881. 'code' => 26,
  882. ];
  883. $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
  884. }
  885. public function testGetIndexInfo()
  886. {
  887. $collection = $this->getCollection();
  888. $collection->createIndex(['foo' => 1]);
  889. $expected = [
  890. [
  891. 'v' => 1,
  892. 'key' => ['_id' => 1],
  893. 'name' => '_id_',
  894. 'ns' => 'mongo-php-adapter.test',
  895. ],
  896. [
  897. 'v' => 1,
  898. 'key' => ['foo' => 1],
  899. 'name' => 'foo_1',
  900. 'ns' => 'mongo-php-adapter.test',
  901. ],
  902. ];
  903. $this->assertSame(
  904. $expected,
  905. $collection->getIndexInfo()
  906. );
  907. }
  908. public function testFindAndModifyUpdate()
  909. {
  910. $id = '54203e08d51d4a1f868b456e';
  911. $collection = $this->getCollection();
  912. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  913. $collection->insert($document);
  914. $document = $collection->findAndModify(
  915. ['_id' => new \MongoId($id)],
  916. ['$set' => ['foo' => 'foo']]
  917. );
  918. $this->assertSame('bar', $document['foo']);
  919. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  920. $this->assertSame(1, $newCollection->count());
  921. $object = $newCollection->findOne();
  922. $this->assertNotNull($object);
  923. $this->assertAttributeSame('foo', 'foo', $object);
  924. }
  925. public function testFindAndModifyUpdateWithUpdateOptions()
  926. {
  927. $id = '54203e08d51d4a1f868b456e';
  928. $collection = $this->getCollection();
  929. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  930. $collection->insert($document);
  931. $document = $collection->findAndModify(
  932. ['_id' => new \MongoId($id)],
  933. [],
  934. [],
  935. [
  936. 'update' => ['bar' => 'foo']
  937. ]
  938. );
  939. $this->assertSame('bar', $document['foo']);
  940. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  941. $this->assertSame(1, $newCollection->count());
  942. $object = $newCollection->findOne();
  943. $this->assertNotNull($object);
  944. $this->assertAttributeSame('foo', 'bar', $object);
  945. $this->assertObjectNotHasAttribute('foo', $object);
  946. }
  947. public function testFindAndModifyUpdateReplace()
  948. {
  949. $id = '54203e08d51d4a1f868b456e';
  950. $collection = $this->getCollection();
  951. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  952. $collection->insert($document);
  953. $document = $collection->findAndModify(
  954. ['_id' => new \MongoId($id)],
  955. ['_id' => new \MongoId($id), 'foo' => 'boo']
  956. );
  957. $this->assertSame('bar', $document['foo']);
  958. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  959. $this->assertSame(1, $newCollection->count());
  960. $object = $newCollection->findOne();
  961. $this->assertNotNull($object);
  962. $this->assertAttributeSame('boo', 'foo', $object);
  963. $this->assertObjectNotHasAttribute('bar', $object);
  964. }
  965. public function testFindAndModifyUpdateReturnNew()
  966. {
  967. $id = '54203e08d51d4a1f868b456e';
  968. $collection = $this->getCollection();
  969. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  970. $collection->insert($document);
  971. $document = $collection->findAndModify(
  972. ['_id' => new \MongoId($id)],
  973. ['$set' => ['foo' => 'foo']],
  974. null,
  975. ['new' => true]
  976. );
  977. $this->assertSame('foo', $document['foo']);
  978. }
  979. public function testFindAndModifyWithFields()
  980. {
  981. $id = '54203e08d51d4a1f868b456e';
  982. $collection = $this->getCollection();
  983. $document = [
  984. '_id' => new \MongoId($id),
  985. 'foo' => 'bar',
  986. 'bar' => 'foo',
  987. ];
  988. $collection->insert($document);
  989. $document = $collection->findAndModify(
  990. ['_id' => new \MongoId($id)],
  991. ['$set' => ['foo' => 'foo']],
  992. ['foo' => true]
  993. );
  994. $this->assertArrayNotHasKey('bar', $document);
  995. $this->assertArrayHasKey('foo', $document);
  996. }
  997. public function testGroup()
  998. {
  999. $collection = $this->getCollection();
  1000. $document1 = ['a' => 2];
  1001. $collection->insert($document1);
  1002. $document2 = ['b' => 5];
  1003. $collection->insert($document2);
  1004. $document3 = ['a' => 1];
  1005. $collection->insert($document3);
  1006. $keys = [];
  1007. $initial = ["count" => 0];
  1008. $reduce = "function (obj, prev) { prev.count++; }";
  1009. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  1010. $result = $collection->group($keys, $initial, $reduce, $condition);
  1011. $this->assertArraySubset(
  1012. [
  1013. 'retval' => [['count' => 1.0]],
  1014. 'count' => 1.0,
  1015. 'keys' => 1,
  1016. 'ok' => 1.0,
  1017. ],
  1018. $result
  1019. );
  1020. }
  1021. public function testMapReduce()
  1022. {
  1023. $data = array(
  1024. array(
  1025. 'username' => 'jones',
  1026. 'likes' => 20.0,
  1027. 'text' => 'Hello world!'
  1028. ),
  1029. array(
  1030. 'username' => 'bob',
  1031. 'likes' => 100.0,
  1032. 'text' => 'Hello world!'
  1033. ),
  1034. array(
  1035. 'username' => 'bob',
  1036. 'likes' => 100.0,
  1037. 'text' => 'Hello world!'
  1038. ),
  1039. );
  1040. $collection = $this->getCollection();
  1041. $collection->batchInsert($data);
  1042. $map = 'function() {
  1043. emit(this.username, { count: 1, likes: this.likes });
  1044. }';
  1045. $reduce = 'function(key, values) {
  1046. var result = {count: 0, likes: 0};
  1047. values.forEach(function(value) {
  1048. result.count += value.count;
  1049. result.likes += value.likes;
  1050. });
  1051. return result;
  1052. }';
  1053. $finalize = 'function (key, value) { value.test = "test"; return value; }';
  1054. $command = [
  1055. 'mapreduce' => $this->getCollection()->getName(),
  1056. 'map' => new \MongoCode($map),
  1057. 'reduce' => new \MongoCode($reduce),
  1058. 'query' => (object) [],
  1059. 'out' => ['inline' => true],
  1060. 'finalize' => new \MongoCode($finalize),
  1061. ];
  1062. $result = $this->getDatabase()->command($command);
  1063. $expected = [
  1064. [
  1065. '_id' => 'bob',
  1066. 'value' => [
  1067. 'count' => 2.0,
  1068. 'likes' => 200.0,
  1069. 'test' => 'test',
  1070. ],
  1071. ],
  1072. [
  1073. '_id' => 'jones',
  1074. 'value' => [
  1075. 'count' => 1.0,
  1076. 'likes' => 20.0,
  1077. 'test' => 'test',
  1078. ],
  1079. ],
  1080. ];
  1081. $this->assertSame(1.0, $result['ok']);
  1082. $this->assertSame($expected, $result['results']);
  1083. }
  1084. public function testFindAndModifyResultException()
  1085. {
  1086. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  1087. $collection = $this->getCollection();
  1088. $this->setExpectedException('MongoResultException');
  1089. $collection->findAndModify(
  1090. array("inprogress" => false, "name" => "Next promo"),
  1091. array('$unsupportedOperator' => array("tasks" => -1)),
  1092. array("tasks" => true),
  1093. array("new" => true)
  1094. );
  1095. }
  1096. public function testFindAndModifyExceptionTimeout()
  1097. {
  1098. $this->failMaxTimeMS();
  1099. $id = '54203e08d51d4a1f868b456e';
  1100. $collection = $this->getCollection();
  1101. $this->setExpectedException('MongoExecutionTimeoutException');
  1102. $document = $collection->findAndModify(
  1103. ['_id' => new \MongoId($id)],
  1104. null,
  1105. null,
  1106. ['maxTimeMS' => 1, 'remove' => true]
  1107. );
  1108. }
  1109. public function testFindAndModifyRemove()
  1110. {
  1111. $id = '54203e08d51d4a1f868b456e';
  1112. $collection = $this->getCollection();
  1113. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  1114. $collection->insert($document);
  1115. $document = $collection->findAndModify(
  1116. ['_id' => new \MongoId($id)],
  1117. null,
  1118. null,
  1119. ['remove' => true]
  1120. );
  1121. $this->assertEquals('bar', $document['foo']);
  1122. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  1123. $this->assertSame(0, $newCollection->count());
  1124. }
  1125. public function testValidate()
  1126. {
  1127. $collection = $this->getCollection();
  1128. $document = ['foo' => 'bar'];
  1129. $collection->insert($document);
  1130. $result = $collection->validate();
  1131. $this->assertArraySubset(
  1132. [
  1133. 'ns' => 'mongo-php-adapter.test',
  1134. 'nrecords' => 1,
  1135. 'nIndexes' => 1,
  1136. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  1137. 'valid' => true,
  1138. 'errors' => [],
  1139. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  1140. 'ok' => 1.0
  1141. ],
  1142. $result
  1143. );
  1144. }
  1145. public function testDrop()
  1146. {
  1147. $document = ['foo' => 'bar'];
  1148. $this->getCollection()->insert($document);
  1149. $expected = [
  1150. 'ns' => (string) $this->getCollection(),
  1151. 'nIndexesWas' => 1,
  1152. 'ok' => 1.0
  1153. ];
  1154. $this->assertSame($expected, $this->getCollection()->drop());
  1155. }
  1156. public function testEmptyCollectionName()
  1157. {
  1158. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  1159. new \MongoCollection($this->getDatabase(), '');
  1160. }
  1161. public function testSelectCollectionWithNullBytes()
  1162. {
  1163. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  1164. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  1165. }
  1166. public function testSubCollectionWithNullBytes()
  1167. {
  1168. $collection = $this->getCollection();
  1169. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  1170. $this->assertSame('test', $collection->getName());
  1171. }
  1172. }
  1173. class PrivatePropertiesStub
  1174. {
  1175. private $foo = 'bar';
  1176. }