MongoCursorTest.php 11 KB

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