MongoGridFSCursorTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. class MongoGridFSCursorTest extends TestCase
  4. {
  5. public function testCursor()
  6. {
  7. $cursor = $this->getCursor();
  8. $array = iterator_to_array($cursor);
  9. $this->assertCount(2, $array);
  10. $this->assertArrayHasKey('One.txt', $array);
  11. $this->assertArrayHasKey('Two.txt', $array);
  12. $firstFile = $array['One.txt'];
  13. $this->assertInstanceOf('MongoGridFSFile', $firstFile);
  14. $this->assertArraySubset(['length' => 3, 'filename' => 'One.txt'], $firstFile->file);
  15. $secondFile = $array['Two.txt'];
  16. $this->assertInstanceOf('MongoGridFSFile', $secondFile);
  17. $this->assertArraySubset(['length' => 3, 'filename' => 'Two.txt'], $secondFile->file);
  18. }
  19. private function getCursor()
  20. {
  21. $gridFS = $this->getGridFS();
  22. $gridFS->storeBytes('One', ['filename' => 'One.txt']);
  23. $gridFS->storeBytes('Two', ['filename' => 'Two.txt']);
  24. return $gridFS->find();
  25. }
  26. /**
  27. * @param string $name
  28. * @param \MongoDB|null $database
  29. * @return \MongoGridFS
  30. */
  31. private function getGridFS($name = 'testfs', \MongoDB $database = null)
  32. {
  33. if ($database === null) {
  34. $database = $this->getDatabase();
  35. }
  36. return new \MongoGridFS($database, $name);
  37. }
  38. }