MongoGridFSFileTest.php 3.5 KB

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