MongoCursorTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. /**
  4. * @author alcaeus <alcaeus@alcaeus.org>
  5. */
  6. class MongoCursorTest extends TestCase
  7. {
  8. public function testCursorConvertsTypes()
  9. {
  10. $this->prepareData();
  11. $collection = $this->getCollection();
  12. $cursor = $collection->find(['foo' => 'bar']);
  13. $this->assertCount(2, $cursor);
  14. $iterated = 0;
  15. foreach ($cursor as $item) {
  16. $iterated++;
  17. $this->assertInstanceOf('MongoId', $item['_id']);
  18. $this->assertSame('bar', $item['foo']);
  19. }
  20. $this->assertSame(2, $iterated);
  21. }
  22. public function testCount()
  23. {
  24. $this->prepareData();
  25. $collection = $this->getCollection();
  26. $cursor = $collection->find(['foo' => 'bar'])->limit(1);
  27. $this->assertSame(2, $cursor->count());
  28. $this->assertSame(1, $cursor->count(true));
  29. }
  30. /**
  31. * @return \MongoCollection
  32. */
  33. protected function getCollection($name = 'test')
  34. {
  35. $client = new \MongoClient();
  36. return $client->selectCollection('mongo-php-adapter', $name);
  37. }
  38. /**
  39. * @return \MongoCollection
  40. */
  41. protected function prepareData()
  42. {
  43. $collection = $this->getCollection();
  44. $collection->insert(['foo' => 'bar']);
  45. $collection->insert(['foo' => 'bar']);
  46. $collection->insert(['foo' => 'foo']);
  47. return $collection;
  48. }
  49. }