MongoCursorTest.php 11 KB

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