MongoCommandCursorTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. use MongoDB\Driver\ReadPreference;
  4. use MongoDB\Operation\Find;
  5. /**
  6. * @author alcaeus <alcaeus@alcaeus.org>
  7. */
  8. class MongoCommandCursorTest extends TestCase
  9. {
  10. public function testInfo()
  11. {
  12. $this->prepareData();
  13. $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
  14. $expected = [
  15. 'ns' => 'mongo-php-adapter.test',
  16. 'limit' => 0,
  17. 'batchSize' => null,
  18. 'skip' => 0,
  19. 'flags' => 0,
  20. 'query' => [
  21. 'aggregate' => 'test',
  22. 'pipeline' => [
  23. [
  24. '$match' => ['foo' => 'bar']
  25. ]
  26. ],
  27. 'cursor' => new \stdClass()
  28. ],
  29. 'fields' => null,
  30. 'started_iterating' => false,
  31. ];
  32. $this->assertEquals($expected, $cursor->info());
  33. // Ensure cursor started iterating
  34. iterator_to_array($cursor);
  35. $expected['started_iterating'] = true;
  36. $expected += [
  37. 'id' => '0',
  38. 'at' => null,
  39. 'numReturned' => null,
  40. 'server' => null,
  41. 'host' => 'localhost',
  42. 'port' => 27017,
  43. 'connection_type_desc' => 'STANDALONE'
  44. ];
  45. $this->assertEquals($expected, $cursor->info());
  46. }
  47. /**
  48. * @param string $name
  49. * @return \MongoCollection
  50. */
  51. protected function getCollection($name = 'test')
  52. {
  53. $client = new \MongoClient();
  54. return $client->selectCollection('mongo-php-adapter', $name);
  55. }
  56. /**
  57. * @return \MongoCollection
  58. */
  59. protected function prepareData()
  60. {
  61. $collection = $this->getCollection();
  62. $collection->insert(['foo' => 'bar']);
  63. $collection->insert(['foo' => 'bar']);
  64. $collection->insert(['foo' => 'foo']);
  65. return $collection;
  66. }
  67. }