MongoCommandCursorTest.php 1.8 KB

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