MongoCursorTest.php 8.9 KB

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