DownloadTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace yiiunit\extensions\mongodb\file;
  3. use yiiunit\extensions\mongodb\TestCase;
  4. /**
  5. * @group file
  6. */
  7. class DownloadTest extends TestCase
  8. {
  9. protected function tearDown()
  10. {
  11. $this->dropFileCollection('fs');
  12. parent::tearDown();
  13. }
  14. // Tests :
  15. public function testToStream()
  16. {
  17. $collection = $this->getConnection()->getFileCollection();
  18. $upload = $collection->createUpload();
  19. $document = $upload->addContent('test content')->complete();
  20. $download = $collection->createDownload($document);
  21. $stream = fopen('php://temp', 'w+');
  22. $download->toStream($stream);
  23. rewind($stream);
  24. $this->assertEquals('test content', stream_get_contents($stream));
  25. }
  26. public function testToFile()
  27. {
  28. $collection = $this->getConnection()->getFileCollection();
  29. $upload = $collection->createUpload();
  30. $document = $upload->addContent('test content')->complete();
  31. $download = $collection->createDownload($document);
  32. $fileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . 'download.txt';
  33. $download->toFile($fileName);
  34. $this->assertFileExists($fileName);
  35. $this->assertEquals('test content', file_get_contents($fileName));
  36. }
  37. public function testSubstr()
  38. {
  39. $collection = $this->getConnection()->getFileCollection();
  40. $upload = $collection->createUpload();
  41. $upload->chunkSize = 10;
  42. $document = $upload->addContent('0123456789')
  43. ->addContent('0123456789')
  44. ->addContent('0123456789')
  45. ->complete();
  46. $download = $collection->createDownload($document);
  47. $this->assertEquals('2345', $download->substr(12, 4), 'Unable to read part of chunk');
  48. $this->assertEquals('89012345678901', $download->substr(8, 14), 'Unable to read from different chunks');
  49. $this->assertEquals('56789', $download->substr(25, 10), 'Unable handle read out of length');
  50. $this->assertEquals(false, $download->substr(100, 10), 'No fail on read out of file');
  51. $this->assertEquals('2345678', $download->substr(-8, 7), 'Unable to use negative start');
  52. $this->assertEquals('234567', $download->substr(22, -2), 'Unable to use negative length');
  53. }
  54. }