MongoCursorTest.php 11 KB

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