MongoCollectionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. $id = '54203e08d51d4a1f868b456e';
  17. $collection = $this->getCollection();
  18. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  19. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  20. $this->assertSame(1, $newCollection->count());
  21. $object = $newCollection->findOne();
  22. $this->assertNotNull($object);
  23. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  24. $this->assertSame($id, (string) $object->_id);
  25. $this->assertObjectHasAttribute('foo', $object);
  26. $this->assertAttributeSame('bar', 'foo', $object);
  27. }
  28. public function testFindReturnsCursor()
  29. {
  30. $this->prepareData();
  31. $collection = $this->getCollection();
  32. $this->assertInstanceOf('MongoCursor', $collection->find());
  33. }
  34. public function testCount()
  35. {
  36. $this->prepareData();
  37. $collection = $this->getCollection();
  38. $this->assertSame(3, $collection->count());
  39. $this->assertSame(2, $collection->count(['foo' => 'bar']));
  40. }
  41. public function testFindOne()
  42. {
  43. $this->prepareData();
  44. $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
  45. $this->assertSame(['foo' => 'foo'], $document);
  46. }
  47. public function testDistinct()
  48. {
  49. $this->prepareData();
  50. $values = $this->getCollection()->distinct('foo');
  51. $this->assertInternalType('array', $values);
  52. sort($values);
  53. $this->assertEquals(['bar', 'foo'], $values);
  54. }
  55. public function testDistinctWithQuery()
  56. {
  57. $this->prepareData();
  58. $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
  59. $this->assertInternalType('array', $values);
  60. $this->assertEquals(['bar'], $values);
  61. }
  62. public function testAggregate()
  63. {
  64. $collection = $this->getCollection();
  65. $collection->insert(['foo' => 'bar']);
  66. $collection->insert(['foo' => 'bar']);
  67. $collection->insert(['foo' => 'foo']);
  68. $pipeline = [
  69. [
  70. '$group' => [
  71. '_id' => '$foo',
  72. 'count' => [ '$sum' => 1 ],
  73. ],
  74. ],
  75. [
  76. '$sort' => ['_id' => 1]
  77. ]
  78. ];
  79. $result = $collection->aggregate($pipeline);
  80. $this->assertInternalType('array', $result);
  81. $this->assertArrayHasKey('result', $result);
  82. $this->assertEquals([
  83. ['_id' => 'bar', 'count' => 2],
  84. ['_id' => 'foo', 'count' => 1],
  85. ], $result['result']);
  86. }
  87. public function testAggregateCursor()
  88. {
  89. $collection = $this->getCollection();
  90. $collection->insert(['foo' => 'bar']);
  91. $collection->insert(['foo' => 'bar']);
  92. $collection->insert(['foo' => 'foo']);
  93. $pipeline = [
  94. [
  95. '$group' => [
  96. '_id' => '$foo',
  97. 'count' => [ '$sum' => 1 ],
  98. ],
  99. ],
  100. [
  101. '$sort' => ['_id' => 1]
  102. ]
  103. ];
  104. $cursor = $collection->aggregateCursor($pipeline);
  105. $this->assertInstanceOf('MongoCommandCursor', $cursor);
  106. $this->assertEquals([
  107. ['_id' => 'bar', 'count' => 2],
  108. ['_id' => 'foo', 'count' => 1],
  109. ], iterator_to_array($cursor));
  110. }
  111. public function testReadPreference()
  112. {
  113. $collection = $this->getCollection();
  114. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  115. $this->assertFalse($collection->getSlaveOkay());
  116. $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']));
  117. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  118. $this->assertTrue($collection->getSlaveOkay());
  119. // Only way to check whether options are passed down is through debugInfo
  120. $writeConcern = $collection->getCollection()->__debugInfo()['readPreference'];
  121. $this->assertSame(ReadPreference::RP_SECONDARY, $writeConcern->getMode());
  122. $this->assertSame(['a' => 'b'], $writeConcern->getTagSets());
  123. $this->assertTrue($collection->setSlaveOkay(true));
  124. $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  125. $this->assertTrue($collection->setSlaveOkay(false));
  126. $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
  127. }
  128. public function testReadPreferenceIsInherited()
  129. {
  130. $database = $this->getDatabase();
  131. $database->setReadPreference(\MongoClient::RP_SECONDARY, ['a' => 'b']);
  132. $collection = $database->selectCollection('test');
  133. $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => ['a' => 'b']], $collection->getReadPreference());
  134. }
  135. public function testWriteConcern()
  136. {
  137. $collection = $this->getCollection();
  138. $this->assertSame(['w' => 1, 'wtimeout' => 0], $collection->getWriteConcern());
  139. $this->assertSame(1, $collection->w);
  140. $this->assertSame(0, $collection->wtimeout);
  141. $this->assertTrue($collection->setWriteConcern('majority', 100));
  142. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  143. $collection->w = 2;
  144. $this->assertSame(['w' => 2, 'wtimeout' => 100], $collection->getWriteConcern());
  145. $collection->wtimeout = -1;
  146. $this->assertSame(['w' => 2, 'wtimeout' => 0], $collection->getWriteConcern());
  147. // Only way to check whether options are passed down is through debugInfo
  148. $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
  149. $this->assertSame(2, $writeConcern->getW());
  150. $this->assertSame(0, $writeConcern->getWtimeout());
  151. }
  152. public function testWriteConcernIsInherited()
  153. {
  154. $database = $this->getDatabase();
  155. $database->setWriteConcern('majority', 100);
  156. $collection = $database->selectCollection('test');
  157. $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
  158. }
  159. public function testSaveInsert()
  160. {
  161. $id = '54203e08d51d4a1f868b456e';
  162. $collection = $this->getCollection();
  163. $collection->save(['_id' => new \MongoId($id), 'foo' => 'bar']);
  164. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  165. $this->assertSame(1, $newCollection->count());
  166. $object = $newCollection->findOne();
  167. $this->assertNotNull($object);
  168. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  169. $this->assertSame($id, (string) $object->_id);
  170. $this->assertObjectHasAttribute('foo', $object);
  171. $this->assertAttributeSame('bar', 'foo', $object);
  172. }
  173. public function testSaveUpdate()
  174. {
  175. $id = '54203e08d51d4a1f868b456e';
  176. $collection = $this->getCollection();
  177. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  178. $collection->save(['_id' => new \MongoId($id), 'foo' => 'foo']);
  179. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  180. $this->assertSame(1, $newCollection->count());
  181. $object = $newCollection->findOne();
  182. $this->assertNotNull($object);
  183. $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
  184. $this->assertSame($id, (string) $object->_id);
  185. $this->assertObjectHasAttribute('foo', $object);
  186. $this->assertAttributeSame('foo', 'foo', $object);
  187. }
  188. public function testGetDBRef()
  189. {
  190. $collection = $this->getCollection();
  191. $collection->insert(['_id' => 1, 'foo' => 'bar']);
  192. $document = $collection->getDBRef([
  193. '$ref' => 'test',
  194. '$id' => 1,
  195. ]);
  196. $this->assertEquals(['_id' => 1, 'foo' => 'bar'], $document);
  197. }
  198. public function testCreateDBRef()
  199. {
  200. $collection = $this->getCollection();
  201. $reference = $collection->createDBRef(['_id' => 'foo']);
  202. $this->assertSame(
  203. [
  204. '$ref' => 'test',
  205. '$id' => 'foo',
  206. ],
  207. $reference
  208. );
  209. }
  210. public function testCreateIndex()
  211. {
  212. $collection = $this->getCollection();
  213. $collection->createIndex(['foo' => 1]);
  214. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  215. $iterator = $newCollection->listIndexes();
  216. $indexes = iterator_to_array($iterator);
  217. $this->assertCount(2, $indexes);
  218. $index = $indexes[1];
  219. $this->assertSame(['foo' => 1], $index->getKey());
  220. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  221. }
  222. public function testEnsureIndex()
  223. {
  224. $collection = $this->getCollection();
  225. $this->assertTrue($collection->ensureIndex(['bar' => 1], ['unique' => true]));
  226. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  227. $indexes = iterator_to_array($newCollection->listIndexes());
  228. $this->assertCount(2, $indexes);
  229. $index = $indexes[1];
  230. $this->assertSame(['bar' => 1], $index->getKey());
  231. $this->assertTrue($index->isUnique());
  232. $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
  233. }
  234. public function testDeleteIndexUsingIndexName()
  235. {
  236. $collection = $this->getCollection();
  237. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  238. $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
  239. $collection->deleteIndex('bar');
  240. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  241. }
  242. public function testdeleteindexusingkeys()
  243. {
  244. $collection = $this->getcollection();
  245. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  246. $newCollection->createIndex(['bar' => 1]);
  247. $collection->deleteIndex(['bar' => 1]);
  248. $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
  249. }
  250. public function testDeleteIndexes()
  251. {
  252. $collection = $this->getcollection();
  253. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  254. $newCollection->createIndex(['bar' => 1]);
  255. $collection->deleteIndexes();
  256. $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
  257. }
  258. public function testGetIndexInfo()
  259. {
  260. $this->prepareData();
  261. $this->assertSame(
  262. [
  263. [
  264. 'v' => 1,
  265. 'key' => ['_id' => 1],
  266. 'name' => '_id_',
  267. 'ns' => 'mongo-php-adapter.test',
  268. ],
  269. ],
  270. $this->getcollection()->getIndexInfo()
  271. );
  272. }
  273. public function testFindAndModifyUpdate()
  274. {
  275. $id = '54203e08d51d4a1f868b456e';
  276. $collection = $this->getCollection();
  277. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  278. $document = $collection->findAndModify(
  279. ['_id' => new \MongoId($id)],
  280. ['$set' => ['foo' => 'foo']]
  281. );
  282. $this->assertSame('bar', $document['foo']);
  283. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  284. $this->assertSame(1, $newCollection->count());
  285. $object = $newCollection->findOne();
  286. $this->assertNotNull($object);
  287. $this->assertAttributeSame('foo', 'foo', $object);
  288. }
  289. public function testFindAndModifyUpdateReturnNew()
  290. {
  291. $id = '54203e08d51d4a1f868b456e';
  292. $collection = $this->getCollection();
  293. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  294. $document = $collection->findAndModify(
  295. ['_id' => new \MongoId($id)],
  296. ['$set' => ['foo' => 'foo']],
  297. null,
  298. ['new' => true]
  299. );
  300. $this->assertSame('foo', $document['foo']);
  301. }
  302. public function testFindAndModifyWithFields()
  303. {
  304. $id = '54203e08d51d4a1f868b456e';
  305. $collection = $this->getCollection();
  306. $collection->insert([
  307. '_id' => new \MongoId($id),
  308. 'foo' => 'bar',
  309. 'bar' => 'foo',
  310. ]);
  311. $document = $collection->findAndModify(
  312. ['_id' => new \MongoId($id)],
  313. ['$set' => ['foo' => 'foo']],
  314. ['foo' => true]
  315. );
  316. $this->assertArrayNotHasKey('bar', $document);
  317. $this->assertArrayHasKey('foo', $document);
  318. }
  319. public function testGroup()
  320. {
  321. $collection = $this->getCollection();
  322. $collection->insert(['a' => 2]);
  323. $collection->insert(['b' => 5]);
  324. $collection->insert(['a' => 1]);
  325. $keys = [];
  326. $initial = ["count" => 0];
  327. $reduce = "function (obj, prev) { prev.count++; }";
  328. $condition = ['condition' => ["a" => [ '$gt' => 1]]];
  329. $result = $collection->group($keys, $initial, $reduce, $condition);
  330. $this->assertEquals(
  331. [
  332. 'retval' => ['count' => 1],
  333. 'count' => 1,
  334. 'keys' => 1,
  335. 'ok' => 1,
  336. ],
  337. $result
  338. );
  339. }
  340. public function testFindAndModifyRemove()
  341. {
  342. $id = '54203e08d51d4a1f868b456e';
  343. $collection = $this->getCollection();
  344. $collection->insert(['_id' => new \MongoId($id), 'foo' => 'bar']);
  345. $document = $collection->findAndModify(
  346. ['_id' => new \MongoId($id)],
  347. null,
  348. null,
  349. ['remove' => true]
  350. );
  351. $newCollection = $this->getCheckDatabase()->selectCollection('test');
  352. $this->assertSame(0, $newCollection->count());
  353. }
  354. public function testValidate()
  355. {
  356. $collection = $this->getCollection();
  357. $collection->insert(['foo' => 'bar']);
  358. $result = $collection->validate();
  359. $this->assertArraySubset(
  360. [
  361. 'ns' => 'mongo-php-adapter.test',
  362. 'nrecords' => 1,
  363. 'nIndexes' => 1,
  364. 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
  365. 'valid' => true,
  366. 'errors' => [],
  367. 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
  368. 'ok' => 1.0
  369. ],
  370. $result
  371. );
  372. }
  373. /**
  374. * @return \MongoCollection
  375. */
  376. protected function getCollection($name = 'test')
  377. {
  378. return $this->getDatabase()->selectCollection($name);
  379. }
  380. /**
  381. * @return \MongoDB
  382. */
  383. protected function getDatabase()
  384. {
  385. $client = new \MongoClient();
  386. return $client->selectDB('mongo-php-adapter');
  387. }
  388. /**
  389. * @return \MongoCollection
  390. */
  391. protected function prepareData()
  392. {
  393. $collection = $this->getCollection();
  394. $collection->insert(['foo' => 'bar']);
  395. $collection->insert(['foo' => 'bar']);
  396. $collection->insert(['foo' => 'foo']);
  397. return $collection;
  398. }
  399. }