MongoCursorTest.php 11 KB

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