MongoCursorTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\ReadPreference;
  4. use MongoDB\Operation\Find;
  5. /**
  6. * @author alcaeus <alcaeus@alcaeus.org>
  7. */
  8. class MongoCursorTest extends TestCase
  9. {
  10. public function testCursorConvertsTypes()
  11. {
  12. $this->prepareData();
  13. $collection = $this->getCollection();
  14. $cursor = $collection->find(['foo' => 'bar']);
  15. $this->assertCount(2, $cursor);
  16. $iterated = 0;
  17. foreach ($cursor as $item) {
  18. $iterated++;
  19. $this->assertInstanceOf('MongoId', $item['_id']);
  20. $this->assertSame('bar', $item['foo']);
  21. }
  22. $this->assertSame(2, $iterated);
  23. }
  24. public function testCount()
  25. {
  26. $this->prepareData();
  27. $collection = $this->getCollection();
  28. $cursor = $collection->find(['foo' => 'bar'])->limit(1);
  29. $this->assertSame(2, $cursor->count());
  30. $this->assertSame(1, $cursor->count(true));
  31. }
  32. /**
  33. * @dataProvider getCursorOptions
  34. */
  35. public function testCursorAppliesOptions($checkOptionCallback, \Closure $applyOptionCallback = null)
  36. {
  37. $query = ['foo' => 'bar'];
  38. $projection = ['_id' => false, 'foo' => true];
  39. $collectionMock = $this->getCollectionMock();
  40. $collectionMock
  41. ->expects($this->once())
  42. ->method('find')
  43. ->with($this->equalTo($query), $this->callback($checkOptionCallback))
  44. ->will($this->returnValue(new \ArrayIterator([])));
  45. $collection = $this->getCollection('test');
  46. $cursor = $collection->find($query, $projection);
  47. // Replace the original MongoDB collection with our mock
  48. $reflectionProperty = new \ReflectionProperty($cursor, 'collection');
  49. $reflectionProperty->setAccessible(true);
  50. $reflectionProperty->setValue($cursor, $collectionMock);
  51. if ($applyOptionCallback !== null) {
  52. $applyOptionCallback($cursor);
  53. }
  54. // Force query by converting to array
  55. iterator_to_array($cursor);
  56. }
  57. public static function getCursorOptions()
  58. {
  59. function getMissingOptionCallback($optionName) {
  60. return function ($value) use ($optionName) {
  61. return
  62. is_array($value) &&
  63. ! array_key_exists($optionName, $value);
  64. };
  65. }
  66. function getBasicCheckCallback($expected, $optionName) {
  67. return function ($value) use ($expected, $optionName) {
  68. return
  69. is_array($value) &&
  70. array_key_exists($optionName, $value) &&
  71. $value[$optionName] == $expected;
  72. };
  73. }
  74. function getModifierCheckCallback($expected, $modifierName) {
  75. return function ($value) use ($expected, $modifierName) {
  76. return
  77. is_array($value) &&
  78. is_array($value['modifiers']) &&
  79. array_key_exists($modifierName, $value['modifiers']) &&
  80. $value['modifiers'][$modifierName] == $expected;
  81. };
  82. }
  83. $tests = [
  84. 'allowPartialResults' => [
  85. getBasicCheckCallback(true, 'allowPartialResults'),
  86. function (\MongoCursor $cursor) {
  87. $cursor->partial(true);
  88. },
  89. ],
  90. 'batchSize' => [
  91. getBasicCheckCallback(10, 'batchSize'),
  92. function (\MongoCursor $cursor) {
  93. $cursor->batchSize(10);
  94. },
  95. ],
  96. 'cursorTypeNonTailable' => [
  97. getMissingOptionCallback('cursorType'),
  98. function (\MongoCursor $cursor) {
  99. $cursor
  100. ->tailable(false)
  101. ->awaitData(true);
  102. },
  103. ],
  104. 'cursorTypeTailable' => [
  105. getBasicCheckCallback(Find::TAILABLE, 'cursorType'),
  106. function (\MongoCursor $cursor) {
  107. $cursor->tailable(true);
  108. },
  109. ],
  110. 'cursorTypeTailableAwait' => [
  111. getBasicCheckCallback(Find::TAILABLE_AWAIT, 'cursorType'),
  112. function (\MongoCursor $cursor) {
  113. $cursor->tailable(true)->awaitData(true);
  114. },
  115. ],
  116. 'hint' => [
  117. getModifierCheckCallback('index_name', '$hint'),
  118. function (\MongoCursor $cursor) {
  119. $cursor->hint('index_name');
  120. },
  121. ],
  122. 'limit' => [
  123. getBasicCheckCallback(5, 'limit'),
  124. function (\MongoCursor $cursor) {
  125. $cursor->limit(5);
  126. }
  127. ],
  128. 'maxTimeMS' => [
  129. getBasicCheckCallback(100, 'maxTimeMS'),
  130. function (\MongoCursor $cursor) {
  131. $cursor->maxTimeMS(100);
  132. },
  133. ],
  134. 'noCursorTimeout' => [
  135. getBasicCheckCallback(true, 'noCursorTimeout'),
  136. function (\MongoCursor $cursor) {
  137. $cursor->immortal(true);
  138. },
  139. ],
  140. 'slaveOkay' => [
  141. getBasicCheckCallback(new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED), 'readPreference'),
  142. function (\MongoCursor $cursor) {
  143. $cursor->slaveOkay(true);
  144. },
  145. ],
  146. 'slaveOkayWithReadPreferenceSet' => [
  147. getBasicCheckCallback(new ReadPreference(ReadPreference::RP_SECONDARY), 'readPreference'),
  148. function (\MongoCursor $cursor) {
  149. $cursor
  150. ->setReadPreference(\MongoClient::RP_SECONDARY)
  151. ->slaveOkay(true);
  152. },
  153. ],
  154. 'projectionDefaultFields' => [
  155. getBasicCheckCallback(['_id' => false, 'foo' => true], 'projection'),
  156. ],
  157. 'projectionDifferentFields' => [
  158. getBasicCheckCallback(['_id' => false, 'foo' => true, 'bar' => true], 'projection'),
  159. function (\MongoCursor $cursor) {
  160. $cursor->fields(['_id' => false, 'foo' => true, 'bar' => true]);
  161. },
  162. ],
  163. 'readPreferencePrimary' => [
  164. getBasicCheckCallback(new ReadPreference(ReadPreference::RP_PRIMARY), 'readPreference'),
  165. function (\MongoCursor $cursor) {
  166. $cursor->setReadPreference(\MongoClient::RP_PRIMARY);
  167. },
  168. ],
  169. 'skip' => [
  170. getBasicCheckCallback(5, 'skip'),
  171. function (\MongoCursor $cursor) {
  172. $cursor->skip(5);
  173. },
  174. ],
  175. 'sort' => [
  176. getBasicCheckCallback(['foo' => -1], 'sort'),
  177. function (\MongoCursor $cursor) {
  178. $cursor->sort(['foo' => -1]);
  179. },
  180. ],
  181. ];
  182. return $tests;
  183. }
  184. public function testCursorInfo()
  185. {
  186. $this->prepareData();
  187. $collection = $this->getCollection();
  188. $cursor = $collection->find(['foo' => 'bar'], ['_id' => false])->skip(1)->limit(3);
  189. $expected = [
  190. 'ns' => 'mongo-php-adapter.test',
  191. 'limit' => 3,
  192. 'batchSize' => null,
  193. 'skip' => 1,
  194. 'flags' => 0,
  195. 'query' => ['foo' => 'bar'],
  196. 'fields' => ['_id' => false],
  197. 'started_iterating' => false,
  198. ];
  199. $this->assertSame($expected, $cursor->info());
  200. // Ensure cursor started iterating
  201. iterator_to_array($cursor);
  202. $expected['started_iterating'] = true;
  203. $expected += [
  204. 'id' => '0',
  205. 'at' => null,
  206. 'numReturned' => null,
  207. 'server' => null,
  208. 'host' => 'localhost',
  209. 'port' => 27017,
  210. 'connection_type_desc' => 'STANDALONE'
  211. ];
  212. $this->assertSame($expected, $cursor->info());
  213. }
  214. /**
  215. * @param string $name
  216. * @return \MongoCollection
  217. */
  218. protected function getCollection($name = 'test')
  219. {
  220. $client = new \MongoClient();
  221. return $client->selectCollection('mongo-php-adapter', $name);
  222. }
  223. /**
  224. * @return \PHPUnit_Framework_MockObject_MockObject
  225. */
  226. protected function getCollectionMock()
  227. {
  228. return $this->getMock('MongoDB\Collection', [], [], '', false);
  229. }
  230. /**
  231. * @return \MongoCollection
  232. */
  233. protected function prepareData()
  234. {
  235. $collection = $this->getCollection();
  236. $collection->insert(['foo' => 'bar']);
  237. $collection->insert(['foo' => 'bar']);
  238. $collection->insert(['foo' => 'foo']);
  239. return $collection;
  240. }
  241. }