MongoCollectionTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. "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 testFindAndModifyUpdateReturnNew()
  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. ['$set' => ['foo' => 'foo']],
  705. null,
  706. ['new' => true]
  707. );
  708. $this->assertSame('foo', $document['foo']);
  709. }
  710. public function testFindAndModifyWithFields()
  711. {
  712. $id = '54203e08d51d4a1f868b456e';
  713. $collection = $this->getCollection();
  714. $document = [
  715. '_id' => new \MongoId($id),
  716. 'foo' => 'bar',
  717. 'bar' => 'foo',
  718. ];
  719. $collection->insert($document);
  720. $document = $collection->findAndModify(
  721. ['_id' => new \MongoId($id)],
  722. ['$set' => ['foo' => 'foo']],
  723. ['foo' => true]
  724. );
  725. $this->assertArrayNotHasKey('bar', $document);
  726. $this->assertArrayHasKey('foo', $document);
  727. }
  728. public function testGroup()
  729. {
  730. $collection = $this->getCollection();
  731. $document1 = ['a' => 2];
  732. $collection->insert($document1);
  733. $document2 = ['b' => 5];
  734. $collection->insert($document2);
  735. $document3 = ['a' => 1];
  736. $collection->insert($document3);
  737. $keys = [];
  738. $initial = ["count" => 0];
  739. $reduce = "function (obj, prev) { prev.count++; }";
  740. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  741. $result = $collection->group($keys, $initial, $reduce, $condition);
  742. $this->assertArraySubset(
  743. [
  744. 'retval' => [['count' => 1.0]],
  745. 'count' => 1.0,
  746. 'keys' => 1,
  747. 'ok' => 1.0,
  748. ],
  749. $result
  750. );
  751. }
  752. public function testFindAndModifyResultException()
  753. {
  754. $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
  755. $collection = $this->getCollection();
  756. $this->setExpectedException('MongoResultException');
  757. $collection->findAndModify(
  758. array("inprogress" => false, "name" => "Next promo"),
  759. array('$unsupportedOperator' => array("tasks" => -1)),
  760. array("tasks" => true),
  761. array("new" => true)
  762. );
  763. }
  764. public function testFindAndModifyExceptionTimeout()
  765. {
  766. $this->failMaxTimeMS();
  767. $id = '54203e08d51d4a1f868b456e';
  768. $collection = $this->getCollection();
  769. $this->setExpectedException('MongoExecutionTimeoutException');
  770. $document = $collection->findAndModify(
  771. ['_id' => new \MongoId($id)],
  772. null,
  773. null,
  774. ['maxTimeMS' => 1, 'remove' => true]
  775. );
  776. }
  777. public function testFindAndModifyRemove()
  778. {
  779. $id = '54203e08d51d4a1f868b456e';
  780. $collection = $this->getCollection();
  781. $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
  782. $collection->insert($document);
  783. $document = $collection->findAndModify(
  784. ['_id' => new \MongoId($id)],
  785. null,
  786. null,
  787. ['remove' => true]
  788. );
  789. $this->assertEquals('bar', $document['foo']);
  790. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  791. $this->assertSame(0, $newCollection->count());
  792. }
  793. public function testValidate()
  794. {
  795. $collection = $this->getCollection();
  796. $document = ['foo' => 'bar'];
  797. $collection->insert($document);
  798. $result = $collection->validate();
  799. $this->assertArraySubset(
  800. [
  801. 'ns' => 'mongo-php-adapter.test',
  802. 'nrecords' => 1,
  803. 'nIndexes' => 1,
  804. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  805. 'valid' => true,
  806. 'errors' => [],
  807. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  808. 'ok' => 1.0
  809. ],
  810. $result
  811. );
  812. }
  813. public function testDrop()
  814. {
  815. $document = ['foo' => 'bar'];
  816. $this->getCollection()->insert($document);
  817. $expected = [
  818. 'ns' => (string) $this->getCollection(),
  819. 'nIndexesWas' => 1,
  820. 'ok' => 1.0
  821. ];
  822. $this->assertSame($expected, $this->getCollection()->drop());
  823. }
  824. public function testEmptyCollectionName()
  825. {
  826. $this->setExpectedException('Exception', 'Collection name cannot be empty');
  827. new \MongoCollection($this->getDatabase(), '');
  828. }
  829. public function testSelectCollectionWithNullBytes()
  830. {
  831. $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
  832. new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
  833. }
  834. public function testSubCollectionWithNullBytes()
  835. {
  836. $collection = $this->getCollection();
  837. $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
  838. $this->assertSame('test', $collection->getName());
  839. }
  840. }
  841. class PrivatePropertiesStub
  842. {
  843. private $foo = 'bar';
  844. }