MongoCollectionTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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->setExpectedException('MongoDuplicateKeyException');
  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. "localhost:27017: 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', 'localhost:27017: 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', "localhost:27017: 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 testUpdateFail()
  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('MongoCursorException');
  506. unset($document['_id']);
  507. $this->assertArraySubset(
  508. [
  509. 'ok' => 0.0,
  510. 'nModified' => 0,
  511. 'n' => 0,
  512. 'err' => 11000,
  513. 'updatedExisting' => true,
  514. ],
  515. $collection->save($document)
  516. );
  517. }
  518. public function testSaveEmptyKeys()
  519. {
  520. $document = [];
  521. $this->getCollection()->save($document);
  522. $this->assertSame(1, $this->getCollection()->count());
  523. }
  524. public function testSaveEmptyObject()
  525. {
  526. $document = (object) [];
  527. $this->getCollection()->save($document);
  528. $this->assertSame(1, $this->getCollection()->count());
  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', 'index specification has no elements');
  574. $this->getCollection()->createIndex([]);
  575. }
  576. public function testCreateIndexTwice()
  577. {
  578. $this->getCollection()->createIndex(['foo' => 1]);
  579. $expected = [
  580. 'createdCollectionAutomatically' => false,
  581. 'numIndexesBefore' => 2,
  582. 'numIndexesAfter' => 2,
  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. $expected = [
  603. 'createdCollectionAutomatically' => true,
  604. 'numIndexesBefore' => 1,
  605. 'numIndexesAfter' => 2,
  606. 'ok' => 1.0
  607. ];
  608. $collection = $this->getCollection();
  609. $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
  610. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  611. $indexes = iterator_to_array($newCollection->listIndexes());
  612. $this->assertCount(2, $indexes);
  613. $index = $indexes[1];
  614. $this->assertSame(['bar' => 1], $index->getKey());
  615. $this->assertTrue($index->isUnique());
  616. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  617. }
  618. public function testDeleteIndexUsingIndexName()
  619. {
  620. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  621. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  622. $expected = [
  623. 'nIndexesWas' => 2,
  624. 'errmsg' => 'index not found with name [bar_1]',
  625. 'ok' => 0.0,
  626. 'code' => 27,
  627. ];
  628. $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
  629. $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
  630. }
  631. public function testDeleteIndexUsingField()
  632. {
  633. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  634. $newCollection->createIndex(['bar' => 1]);
  635. $expected = [
  636. 'nIndexesWas' => 2,
  637. 'ok' => 1.0,
  638. ];
  639. $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
  640. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  641. }
  642. public function testDeleteIndexUsingKeys()
  643. {
  644. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  645. $newCollection->createIndex(['bar' => 1]);
  646. $expected = [
  647. 'nIndexesWas' => 2,
  648. 'ok' => 1.0,
  649. ];
  650. $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
  651. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  652. }
  653. public function testDeleteIndexes()
  654. {
  655. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  656. $newCollection->createIndex(['bar' => 1]);
  657. $expected = [
  658. 'nIndexesWas' => 2,
  659. 'msg' => 'non-_id indexes dropped for collection',
  660. 'ok' => 1.0,
  661. ];
  662. $this->assertSame($expected, $this->getcollection()->deleteIndexes());
  663. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  664. }
  665. public function testGetIndexInfo()
  666. {
  667. $collection = $this->getCollection();
  668. $collection->createIndex(['foo' => 1]);
  669. $expected = [
  670. [
  671. 'v' => 1,
  672. 'key' => ['_id' => 1],
  673. 'name' => '_id_',
  674. 'ns' => 'mongo-php-adapter.test',
  675. ],
  676. [
  677. 'v' => 1,
  678. 'key' => ['foo' => 1],
  679. 'name' => 'foo_1',
  680. 'ns' => 'mongo-php-adapter.test',
  681. ],
  682. ];
  683. $this->assertSame(
  684. $expected,
  685. $collection->getIndexInfo()
  686. );
  687. }
  688. public function testFindAndModifyUpdate()
  689. {
  690. $id = '54203e08d51d4a1f868b456e';
  691. $collection = $this->getCollection();
  692. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  693. $collection->insert($document);
  694. $document = $collection->findAndModify(
  695. ['_id' => new \MongoId($id)],
  696. ['$set' => ['foo' => 'foo']]
  697. );
  698. $this->assertSame('bar', $document['foo']);
  699. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  700. $this->assertSame(1, $newCollection->count());
  701. $object = $newCollection->findOne();
  702. $this->assertNotNull($object);
  703. $this->assertAttributeSame('foo', 'foo', $object);
  704. }
  705. public function testFindAndModifyUpdateReturnNew()
  706. {
  707. $id = '54203e08d51d4a1f868b456e';
  708. $collection = $this->getCollection();
  709. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  710. $collection->insert($document);
  711. $document = $collection->findAndModify(
  712. ['_id' => new \MongoId($id)],
  713. ['$set' => ['foo' => 'foo']],
  714. null,
  715. ['new' => true]
  716. );
  717. $this->assertSame('foo', $document['foo']);
  718. }
  719. public function testFindAndModifyWithFields()
  720. {
  721. $id = '54203e08d51d4a1f868b456e';
  722. $collection = $this->getCollection();
  723. $document = [
  724. '_id' => new \MongoId($id),
  725. 'foo' => 'bar',
  726. 'bar' => 'foo',
  727. ];
  728. $collection->insert($document);
  729. $document = $collection->findAndModify(
  730. ['_id' => new \MongoId($id)],
  731. ['$set' => ['foo' => 'foo']],
  732. ['foo' => true]
  733. );
  734. $this->assertArrayNotHasKey('bar', $document);
  735. $this->assertArrayHasKey('foo', $document);
  736. }
  737. public function testGroup()
  738. {
  739. $collection = $this->getCollection();
  740. $document1 = ['a' => 2];
  741. $collection->insert($document1);
  742. $document2 = ['b' => 5];
  743. $collection->insert($document2);
  744. $document3 = ['a' => 1];
  745. $collection->insert($document3);
  746. $keys = [];
  747. $initial = ["count" => 0];
  748. $reduce = "function (obj, prev) { prev.count++; }";
  749. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  750. $result = $collection->group($keys, $initial, $reduce, $condition);
  751. $this->assertArraySubset(
  752. [
  753. 'retval' => [['count' => 1.0]],
  754. 'count' => 1.0,
  755. 'keys' => 1,
  756. 'ok' => 1.0,
  757. ],
  758. $result
  759. );
  760. }
  761. public function testFindAndModifyResultException()
  762. {
  763. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  764. $collection = $this->getCollection();
  765. $this->setExpectedException('MongoResultException');
  766. $collection->findAndModify(
  767. array("inprogress" => false, "name" => "Next promo"),
  768. array('$unsupportedOperator' => array("tasks" => -1)),
  769. array("tasks" => true),
  770. array("new" => true)
  771. );
  772. }
  773. public function testFindAndModifyExceptionTimeout()
  774. {
  775. $this->failMaxTimeMS();
  776. $id = '54203e08d51d4a1f868b456e';
  777. $collection = $this->getCollection();
  778. $this->setExpectedException('MongoExecutionTimeoutException');
  779. $document = $collection->findAndModify(
  780. ['_id' => new \MongoId($id)],
  781. null,
  782. null,
  783. ['maxTimeMS' => 1, 'remove' => true]
  784. );
  785. }
  786. public function testFindAndModifyRemove()
  787. {
  788. $id = '54203e08d51d4a1f868b456e';
  789. $collection = $this->getCollection();
  790. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  791. $collection->insert($document);
  792. $document = $collection->findAndModify(
  793. ['_id' => new \MongoId($id)],
  794. null,
  795. null,
  796. ['remove' => true]
  797. );
  798. $this->assertEquals('bar', $document['foo']);
  799. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  800. $this->assertSame(0, $newCollection->count());
  801. }
  802. public function testValidate()
  803. {
  804. $collection = $this->getCollection();
  805. $document = ['foo' => 'bar'];
  806. $collection->insert($document);
  807. $result = $collection->validate();
  808. $this->assertArraySubset(
  809. [
  810. 'ns' => 'mongo-php-adapter.test',
  811. 'nrecords' => 1,
  812. 'nIndexes' => 1,
  813. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  814. 'valid' => true,
  815. 'errors' => [],
  816. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  817. 'ok' => 1.0
  818. ],
  819. $result
  820. );
  821. }
  822. public function testDrop()
  823. {
  824. $document = ['foo' => 'bar'];
  825. $this->getCollection()->insert($document);
  826. $expected = [
  827. 'ns' => (string) $this->getCollection(),
  828. 'nIndexesWas' => 1,
  829. 'ok' => 1.0
  830. ];
  831. $this->assertSame($expected, $this->getCollection()->drop());
  832. }
  833. public function testEmptyCollectionName()
  834. {
  835. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  836. new \MongoCollection($this->getDatabase(), '');
  837. }
  838. public function testSelectCollectionWithNullBytes()
  839. {
  840. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  841. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  842. }
  843. public function testSubCollectionWithNullBytes()
  844. {
  845. $collection = $this->getCollection();
  846. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  847. $this->assertSame('test', $collection->getName());
  848. }
  849. }
  850. class PrivatePropertiesStub
  851. {
  852. private $foo = 'bar';
  853. }