ImageUploaderTest.php 4.96 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Catalog\Test\Unit\Model;

class ImageUploaderTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Catalog\Model\ImageUploader
     */
    private $imageUploader;

    /**
     * Core file storage database
     *
     * @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
     */
    private $coreFileStorageDatabaseMock;

    /**
     * Media directory object (writable).
     *
     * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
     */
    private $mediaDirectoryMock;

    /**
     * Media directory object (writable).
     *
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $mediaWriteDirectoryMock;

    /**
     * Uploader factory
     *
     * @var \Magento\MediaStorage\Model\File\UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
     */
    private $uploaderFactoryMock;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $storeManagerMock;

    /**
     * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $loggerMock;

    /**
     * Base tmp path
     *
     * @var string
     */
    private $baseTmpPath;

    /**
     * Base path
     *
     * @var string
     */
    private $basePath;

    /**
     * Allowed extensions
     *
     * @var array
     */
    private $allowedExtensions;

    /**
     * Allowed mime types
     *
     * @var array
     */
    private $allowedMimeTypes;

    protected function setUp()
    {
        $this->coreFileStorageDatabaseMock = $this->createMock(
            \Magento\MediaStorage\Helper\File\Storage\Database::class
        );
        $this->mediaDirectoryMock = $this->createMock(
            \Magento\Framework\Filesystem::class
        );
        $this->mediaWriteDirectoryMock = $this->createMock(
            \Magento\Framework\Filesystem\Directory\WriteInterface::class
        );
        $this->mediaDirectoryMock->expects($this->any())->method('getDirectoryWrite')->willReturn(
            $this->mediaWriteDirectoryMock
        );
        $this->uploaderFactoryMock = $this->createMock(
            \Magento\MediaStorage\Model\File\UploaderFactory::class
        );
        $this->storeManagerMock = $this->createMock(
            \Magento\Store\Model\StoreManagerInterface::class
        );
        $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
        $this->baseTmpPath = 'base/tmp/';
        $this->basePath =  'base/real/';
        $this->allowedExtensions = ['.jpg'];
        $this->allowedMimeTypes = ['image/jpg', 'image/jpeg', 'image/gif', 'image/png'];

        $this->imageUploader =
            new \Magento\Catalog\Model\ImageUploader(
                $this->coreFileStorageDatabaseMock,
                $this->mediaDirectoryMock,
                $this->uploaderFactoryMock,
                $this->storeManagerMock,
                $this->loggerMock,
                $this->baseTmpPath,
                $this->basePath,
                $this->allowedExtensions,
                $this->allowedMimeTypes
            );
    }

    public function testSaveFileToTmpDir()
    {
        $fileId = 'file.jpg';
        $allowedMimeTypes = [
            'image/jpg',
            'image/jpeg',
            'image/gif',
            'image/png',
        ];
        /** @var \Magento\MediaStorage\Model\File\Uploader|\PHPUnit_Framework_MockObject_MockObject $uploader */
        $uploader = $this->createMock(\Magento\MediaStorage\Model\File\Uploader::class);
        $this->uploaderFactoryMock->expects($this->once())->method('create')->willReturn($uploader);
        $uploader->expects($this->once())->method('setAllowedExtensions')->with($this->allowedExtensions);
        $uploader->expects($this->once())->method('setAllowRenameFiles')->with(true);
        $this->mediaWriteDirectoryMock->expects($this->once())->method('getAbsolutePath')->with($this->baseTmpPath)
            ->willReturn($this->basePath);
        $uploader->expects($this->once())->method('save')->with($this->basePath)
            ->willReturn(['tmp_name' => $this->baseTmpPath, 'file' => $fileId, 'path' => $this->basePath]);
        $uploader->expects($this->atLeastOnce())->method('checkMimeType')->with($allowedMimeTypes)->willReturn(true);
        $storeMock = $this->createPartialMock(
            \Magento\Store\Model\Store::class,
            ['getBaseUrl']
        );
        $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
        $storeMock->expects($this->once())->method('getBaseUrl');
        $this->coreFileStorageDatabaseMock->expects($this->once())->method('saveFile');

        $result = $this->imageUploader->saveFileToTmpDir($fileId);

        $this->assertArrayNotHasKey('path', $result);
    }
}