MongoCollectionTest.php 31 KB

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