Passed
Push — master ( 3f7190...385f04 )
by Adrien
13:37
created

ImageTest.php$1 ➔ getBasePath()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
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 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 'svg is untouched' => ['logo.svg', 445, 488, true];
74
        yield 'webp is untouched' => ['image.webp', 400, 400];
75
        yield 'huge jpg is resized to webp' => ['huge.jpg', 3500, 19];
76
        yield 'huge webp is resized' => ['huge.webp', 3500, 19];
77
    }
78
79
    private function createImage(): \Ecodev\Felix\Model\Image
80
    {
81
        return new class() implements \Ecodev\Felix\Model\Image {
82
            use Image;
83
        };
84
    }
85
86
    private function createImageForUpload(): \Ecodev\Felix\Model\Image
87
    {
88
        return new class() implements \Ecodev\Felix\Model\Image {
89
            use Image;
90
91
            public function getBasePath(): string
92
            {
93
                return '/tmp/felix/';
94
            }
95
96
            public function getPath(): string
97
            {
98
                return $this->getBasePath() . $this->getFilename();
99
            }
100
        };
101
    }
102
103
    private function createFileToUpload(string $filename): UploadedFileInterface
104
    {
105
        $file = $this->createMock(UploadedFileInterface::class);
106
        $file->expects(self::once())
107
            ->method('getClientFilename')
108
            ->willReturn("useless-prefix-$filename");
109
110
        $file->expects(self::once())
111
            ->method('moveTo')
112
            ->willReturnCallback(fn ($dest) => copy("tests/data/images/$filename", $dest));
113
114
        return $file;
115
    }
116
}
117