GridFSSpec.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. use MongoDB\BSON\UTCDateTime;
  4. use MongoDB\GridFS\Bucket;
  5. use MongoDB\GridFS\Exception\FileNotFoundException;
  6. use MongoDB\Model\BSONDocument;
  7. use PhpSpec\ObjectBehavior;
  8. use Prophecy\Argument;
  9. class GridFSSpec extends ObjectBehavior
  10. {
  11. private $resources = [];
  12. function let(Bucket $bucket)
  13. {
  14. $this->beConstructedWith($bucket);
  15. }
  16. function letGo()
  17. {
  18. array_map(function ($res) {
  19. @fclose($res);
  20. }, $this->resources);
  21. }
  22. function it_is_adapter()
  23. {
  24. $this->shouldHaveType('Gaufrette\Adapter');
  25. }
  26. function it_is_checksum_calculator()
  27. {
  28. $this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
  29. }
  30. function it_supports_metadata()
  31. {
  32. $this->shouldHaveType('Gaufrette\Adapter\MetadataSupporter');
  33. }
  34. function it_supports_native_list_keys()
  35. {
  36. $this->shouldHaveType('Gaufrette\Adapter\ListKeysAware');
  37. }
  38. function it_reads_file($bucket)
  39. {
  40. $this->resources[] = $readable = fopen('php://memory', 'rw');
  41. fwrite($readable, 'some content');
  42. fseek($readable, 0);
  43. $bucket
  44. ->openDownloadStreamByName('filename')
  45. ->shouldBeCalled()
  46. ->willReturn($readable)
  47. ;
  48. $this->read('filename')->shouldReturn('some content');
  49. }
  50. function it_does_not_fail_when_cannot_read($bucket)
  51. {
  52. $bucket->openDownloadStreamByName('filename')->willThrow(FileNotFoundException::class);
  53. $this->read('filename')->shouldReturn(false);
  54. }
  55. function it_checks_if_file_exists($bucket, BSONDocument $file)
  56. {
  57. $bucket
  58. ->findOne(['filename' => 'filename'])
  59. ->willReturn($file)
  60. ;
  61. $bucket
  62. ->findOne(['filename' => 'filename2'])
  63. ->willReturn(null)
  64. ;
  65. $this->exists('filename')->shouldReturn(true);
  66. $this->exists('filename2')->shouldReturn(false);
  67. }
  68. function it_deletes_file($bucket)
  69. {
  70. $bucket
  71. ->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])
  72. ->willReturn($file = new BSONDocument(['_id' => 123]))
  73. ;
  74. $bucket->delete(123)->shouldBeCalled();
  75. $this->delete('filename')->shouldReturn(true);
  76. }
  77. function it_does_not_delete_file($bucket)
  78. {
  79. $bucket->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])->willReturn(null);
  80. $this->delete('filename')->shouldReturn(false);
  81. }
  82. function it_writes_file($bucket)
  83. {
  84. $this->resources[] = $writable = fopen('php://memory', 'rw');
  85. $bucket
  86. ->openUploadStream('filename', ['metadata' => ['someother' => 'metadata']])
  87. ->willReturn($writable)
  88. ;
  89. $this->setMetadata('filename', ['someother' => 'metadata']);
  90. $this
  91. ->write('filename', 'some content')
  92. ->shouldReturn(12)
  93. ;
  94. }
  95. function it_renames_file($bucket)
  96. {
  97. $this->resources[] = $writable = fopen('php://memory', 'rw');
  98. $this->resources[] = $readable = fopen('php://memory', 'rw');
  99. fwrite($readable, 'some content');
  100. fseek($readable, 0);
  101. $bucket->openUploadStream('otherFilename', ['metadata' => ['some' => 'metadata']])->willReturn($writable);
  102. $bucket->downloadToStreamByName('filename', $writable)->shouldBeCalled();
  103. $bucket
  104. ->findOne(['filename' => 'filename'], ['projection' => ['_id' => 1]])
  105. ->willReturn($toDelete = new BSONDocument(['_id' => 1234]))
  106. ;
  107. $bucket->delete(1234)->shouldBeCalled();
  108. $this->setMetadata('filename', ['some' => 'metadata']);
  109. $this->rename('filename', 'otherFilename')->shouldReturn(true);
  110. }
  111. function it_fetches_keys($bucket)
  112. {
  113. $bucket
  114. ->find([], ['projection' => ['filename' => 1]])
  115. ->willReturn([new BSONDocument(['filename' => 'filename']), new BSONDocument(['filename' => 'otherFilename'])])
  116. ;
  117. $this->keys()->shouldReturn(['filename', 'otherFilename']);
  118. }
  119. function it_fetches_mtime($bucket)
  120. {
  121. $bucket
  122. ->findOne(['filename' => 'filename'], ['projection' => ['uploadDate' => 1]])
  123. ->willReturn(new BSONDocument(['uploadDate' => new UTCDateTime(12345000)]))
  124. ;
  125. $this->mtime('filename')->shouldReturn(12345);
  126. }
  127. function it_calculates_checksum($bucket)
  128. {
  129. $bucket
  130. ->findOne(['filename' => 'filename'], ['projection' => ['md5' => 1]])
  131. ->willReturn(new BSONDocument(['md5' => 'md5123']))
  132. ;
  133. $this->checksum('filename')->shouldReturn('md5123');
  134. }
  135. }