MongoGridFSFileTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $filename = '/tmp/test-mongo-grid-fs-file';
  31. $id = $this->prepareFile('abcd', ['filename' => $filename]);
  32. @unlink($filename);
  33. $file = $this->getGridFS()->findOne(['_id' => $id]);
  34. $this->assertInstanceOf(\MongoGridFSFile::class, $file);
  35. $file->write();
  36. $this->assertTrue(file_exists($filename));
  37. $this->assertSame('e2fc714c4727ee9395f324cd2e7f331f', md5_file($filename));
  38. unlink($filename);
  39. }
  40. public function testWriteSpecifyFilename()
  41. {
  42. $id = $this->prepareFile();
  43. $filename = '/tmp/test-mongo-grid-fs-file';
  44. @unlink($filename);
  45. $file = $this->getGridFS()->findOne(['_id' => $id]);
  46. $this->assertInstanceOf(\MongoGridFSFile::class, $file);
  47. $file->write($filename);
  48. $this->assertTrue(file_exists($filename));
  49. $this->assertSame('e2fc714c4727ee9395f324cd2e7f331f', md5_file($filename));
  50. unlink($filename);
  51. }
  52. public function testGetBytes()
  53. {
  54. $id = $this->prepareFile();
  55. $file = $this->getFile(['_id' => $id, 'length' => 4]);
  56. $result = $file->getBytes();
  57. $this->assertSame('abcd', $result);
  58. }
  59. public function testGetResource()
  60. {
  61. $data = str_repeat('a', 500 * 1024);
  62. $id = $this->prepareFile($data);
  63. $file = $this->getGridFS()->findOne(['_id' => $id]);
  64. $this->assertInstanceOf(\MongoGridFSFile::class, $file);
  65. $result = $file->getResource();
  66. $this->assertTrue(is_resource($result));
  67. $this->assertSame($data, stream_get_contents($result));
  68. }
  69. /**
  70. * @var \MongoGridFSFile
  71. */
  72. protected function getFile($extra = [])
  73. {
  74. $file = [
  75. '_id' => new \MongoID(),
  76. 'length' => 666,
  77. 'filename' => 'file',
  78. 'md5' => 'md5',
  79. ];
  80. $file = array_merge($file, $extra);
  81. return new \MongoGridFSFile($this->getGridFS(), $file);
  82. }
  83. /**
  84. * @var \MongoID
  85. */
  86. protected function prepareFile($data = 'abcd', $extra = [])
  87. {
  88. $collection = $this->getGridFS();
  89. return $collection->storeBytes($data, $extra);
  90. }
  91. /**
  92. * @param string $name
  93. * @param \MongoDB|null $database
  94. * @return \MongoGridFS
  95. */
  96. protected function getGridFS($name = 'testfs', \MongoDB $database = null)
  97. {
  98. if ($database === null) {
  99. $database = $this->getDatabase();
  100. }
  101. return new \MongoGridFS($database, $name);
  102. }
  103. }