FileTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Helper;
  7. class FileTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Downloadable\Helper\File
  11. */
  12. private $file;
  13. /**
  14. * Core file storage database
  15. *
  16. * @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $coreFileStorageDatabase;
  19. /**
  20. * Filesystem object.
  21. *
  22. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $filesystem;
  25. /**
  26. * Media Directory object (writable).
  27. *
  28. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $mediaDirectory;
  31. /**
  32. * @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $appContext;
  35. protected function setUp()
  36. {
  37. $this->mediaDirectory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  38. ->getMockForAbstractClass();
  39. $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->filesystem->expects($this->any())
  43. ->method('getDirectoryWrite')
  44. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  45. ->willReturn($this->mediaDirectory);
  46. $this->coreFileStorageDatabase =
  47. $this->getMockBuilder(\Magento\MediaStorage\Helper\File\Storage\Database::class)
  48. ->setMethods(['create'])
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->appContext = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(
  54. [
  55. 'getModuleManager',
  56. 'getLogger',
  57. 'getRequest',
  58. 'getUrlBuilder',
  59. 'getHttpHeader',
  60. 'getEventManager',
  61. 'getRemoteAddress',
  62. 'getCacheConfig',
  63. 'getUrlEncoder',
  64. 'getUrlDecoder',
  65. 'getScopeConfig'
  66. ]
  67. )
  68. ->getMock();
  69. $this->file = new \Magento\Downloadable\Helper\File(
  70. $this->appContext,
  71. $this->coreFileStorageDatabase,
  72. $this->filesystem
  73. );
  74. }
  75. public function testUploadFromTmp()
  76. {
  77. $uploaderMock = $this->getMockBuilder(\Magento\MediaStorage\Model\File\Uploader::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $uploaderMock->expects($this->once())->method('setAllowRenameFiles');
  81. $uploaderMock->expects($this->once())->method('setFilesDispersion');
  82. $this->mediaDirectory->expects($this->once())->method('getAbsolutePath')->willReturn('absPath');
  83. $uploaderMock->expects($this->once())->method('save')->with('absPath')
  84. ->willReturn(['file' => 'file.jpg', 'path' => 'absPath']);
  85. $result = $this->file->uploadFromTmp('tmpPath', $uploaderMock);
  86. $this->assertArrayNotHasKey('path', $result);
  87. }
  88. }