MongoCursorTest.php 10.0 KB

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