MongoGridFSFileTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Alcaeus\MongoDbAdapter\Tests;
  3. class MongoGridFSFileTest extends TestCase
  4. {
  5. public function testFileProperty()
  6. {
  7. $file = $this->getFile();
  8. $this->assertArrayHasKey('_id', $file->file);
  9. $this->assertArraySubset(
  10. [
  11. 'length' => 666,
  12. 'filename' => 'file',
  13. 'md5' => 'md5',
  14. ],
  15. $file->file
  16. );
  17. }
  18. public function testGetFilename()
  19. {
  20. $file = $this->getFile();
  21. $this->assertSame('file', $file->getFilename());
  22. }
  23. public function testGetSize()
  24. {
  25. $file = $this->getFile();
  26. $this->assertSame(666, $file->getSize());
  27. }
  28. public function testWrite()
  29. {
  30. $id = $this->prepareFile();
  31. $filename = '/tmp/test-mongo-grid-fs-file';
  32. @unlink($filename);
  33. $file = $this->getFile(['_id' => $id, 'length' => 4, 'filename' => $filename]);
  34. $file->write();
  35. $this->assertTrue(file_exists($filename));
  36. $this->assertSame('e2fc714c4727ee9395f324cd2e7f331f', md5_file($filename));
  37. unlink($filename);
  38. }
  39. public function testWriteSpecifyFilename()
  40. {
  41. $id = $this->prepareFile();
  42. $filename = '/tmp/test-mongo-grid-fs-file';
  43. @unlink($filename);
  44. $file = $this->getFile(['_id' => $id, 'length' => 4]);
  45. $file->write($filename);
  46. $this->assertTrue(file_exists($filename));
  47. $this->assertSame('e2fc714c4727ee9395f324cd2e7f331f', md5_file($filename));
  48. unlink($filename);
  49. }
  50. public function testGetBytes()
  51. {
  52. $id = $this->prepareFile();
  53. $file = $this->getFile(['_id' => $id, 'length' => 4]);
  54. $result = $file->getBytes();
  55. $this->assertSame('abcd', $result);
  56. }
  57. public function testGetResource()
  58. {
  59. $id = $this->prepareFile();
  60. $file = $this->getFile(['_id' => $id, 'length' => 4]);
  61. $result = $file->getResource();
  62. $this->assertTrue(is_resource($result));
  63. $this->assertSame('abcd', stream_get_contents($result));
  64. }
  65. /**
  66. * @var \MongoGridFSFile
  67. */
  68. protected function getFile($extra = [])
  69. {
  70. $file = [
  71. '_id' => new \MongoID(),
  72. 'length' => 666,
  73. 'filename' => 'file',
  74. 'md5' => 'md5',
  75. ];
  76. $file = array_merge($file, $extra);
  77. return new \MongoGridFSFile($this->getGridFS(), $file);
  78. }
  79. /**
  80. * @var \MongoID
  81. */
  82. protected function prepareFile($data = 'abcd', $extra = [])
  83. {
  84. $collection = $this->getGridFS();
  85. // to make sure we have multiple chunks
  86. $extra += ['chunkSize' => 2];
  87. return $collection->storeBytes($data, $extra);
  88. }
  89. /**
  90. * @param string $name
  91. * @param \MongoDB|null $database
  92. * @return \MongoGridFS
  93. */
  94. protected function getGridFS($name = 'testfs', \MongoDB $database = null)
  95. {
  96. if ($database === null) {
  97. $database = $this->getDatabase();
  98. }
  99. return new \MongoGridFS($database, $name);
  100. }
  101. }