MongoCursorTest.php 8.6 KB

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