MongoCursorTest.php 11 KB

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