MongoCommandCursorTest.php 6.3 KB

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