MongoCursorTest.php 12 KB

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