|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MediaStorage\Test\Unit\Model\File\Storage; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\Stdlib\DateTime\DateTime; |
| 13 | +use Magento\MediaStorage\Helper\File\Media; |
| 14 | +use Magento\MediaStorage\Helper\File\Storage\Database; |
| 15 | +use Magento\MediaStorage\Model\File\Storage\File; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +/** Unit tests for \Magento\MediaStorage\Model\File\Storage\File class */ |
| 20 | +class FileTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var File |
| 24 | + */ |
| 25 | + protected $_file; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Media |
| 29 | + */ |
| 30 | + protected $_loggerMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Database |
| 34 | + */ |
| 35 | + protected $_storageHelperMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var DateTime |
| 39 | + */ |
| 40 | + protected $_mediaHelperMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Magento\MediaStorage\Model\ResourceModel\File\Storage\File |
| 44 | + */ |
| 45 | + protected $_fileUtilityMock; |
| 46 | + |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + $this->_loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); |
| 50 | + $this->_storageHelperMock = $this->createMock(Database::class); |
| 51 | + $this->_mediaHelperMock = $this->createMock(Media::class); |
| 52 | + $this->_fileUtilityMock = $this->createMock(\Magento\MediaStorage\Model\ResourceModel\File\Storage\File::class); |
| 53 | + |
| 54 | + $this->_file = new File( |
| 55 | + $this->_loggerMock, |
| 56 | + $this->_storageHelperMock, |
| 57 | + $this->_mediaHelperMock, |
| 58 | + $this->_fileUtilityMock |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + protected function tearDown(): void |
| 63 | + { |
| 64 | + unset($this->_file); |
| 65 | + } |
| 66 | + |
| 67 | + public function testSaveFileWithWrongFileFormat(): void |
| 68 | + { |
| 69 | + $this->expectException(LocalizedException::class); |
| 70 | + $this->expectExceptionMessage('Wrong file info format'); |
| 71 | + $this->_file->saveFile([]); |
| 72 | + } |
| 73 | + |
| 74 | + public function testSaveFileUnsuccessfullyWithMissingDirectory() |
| 75 | + { |
| 76 | + $this->_fileUtilityMock |
| 77 | + ->expects($this->once()) |
| 78 | + ->method('saveFile') |
| 79 | + ->willThrowException(new Exception()); |
| 80 | + |
| 81 | + $this->expectException(LocalizedException::class); |
| 82 | + $this->expectExceptionMessage('Unable to save file "filename.ext" at "filename.ext"'); |
| 83 | + $this->_file->saveFile([ |
| 84 | + 'filename' => 'filename.ext', |
| 85 | + 'content' => 'content', |
| 86 | + ]); |
| 87 | + } |
| 88 | + |
| 89 | + public function testSaveFileUnsuccessfullyWithoutMissingDirectory() |
| 90 | + { |
| 91 | + $this->_fileUtilityMock |
| 92 | + ->expects($this->once()) |
| 93 | + ->method('saveFile') |
| 94 | + ->willThrowException(new Exception()); |
| 95 | + |
| 96 | + $this->expectException(LocalizedException::class); |
| 97 | + $this->expectExceptionMessage('Unable to save file "filename.ext" at "directory/filename.ext"'); |
| 98 | + $this->_file->saveFile([ |
| 99 | + 'directory' => 'directory', |
| 100 | + 'filename' => 'filename.ext', |
| 101 | + 'content' => 'content', |
| 102 | + ]); |
| 103 | + } |
| 104 | +} |
0 commit comments