|
| 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\Validator; |
| 9 | + |
| 10 | +use Magento\Framework\File\Mime; |
| 11 | +use Magento\Framework\Filesystem\Driver\File; |
| 12 | +use Magento\Framework\Image as FrameworkImage; |
| 13 | +use Magento\Framework\Image\Factory; |
| 14 | +use Magento\MediaStorage\Model\File\Validator\Image; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** Unit tests for \Magento\MediaStorage\Model\File\Validator\Image class */ |
| 19 | +class ImageTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Mime|MockObject |
| 23 | + */ |
| 24 | + private $fileMimeMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Factory|MockObject |
| 28 | + */ |
| 29 | + private $imageFactoryMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var FrameworkImage|MockObject |
| 33 | + */ |
| 34 | + private $imageMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var File|MockObject |
| 38 | + */ |
| 39 | + private $fileMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Image |
| 43 | + */ |
| 44 | + private $image; |
| 45 | + |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + $this->fileMimeMock = $this->createMock(Mime::class); |
| 49 | + $this->imageFactoryMock = $this->createMock(Factory::class); |
| 50 | + $this->fileMock = $this->createMock(File::class); |
| 51 | + $this->imageMock = $this->createMock(FrameworkImage::class); |
| 52 | + |
| 53 | + $this->image = new Image( |
| 54 | + $this->fileMimeMock, |
| 55 | + $this->imageFactoryMock, |
| 56 | + $this->fileMock |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider dataProviderForIsValid |
| 62 | + */ |
| 63 | + public function testIsValid($filePath, $mimeType, $result): void |
| 64 | + { |
| 65 | + $this->fileMimeMock->expects($this->once()) |
| 66 | + ->method('getMimeType') |
| 67 | + ->with($filePath) |
| 68 | + ->willReturn($mimeType); |
| 69 | + if ($result) { |
| 70 | + $this->imageMock->expects($this->once()) |
| 71 | + ->method('open') |
| 72 | + ->willReturn(null); |
| 73 | + $this->imageFactoryMock->expects($this->once()) |
| 74 | + ->method('create') |
| 75 | + ->willReturn($this->imageMock); |
| 76 | + } |
| 77 | + $this->assertEquals($result, $this->image->isValid($filePath)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array[] |
| 82 | + */ |
| 83 | + public function dataProviderForIsValid() |
| 84 | + { |
| 85 | + return [ |
| 86 | + 'x-icon' => [dirname(__FILE__) . '/_files/favicon-x-icon.ico', 'image/x-icon', true], |
| 87 | + 'vnd-microsoft-icon' => [dirname(__FILE__) . '/_files/favicon-vnd-microsoft.ico', 'image/vnd.microsoft.icon', true], |
| 88 | + 'not-valid-ico' => [dirname(__FILE__) . '/_files/not-valid-file.ico', 'text/plain', false] |
| 89 | + ]; |
| 90 | + } |
| 91 | +} |
0 commit comments