MongoCollectionTest.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\ReadPreference;
  4. /**
  5. * @author alcaeus <alcaeus@alcaeus.org>
  6. */
  7. class MongoCollectionTest extends TestCase
  8. {
  9. public function testGetNestedCollections()
  10. {
  11. $collection = $this->getCollection()->foo->bar;
  12. $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
  13. }
  14. public function testCreateRecord()
  15. {
  16. $collection = $this->getCollection();
  17. $expected = [
  18. 'ok' => 1.0,
  19. 'n' => 0,
  20. 'err' => null,
  21. 'errmsg' => null,
  22. ];
  23. $document = ['foo' => 'bar'];
  24. $this->assertSame($expected, $collection->insert($document));
  25. $this->assertInstanceOf('MongoId', $document['_id']);
  26. $id = (string) $document['_id'];
  27. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  28. $this->assertSame(1, $newCollection->count());
  29. $object = $newCollection->findOne();
  30. $this->assertNotNull($object);
  31. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  32. $this->assertSame($id, (string) $object->_id);
  33. $this->assertObjectHasAttribute('foo', $object);
  34. $this->assertAttributeSame('bar', 'foo', $object);
  35. }
  36. public function testInsertInvalidData()
  37. {
  38. $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
  39. $document = 8;
  40. $this->getCollection()->insert($document);
  41. }
  42. public function testInsertEmptyArray()
  43. {
  44. $document = [];
  45. $this->getCollection()->insert($document);
  46. $this->assertSame(1, $this->getCollection()->count());
  47. }
  48. public function testInsertArrayWithNumericKeys()
  49. {
  50. $document = [1 => 'foo'];
  51. $this->getCollection()->insert($document);
  52. $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
  53. }
  54. public function testInsertEmptyObject()
  55. {
  56. $document = (object) [];
  57. $this->getCollection()->insert($document);
  58. $this->assertSame(1, $this->getCollection()->count());
  59. }
  60. public function testInsertObjectWithPrivateProperties()
  61. {
  62. $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
  63. $document = new PrivatePropertiesStub();
  64. $this->getCollection()->insert($document);
  65. }
  66. public function testInsertDuplicate()
  67. {
  68. $collection = $this->getCollection();
  69. $collection->createIndex(['foo' => 1], ['unique' => true]);
  70. $document = ['foo' => 'bar'];
  71. $collection->insert($document);
  72. unset($document['_id']);
  73. $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
  74. $collection->insert($document);
  75. }
  76. public function testUnacknowledgedWrite()
  77. {
  78. $document = ['foo' => 'bar'];
  79. $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
  80. }
  81. public function testInsertWriteConcernException()
  82. {
  83. $this->setExpectedException(
  84. 'MongoWriteConcernException',
  85. "cannot use 'w' > 1 when a host is not replicated"
  86. );
  87. $document = ['foo' => 'bar'];
  88. $this->getCollection()->insert($document, ['w' => 2]);
  89. }
  90. public function testInsertMany()
  91. {
  92. $expected = [
  93. 'ok' => 1.0,
  94. 'n' => 0,
  95. 'syncMillis' => 0,
  96. 'writtenTo' => null,
  97. 'err' => null,
  98. ];
  99. $documents = [
  100. ['foo' => 'bar'],
  101. ['bar' => 'foo']
  102. ];
  103. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  104. foreach ($documents as $document) {
  105. $this->assertInstanceOf('MongoId', $document['_id']);
  106. }
  107. }
  108. public function testInsertManyWithNonNumericKeys()
  109. {
  110. $expected = [
  111. 'ok' => 1.0,
  112. 'n' => 0,
  113. 'syncMillis' => 0,
  114. 'writtenTo' => null,
  115. 'err' => null,
  116. ];
  117. $documents = [
  118. 'a' => ['foo' => 'bar'],
  119. 'b' => ['bar' => 'foo']
  120. ];
  121. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
  122. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  123. $this->assertSame(2, $newCollection->count());
  124. }
  125. public function testBatchInsertContinuesOnError()
  126. {
  127. $expected = [
  128. 'ok' => 1.0,
  129. 'n' => 0,
  130. 'syncMillis' => 0,
  131. 'writtenTo' => null,
  132. 'err' => null,
  133. ];
  134. $documents = [
  135. 8,
  136. 'b' => ['bar' => 'foo']
  137. ];
  138. $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
  139. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  140. $this->assertSame(1, $newCollection->count());
  141. }
  142. public function testBatchInsertException()
  143. {
  144. $this->setExpectedException('MongoResultException', 'cannot use \'w\' > 1 when a host is not replicated');
  145. $documents = [['foo' => 'bar']];
  146. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  147. }
  148. public function testBatchInsertEmptyBatchException()
  149. {
  150. $this->setExpectedException('MongoException', 'No write ops were included in the batch');
  151. $documents = [];
  152. $this->getCollection()->batchInsert($documents, ['w' => 2]);
  153. }
  154. public function testUpdateWriteConcern()
  155. {
  156. $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
  157. $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
  158. }
  159. public function testUpdateOne()
  160. {
  161. $document = ['foo' => 'bar'];
  162. $this->getCollection()->insert($document);
  163. // Unset ID to re-insert
  164. unset($document['_id']);
  165. $this->getCollection()->insert($document);
  166. $expected = [
  167. 'ok' => 1.0,
  168. 'nModified' => 1,
  169. 'n' => 1,
  170. 'err' => null,
  171. 'errmsg' => null,
  172. 'updatedExisting' => true,
  173. ];
  174. $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  175. $this->assertSame($expected, $result);
  176. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  177. }
  178. public function testUpdateDuplicate()
  179. {
  180. $collection = $this->getCollection();
  181. $collection->createIndex(['foo' => 1], ['unique' => 1]);
  182. $document = ['foo' => 'bar'];
  183. $collection->insert($document);
  184. $document = ['foo' => 'foo'];
  185. $collection->insert($document);
  186. $this->setExpectedException('MongoDuplicateKeyException');
  187. $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
  188. }
  189. public function testUpdateMany()
  190. {
  191. $document = ['change' => true, 'foo' => 'bar'];
  192. $this->getCollection()->insert($document);
  193. unset($document['_id']);
  194. $this->getCollection()->insert($document);
  195. $document = ['change' => true, 'foo' => 'foo'];
  196. $this->getCollection()->insert($document);
  197. $expected = [
  198. 'ok' => 1.0,
  199. 'nModified' => 2,
  200. 'n' => 3,
  201. 'err' => null,
  202. 'errmsg' => null,
  203. 'updatedExisting' => true,
  204. ];
  205. $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
  206. $this->assertSame($expected, $result);
  207. $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
  208. }
  209. public function testUnacknowledgedUpdate()
  210. {
  211. $document = ['foo' => 'bar'];
  212. $this->getCollection()->insert($document);
  213. unset($document['_id']);
  214. $this->getCollection()->insert($document);
  215. $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
  216. }
  217. public function testRemoveMultiple()
  218. {
  219. $document = ['change' => true, 'foo' => 'bar'];
  220. $this->getCollection()->insert($document);
  221. unset($document['_id']);
  222. $this->getCollection()->insert($document);
  223. $document = ['change' => true, 'foo' => 'foo'];
  224. $this->getCollection()->insert($document);
  225. $expected = [
  226. 'ok' => 1.0,
  227. 'n' => 2,
  228. 'err' => null,
  229. 'errmsg' => null,
  230. ];
  231. $result = $this->getCollection()->remove(['foo' => 'bar']);
  232. $this->assertSame($expected, $result);
  233. $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
  234. }
  235. public function testRemoveSingle()
  236. {
  237. $document = ['change' => true, 'foo' => 'bar'];
  238. $this->getCollection()->insert($document);
  239. unset($document['_id']);
  240. $this->getCollection()->insert($document);
  241. unset($document['_id']);
  242. $this->getCollection()->insert($document);
  243. $expected = [
  244. 'ok' => 1.0,
  245. 'n' => 1,
  246. 'err' => null,
  247. 'errmsg' => null,
  248. ];
  249. $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
  250. $this->assertSame($expected, $result);
  251. $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
  252. }
  253. public function testRemoveUnacknowledged()
  254. {
  255. $document = ['change' => true, 'foo' => 'bar'];
  256. $this->getCollection()->insert($document);
  257. unset($document['_id']);
  258. $this->getCollection()->insert($document);
  259. unset($document['_id']);
  260. $this->getCollection()->insert($document);
  261. $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
  262. }
  263. public function testFindReturnsCursor()
  264. {
  265. $this->prepareData();
  266. $collection = $this->getCollection();
  267. $this->assertInstanceOf('MongoCursor', $collection->find());
  268. }
  269. public function testCount()
  270. {
  271. $this->prepareData();
  272. $collection = $this->getCollection();
  273. $this->assertSame(3, $collection->count());
  274. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  275. }
  276. public function testCountTimeout()
  277. {
  278. $this->failMaxTimeMS();
  279. $this->setExpectedException('MongoExecutionTimeoutException');
  280. $this->getCollection()->count([], ['maxTimeMS' => 1]);
  281. }
  282. public function testFindOne()
  283. {
  284. $this->prepareData();
  285. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  286. $this->assertSame(['foo' => 'foo'], $document);
  287. }
  288. public function testFindOneConnectionIssue()
  289. {
  290. $this->setExpectedException('MongoConnectionException');
  291. $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
  292. $collection = $client->selectCollection('mongo-php-adapter', 'test');
  293. $collection->findOne();
  294. }
  295. public function testDistinct()
  296. {
  297. $this->prepareData();
  298. $values = $this->getCollection()->distinct('foo');
  299. $this->assertInternalType('array', $values);
  300. sort($values);
  301. $this->assertEquals(['bar', 'foo'], $values);
  302. }
  303. public function testDistinctWithQuery()
  304. {
  305. $this->prepareData();
  306. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  307. $this->assertInternalType('array', $values);
  308. $this->assertEquals(['bar'], $values);
  309. }
  310. public function testAggregate()
  311. {
  312. $collection = $this->getCollection();
  313. $this->prepareData();
  314. $pipeline = [
  315. [
  316. '$group' => [
  317. '_id' => '$foo',
  318. 'count' => [ '$sum' => 1 ],
  319. ],
  320. ],
  321. [
  322. '$sort' => ['_id' => 1]
  323. ]
  324. ];
  325. $result = $collection->aggregate($pipeline);
  326. $this->assertInternalType('array', $result);
  327. $this->assertArrayHasKey('result', $result);
  328. $this->assertEquals([
  329. ['_id' => 'bar', 'count' => 2],
  330. ['_id' => 'foo', 'count' => 1],
  331. ], $result['result']);
  332. }
  333. public function testAggregateTimeoutException()
  334. {
  335. $collection = $this->getCollection();
  336. $this->failMaxTimeMS();
  337. $this->setExpectedException('MongoExecutionTimeoutException');
  338. $pipeline = [
  339. [
  340. '$group' => [
  341. '_id' => '$foo',
  342. 'count' => [ '$sum' => 1 ],
  343. ],
  344. ],
  345. [
  346. '$sort' => ['_id' => 1]
  347. ]
  348. ];
  349. $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
  350. }
  351. public function testAggregateCursor()
  352. {
  353. $collection = $this->getCollection();
  354. $this->prepareData();
  355. $pipeline = [
  356. [
  357. '$group' => [
  358. '_id' => '$foo',
  359. 'count' => [ '$sum' => 1 ],
  360. ],
  361. ],
  362. [
  363. '$sort' => ['_id' => 1]
  364. ]
  365. ];
  366. $cursor = $collection->aggregateCursor($pipeline);
  367. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  368. $this->assertEquals([
  369. ['_id' => 'bar', 'count' => 2],
  370. ['_id' => 'foo', 'count' => 1],
  371. ], iterator_to_array($cursor));
  372. }
  373. public function testReadPreference()
  374. {
  375. $collection = $this->getCollection();
  376. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  377. $this->assertFalse($collection->getSlaveOkay());
  378. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  379. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  380. $this->assertTrue($collection->getSlaveOkay());
  381. $this->assertTrue($collection->setSlaveOkay(true));
  382. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  383. $this->assertTrue($collection->setSlaveOkay(false));
  384. $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  385. }
  386. public function testReadPreferenceIsSetInDriver()
  387. {
  388. $this->skipTestIf(extension_loaded('mongo'));
  389. $collection = $this->getCollection();
  390. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
  391. // Only way to check whether options are passed down is through debugInfo
  392. $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
  393. $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
  394. $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
  395. }
  396. public function testReadPreferenceIsInherited()
  397. {
  398. $database = $this->getDatabase();
  399. $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
  400. $collection = $database->selectCollection('test');
  401. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
  402. }
  403. public function testWriteConcern()
  404. {
  405. $collection = $this->getCollection();
  406. $this->assertTrue($collection->setWriteConcern('majority', 100));
  407. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  408. }
  409. public function testWriteConcernIsSetInDriver()
  410. {
  411. $this->skipTestIf(extension_loaded('mongo'));
  412. $collection = $this->getCollection();
  413. $this->assertTrue($collection->setWriteConcern(2, 100));
  414. // Only way to check whether options are passed down is through debugInfo
  415. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  416. $this->assertSame(2, $writeConcern->getW());
  417. $this->assertSame(100, $writeConcern->getWtimeout());
  418. }
  419. public function testWriteConcernIsInherited()
  420. {
  421. $database = $this->getDatabase();
  422. $database->setWriteConcern('majority', 100);
  423. $collection = $database->selectCollection('test');
  424. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  425. }
  426. public function testSaveInsert()
  427. {
  428. $id = '54203e08d51d4a1f868b456e';
  429. $collection = $this->getCollection();
  430. $objectId = new \MongoId($id);
  431. $expected = [
  432. 'ok' => 1.0,
  433. 'nModified' => 0,
  434. 'n' => 1,
  435. 'err' => null,
  436. 'errmsg' => null,
  437. 'upserted' => $objectId,
  438. 'updatedExisting' => false,
  439. ];
  440. $document = ['_id' => $objectId, 'foo' => 'bar'];
  441. $this->assertEquals($expected, $collection->save($document));
  442. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  443. $this->assertSame(1, $newCollection->count());
  444. $object = $newCollection->findOne();
  445. $this->assertNotNull($object);
  446. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  447. $this->assertSame($id, (string) $object->_id);
  448. $this->assertObjectHasAttribute('foo', $object);
  449. $this->assertAttributeSame('bar', 'foo', $object);
  450. }
  451. public function testRemoveOne()
  452. {
  453. $id = '54203e08d51d4a1f868b456e';
  454. $collection = $this->getCollection();
  455. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  456. $collection->insert($document);
  457. $collection->remove(['_id' => new \MongoId($id)]);
  458. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  459. $this->assertSame(0, $newCollection->count());
  460. }
  461. public function testSaveUpdate()
  462. {
  463. $expected = [
  464. 'ok' => 1.0,
  465. 'nModified' => 1,
  466. 'n' => 1,
  467. 'err' => null,
  468. 'errmsg' => null,
  469. 'updatedExisting' => true,
  470. ];
  471. $id = '54203e08d51d4a1f868b456e';
  472. $collection = $this->getCollection();
  473. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  474. $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
  475. $collection->insert($insertDocument);
  476. $this->assertSame($expected, $collection->save($saveDocument));
  477. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  478. $this->assertSame(1, $newCollection->count());
  479. $object = $newCollection->findOne();
  480. $this->assertNotNull($object);
  481. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  482. $this->assertSame($id, (string) $object->_id);
  483. $this->assertObjectHasAttribute('foo', $object);
  484. $this->assertAttributeSame('foo', 'foo', $object);
  485. }
  486. public function testSavingShouldReplaceTheWholeDocument() {
  487. $id = '54203e08d51d4a1f868b456e';
  488. $collection = $this->getCollection();
  489. $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  490. $saveDocument = ['_id' => new \MongoId($id)];
  491. $collection->insert($insertDocument);
  492. $collection->save($saveDocument);
  493. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  494. $this->assertSame(1, $newCollection->count());
  495. $object = $newCollection->findOne();
  496. $this->assertNotNull($object);
  497. $this->assertObjectNotHasAttribute('foo', $object);
  498. }
  499. public function testSaveDuplicate()
  500. {
  501. $collection = $this->getCollection();
  502. $collection->createIndex(['foo' => 1], ['unique' => true]);
  503. $document = ['foo' => 'bar'];
  504. $collection->save($document);
  505. $this->setExpectedException('MongoDuplicateKeyException');
  506. unset($document['_id']);
  507. $collection->save($document);
  508. }
  509. public function testSaveEmptyKeys()
  510. {
  511. $document = [];
  512. $this->getCollection()->save($document);
  513. $this->assertSame(1, $this->getCollection()->count());
  514. }
  515. public function testSaveEmptyObject()
  516. {
  517. $document = (object) [];
  518. $this->getCollection()->save($document);
  519. $this->assertSame(1, $this->getCollection()->count());
  520. }
  521. public function testGetDBRef()
  522. {
  523. $collection = $this->getCollection();
  524. $insertDocument = ['_id' => 1, 'foo' => 'bar'];
  525. $collection->insert($insertDocument);
  526. $document = $collection->getDBRef([
  527. '$ref' => 'test',
  528. '$id' => 1,
  529. ]);
  530. $this->assertEquals($insertDocument, $document);
  531. }
  532. public function testCreateDBRef()
  533. {
  534. $collection = $this->getCollection();
  535. $reference = $collection->createDBRef(['_id' => 'foo']);
  536. $this->assertSame(
  537. [
  538. '$ref' => 'test',
  539. '$id' => 'foo',
  540. ],
  541. $reference
  542. );
  543. }
  544. public function testCreateIndex()
  545. {
  546. $expected = [
  547. 'createdCollectionAutomatically' => true,
  548. 'numIndexesBefore' => 1,
  549. 'numIndexesAfter' => 2,
  550. 'ok' => 1.0,
  551. ];
  552. $collection = $this->getCollection();
  553. $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
  554. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  555. $iterator = $newCollection->listIndexes();
  556. $indexes = iterator_to_array($iterator);
  557. $this->assertCount(2, $indexes);
  558. $index = $indexes[1];
  559. $this->assertSame(['foo' => 1], $index->getKey());
  560. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  561. }
  562. public function testCreateIndexInvalid()
  563. {
  564. $this->setExpectedException('MongoException', 'index specification has no elements');
  565. $this->getCollection()->createIndex([]);
  566. }
  567. public function testCreateIndexTwice()
  568. {
  569. $this->getCollection()->createIndex(['foo' => 1]);
  570. $expected = [
  571. 'createdCollectionAutomatically' => false,
  572. 'numIndexesBefore' => 2,
  573. 'numIndexesAfter' => 2,
  574. 'note' => 'all indexes already exist',
  575. 'ok' => 1.0
  576. ];
  577. $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
  578. }
  579. public function testCreateIndexesWithDifferentOptions()
  580. {
  581. $this->setExpectedException('MongoResultException');
  582. $this->getCollection()->createIndex(['foo' => 1]);
  583. $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
  584. }
  585. public function testCreateIndexWithSameName()
  586. {
  587. $this->setExpectedException('MongoResultException');
  588. $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
  589. $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
  590. }
  591. public function testEnsureIndex()
  592. {
  593. $expected = [
  594. 'createdCollectionAutomatically' => true,
  595. 'numIndexesBefore' => 1,
  596. 'numIndexesAfter' => 2,
  597. 'ok' => 1.0
  598. ];
  599. $collection = $this->getCollection();
  600. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  601. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  602. $indexes = iterator_to_array($newCollection->listIndexes());
  603. $this->assertCount(2, $indexes);
  604. $index = $indexes[1];
  605. $this->assertSame(['bar' => 1], $index->getKey());
  606. $this->assertTrue($index->isUnique());
  607. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  608. }
  609. public function testDeleteIndexUsingIndexName()
  610. {
  611. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  612. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  613. $expected = [
  614. 'nIndexesWas' => 2,
  615. 'errmsg' => 'index not found with name [bar_1]',
  616. 'ok' => 0.0,
  617. 'code' => 27,
  618. ];
  619. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  620. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  621. }
  622. public function testDeleteIndexUsingField()
  623. {
  624. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  625. $newCollection->createIndex(['bar' => 1]);
  626. $expected = [
  627. 'nIndexesWas' => 2,
  628. 'ok' => 1.0,
  629. ];
  630. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  631. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  632. }
  633. public function testDeleteIndexUsingKeys()
  634. {
  635. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  636. $newCollection->createIndex(['bar' => 1]);
  637. $expected = [
  638. 'nIndexesWas' => 2,
  639. 'ok' => 1.0,
  640. ];
  641. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  642. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  643. }
  644. public function testDeleteIndexes()
  645. {
  646. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  647. $newCollection->createIndex(['bar' => 1]);
  648. $expected = [
  649. 'nIndexesWas' => 2,
  650. 'msg' => 'non-_id indexes dropped for collection',
  651. 'ok' => 1.0,
  652. ];
  653. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  654. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  655. }
  656. public function testGetIndexInfo()
  657. {
  658. $collection = $this->getCollection();
  659. $collection->createIndex(['foo' => 1]);
  660. $expected = [
  661. [
  662. 'v' => 1,
  663. 'key' => ['_id' => 1],
  664. 'name' => '_id_',
  665. 'ns' => 'mongo-php-adapter.test',
  666. ],
  667. [
  668. 'v' => 1,
  669. 'key' => ['foo' => 1],
  670. 'name' => 'foo_1',
  671. 'ns' => 'mongo-php-adapter.test',
  672. ],
  673. ];
  674. $this->assertSame(
  675. $expected,
  676. $collection->getIndexInfo()
  677. );
  678. }
  679. public function testFindAndModifyUpdate()
  680. {
  681. $id = '54203e08d51d4a1f868b456e';
  682. $collection = $this->getCollection();
  683. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  684. $collection->insert($document);
  685. $document = $collection->findAndModify(
  686. ['_id' => new \MongoId($id)],
  687. ['$set' => ['foo' => 'foo']]
  688. );
  689. $this->assertSame('bar', $document['foo']);
  690. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  691. $this->assertSame(1, $newCollection->count());
  692. $object = $newCollection->findOne();
  693. $this->assertNotNull($object);
  694. $this->assertAttributeSame('foo', 'foo', $object);
  695. }
  696. public function testFindAndModifyUpdateReplace()
  697. {
  698. $id = '54203e08d51d4a1f868b456e';
  699. $collection = $this->getCollection();
  700. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  701. $collection->insert($document);
  702. $document = $collection->findAndModify(
  703. ['_id' => new \MongoId($id)],
  704. ['_id' => new \MongoId($id), 'foo' => 'boo']
  705. );
  706. $this->assertSame('bar', $document['foo']);
  707. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  708. $this->assertSame(1, $newCollection->count());
  709. $object = $newCollection->findOne();
  710. $this->assertNotNull($object);
  711. $this->assertAttributeSame('boo', 'foo', $object);
  712. $this->assertObjectNotHasAttribute('bar', $object);
  713. }
  714. public function testFindAndModifyUpdateReturnNew()
  715. {
  716. $id = '54203e08d51d4a1f868b456e';
  717. $collection = $this->getCollection();
  718. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  719. $collection->insert($document);
  720. $document = $collection->findAndModify(
  721. ['_id' => new \MongoId($id)],
  722. ['$set' => ['foo' => 'foo']],
  723. null,
  724. ['new' => true]
  725. );
  726. $this->assertSame('foo', $document['foo']);
  727. }
  728. public function testFindAndModifyWithFields()
  729. {
  730. $id = '54203e08d51d4a1f868b456e';
  731. $collection = $this->getCollection();
  732. $document = [
  733. '_id' => new \MongoId($id),
  734. 'foo' => 'bar',
  735. 'bar' => 'foo',
  736. ];
  737. $collection->insert($document);
  738. $document = $collection->findAndModify(
  739. ['_id' => new \MongoId($id)],
  740. ['$set' => ['foo' => 'foo']],
  741. ['foo' => true]
  742. );
  743. $this->assertArrayNotHasKey('bar', $document);
  744. $this->assertArrayHasKey('foo', $document);
  745. }
  746. public function testGroup()
  747. {
  748. $collection = $this->getCollection();
  749. $document1 = ['a' => 2];
  750. $collection->insert($document1);
  751. $document2 = ['b' => 5];
  752. $collection->insert($document2);
  753. $document3 = ['a' => 1];
  754. $collection->insert($document3);
  755. $keys = [];
  756. $initial = ["count" => 0];
  757. $reduce = "function (obj, prev) { prev.count++; }";
  758. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  759. $result = $collection->group($keys, $initial, $reduce, $condition);
  760. $this->assertArraySubset(
  761. [
  762. 'retval' => [['count' => 1.0]],
  763. 'count' => 1.0,
  764. 'keys' => 1,
  765. 'ok' => 1.0,
  766. ],
  767. $result
  768. );
  769. }
  770. public function testFindAndModifyResultException()
  771. {
  772. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  773. $collection = $this->getCollection();
  774. $this->setExpectedException('MongoResultException');
  775. $collection->findAndModify(
  776. array("inprogress" => false, "name" => "Next promo"),
  777. array('$unsupportedOperator' => array("tasks" => -1)),
  778. array("tasks" => true),
  779. array("new" => true)
  780. );
  781. }
  782. public function testFindAndModifyExceptionTimeout()
  783. {
  784. $this->failMaxTimeMS();
  785. $id = '54203e08d51d4a1f868b456e';
  786. $collection = $this->getCollection();
  787. $this->setExpectedException('MongoExecutionTimeoutException');
  788. $document = $collection->findAndModify(
  789. ['_id' => new \MongoId($id)],
  790. null,
  791. null,
  792. ['maxTimeMS' => 1, 'remove' => true]
  793. );
  794. }
  795. public function testFindAndModifyRemove()
  796. {
  797. $id = '54203e08d51d4a1f868b456e';
  798. $collection = $this->getCollection();
  799. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  800. $collection->insert($document);
  801. $document = $collection->findAndModify(
  802. ['_id' => new \MongoId($id)],
  803. null,
  804. null,
  805. ['remove' => true]
  806. );
  807. $this->assertEquals('bar', $document['foo']);
  808. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  809. $this->assertSame(0, $newCollection->count());
  810. }
  811. public function testValidate()
  812. {
  813. $collection = $this->getCollection();
  814. $document = ['foo' => 'bar'];
  815. $collection->insert($document);
  816. $result = $collection->validate();
  817. $this->assertArraySubset(
  818. [
  819. 'ns' => 'mongo-php-adapter.test',
  820. 'nrecords' => 1,
  821. 'nIndexes' => 1,
  822. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  823. 'valid' => true,
  824. 'errors' => [],
  825. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  826. 'ok' => 1.0
  827. ],
  828. $result
  829. );
  830. }
  831. public function testDrop()
  832. {
  833. $document = ['foo' => 'bar'];
  834. $this->getCollection()->insert($document);
  835. $expected = [
  836. 'ns' => (string) $this->getCollection(),
  837. 'nIndexesWas' => 1,
  838. 'ok' => 1.0
  839. ];
  840. $this->assertSame($expected, $this->getCollection()->drop());
  841. }
  842. public function testEmptyCollectionName()
  843. {
  844. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  845. new \MongoCollection($this->getDatabase(), '');
  846. }
  847. public function testSelectCollectionWithNullBytes()
  848. {
  849. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  850. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  851. }
  852. public function testSubCollectionWithNullBytes()
  853. {
  854. $collection = $this->getCollection();
  855. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  856. $this->assertSame('test', $collection->getName());
  857. }
  858. }
  859. class PrivatePropertiesStub
  860. {
  861. private $foo = 'bar';
  862. }