MongoGridFSFileTest.php 3.3 KB

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