MongoCommandCursorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
  3. use MongoCursorInterface;
  4. use MongoDB\Database;
  5. use MongoDB\Driver\ReadPreference;
  6. use Alcaeus\MongoDbAdapter\Tests\TestCase;
  7. /**
  8. * @author alcaeus <alcaeus@alcaeus.org>
  9. */
  10. class MongoCommandCursorTest extends TestCase
  11. {
  12. public function testSerialize()
  13. {
  14. $this->prepareData();
  15. $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
  16. $this->assertIsString(serialize($cursor));
  17. }
  18. public function testInfo()
  19. {
  20. $this->prepareData();
  21. $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
  22. $expected = [
  23. 'ns' => 'mongo-php-adapter.test',
  24. 'limit' => 0,
  25. 'batchSize' => 0,
  26. 'skip' => 0,
  27. 'flags' => 0,
  28. 'query' => [
  29. 'aggregate' => 'test',
  30. 'pipeline' => [
  31. [
  32. '$match' => ['foo' => 'bar']
  33. ]
  34. ],
  35. 'cursor' => new \stdClass(),
  36. ],
  37. 'fields' => null,
  38. 'started_iterating' => false,
  39. ];
  40. $info = $cursor->info();
  41. $this->assertEquals($expected, $info);
  42. // Ensure cursor started iterating
  43. $array = iterator_to_array($cursor);
  44. $expected['started_iterating'] = true;
  45. $expected += [
  46. 'id' => 0,
  47. 'at' => 0,
  48. 'numReturned' => 0,
  49. 'server' => 'localhost:27017;-;.;' . getmypid(),
  50. 'host' => 'localhost',
  51. 'port' => 27017,
  52. 'connection_type_desc' => 'STANDALONE',
  53. ];
  54. $this->assertMatches($expected, $cursor->info());
  55. $i = 0;
  56. foreach ($array as $key => $value) {
  57. $this->assertEquals($i, $key);
  58. $i++;
  59. }
  60. }
  61. /**
  62. * @dataProvider dataCommandAppliesCorrectReadPreference
  63. */
  64. public function testCommandAppliesCorrectReadPreference($command, $expectedReadPreference)
  65. {
  66. $this->skipTestIf(extension_loaded('mongo'));
  67. $checkReadPreference = function ($other) use ($expectedReadPreference) {
  68. if (!is_array($other)) {
  69. return false;
  70. }
  71. if (!array_key_exists('readPreference', $other)) {
  72. return false;
  73. }
  74. if (!$other['readPreference'] instanceof ReadPreference) {
  75. return false;
  76. }
  77. return $other['readPreference']->getMode() === $expectedReadPreference;
  78. };
  79. $databaseMock = $this->createMock(Database::class);
  80. $databaseMock
  81. ->expects($this->once())
  82. ->method('command')
  83. ->with($this->anything(), $this->callback($checkReadPreference))
  84. ->will($this->returnValue(new \ArrayIterator()));
  85. $cursor = new \MongoCommandCursor($this->getClient(), (string) $this->getDatabase(), $command);
  86. $reflection = new \ReflectionProperty($cursor, 'db');
  87. $reflection->setAccessible(true);
  88. $reflection->setValue($cursor, $databaseMock);
  89. $cursor->setReadPreference(\MongoClient::RP_SECONDARY);
  90. iterator_to_array($cursor);
  91. self::assertSame(\MongoClient::RP_SECONDARY, $cursor->getReadPreference()['type']);
  92. }
  93. public function dataCommandAppliesCorrectReadPreference()
  94. {
  95. return [
  96. 'findAndUpdate' => [
  97. [
  98. 'findandmodify' => (string) $this->getCollection(),
  99. 'query' => [],
  100. 'update' => ['$inc' => ['field' => 1]],
  101. ],
  102. ReadPreference::RP_PRIMARY,
  103. ],
  104. 'findAndRemove' => [
  105. [
  106. 'findandremove' => (string) $this->getCollection(),
  107. 'query' => [],
  108. ],
  109. ReadPreference::RP_PRIMARY,
  110. ],
  111. 'mapReduceWithOut' => [
  112. [
  113. 'mapReduce' => (string) $this->getCollection(),
  114. 'out' => 'sample',
  115. ],
  116. ReadPreference::RP_PRIMARY,
  117. ],
  118. 'mapReduceWithOutInline' => [
  119. [
  120. 'mapReduce' => (string) $this->getCollection(),
  121. 'out' => ['inline' => 1],
  122. ],
  123. ReadPreference::RP_SECONDARY,
  124. ],
  125. 'count' => [
  126. [
  127. 'count' => (string) $this->getCollection(),
  128. ],
  129. ReadPreference::RP_SECONDARY,
  130. ],
  131. 'group' => [
  132. [
  133. 'group' => (string) $this->getCollection(),
  134. ],
  135. ReadPreference::RP_SECONDARY,
  136. ],
  137. 'dbStats' => [
  138. [
  139. 'dbStats' => (string) $this->getCollection(),
  140. ],
  141. ReadPreference::RP_SECONDARY,
  142. ],
  143. 'geoNear' => [
  144. [
  145. 'geoNear' => (string) $this->getCollection(),
  146. ],
  147. ReadPreference::RP_SECONDARY,
  148. ],
  149. 'geoWalk' => [
  150. [
  151. 'geoWalk' => (string) $this->getCollection(),
  152. ],
  153. ReadPreference::RP_SECONDARY,
  154. ],
  155. 'distinct' => [
  156. [
  157. 'distinct' => (string) $this->getCollection(),
  158. ],
  159. ReadPreference::RP_SECONDARY,
  160. ],
  161. 'aggregate' => [
  162. [
  163. 'aggregate' => (string) $this->getCollection(),
  164. ],
  165. ReadPreference::RP_SECONDARY,
  166. ],
  167. 'collStats' => [
  168. [
  169. 'collStats' => (string) $this->getCollection(),
  170. ],
  171. ReadPreference::RP_SECONDARY,
  172. ],
  173. 'geoSearch' => [
  174. [
  175. 'geoSearch' => (string) $this->getCollection(),
  176. ],
  177. ReadPreference::RP_SECONDARY,
  178. ],
  179. 'parallelCollectionScan' => [
  180. [
  181. 'parallelCollectionScan' => (string) $this->getCollection(),
  182. ],
  183. ReadPreference::RP_SECONDARY,
  184. ],
  185. ];
  186. }
  187. public function testInterfaces()
  188. {
  189. $this->prepareData();
  190. $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
  191. $this->assertInstanceOf(MongoCursorInterface::class, $cursor);
  192. }
  193. }