ImageTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 48
c 1
b 0
f 0
dl 0
loc 105
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetFile() 0 12 3
A setUp() 0 3 1
A tearDown() 0 4 1
A testGetPath() 0 9 1
A testDimension() 0 8 1
A hp$1 ➔ getPath() 0 3 1
createFileToUpload() 0 12 ?
A hp$1 ➔ createFileToUpload() 0 12 1
A providerSetFile() 0 10 1
A hp$0 ➔ createImage() 0 4 1
A hp$1 ➔ getBasePath() 0 3 1
createImage() 0 4 ?
A hp$1 ➔ createImageForUpload() 0 13 1
createImageForUpload() 0 13 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Model\Traits;
6
7
use Ecodev\Felix\Model\Traits\Image;
8
use EcodevTests\Felix\Traits\TestWithContainer;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\UploadedFileInterface;
11
12
final class ImageTest extends TestCase
13
{
14
    private const TEMP = '/tmp/felix';
15
16
    use TestWithContainer {
17
        tearDown as tearDownWithContainer;
18
    }
19
20
    protected function setUp(): void
21
    {
22
        @mkdir(self::TEMP, recursive: true);
23
    }
24
25
    protected function tearDown(): void
26
    {
27
        $this->tearDownWithContainer();
28
        shell_exec('rm -rf ' . self::TEMP);
29
    }
30
31
    public function testGetPath(): void
32
    {
33
        $image = $this->createImage();
34
        $image->setFilename('photo.jpg');
35
36
        self::assertSame('photo.jpg', $image->getFilename());
37
        $appPath = realpath('.');
38
        $expected = $appPath . '/data/images/photo.jpg';
39
        self::assertSame($expected, $image->getPath());
40
    }
41
42
    public function testDimension(): void
43
    {
44
        $image = $this->createImage();
45
        $image->setWidth(123);
46
        $image->setHeight(456);
47
48
        self::assertSame(123, $image->getWidth());
49
        self::assertSame(456, $image->getHeight());
50
    }
51
52
    /**
53
     * @dataProvider providerSetFile
54
     */
55
    public function testSetFile(string $filename, int $width, int $height, bool $isSvg = false): void
56
    {
57
        $this->createDefaultFelixContainer();
58
59
        $file = $this->createFileToUpload($filename);
60
        $image = $this->createImageForUpload();
61
62
        $image->setFile($file);
63
        self::assertSame($width, $image->getWidth());
64
        self::assertSame($height, $image->getHeight());
65
        self::assertSame($isSvg ? 'image/svg+xml' : 'image/webp', $image->getMime());
66
        self::assertStringEndsWith($isSvg ? '.svg' : '.webp', $image->getPath());
67
    }
68
69
    public static function providerSetFile(): iterable
70
    {
71
        yield 'jpg is converted to webp' => ['image.jpg', 400, 400];
72
        yield 'png is converted to webp' => ['image.png', 400, 300];
73
        yield 'avif is converted to webp' => ['image.avif', 400, 299];
74
        yield 'heif is converted to webp' => ['image.heif', 1440, 960];
75
        yield 'svg is untouched' => ['logo.svg', 445, 488, true];
76
        yield 'webp is untouched' => ['image.webp', 400, 400];
77
        yield 'huge jpg is resized to webp' => ['huge.jpg', 3500, 19];
78
        yield 'huge webp is resized' => ['huge.webp', 3500, 19];
79
    }
80
81
    private function createImage(): \Ecodev\Felix\Model\Image
82
    {
83
        return new class() implements \Ecodev\Felix\Model\Image {
84
            use Image;
85
        };
86
    }
87
88
    private function createImageForUpload(): \Ecodev\Felix\Model\Image
89
    {
90
        return new class() implements \Ecodev\Felix\Model\Image {
91
            use Image;
92
93
            public function getBasePath(): string
94
            {
95
                return '/tmp/felix/';
96
            }
97
98
            public function getPath(): string
99
            {
100
                return $this->getBasePath() . $this->getFilename();
101
            }
102
        };
103
    }
104
105
    private function createFileToUpload(string $filename): UploadedFileInterface
106
    {
107
        $file = $this->createMock(UploadedFileInterface::class);
108
        $file->expects(self::once())
109
            ->method('getClientFilename')
110
            ->willReturn("useless-prefix-$filename");
111
112
        $file->expects(self::once())
113
            ->method('moveTo')
114
            ->willReturnCallback(fn ($dest) => copy("tests/data/images/$filename", $dest));
115
116
        return $file;
117
    }
118
}
119