File: ImageUtilsTest.php

package info (click to toggle)
php-arthurhoaro-web-thumbnailer 2.1.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: php: 3,365; makefile: 19; sh: 9
file content (153 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace WebThumbnailer\Utils;

use WebThumbnailer\Exception\ImageConvertException;
use WebThumbnailer\Exception\NotAnImageException;
use WebThumbnailer\TestCase;

/**
 * Test image utils methods, especially thumbnail rendering.
 */
class ImageUtilsTest extends TestCase
{
    /**
     * @var string Image as string.
     */
    public static $imageSource;

    /**
     * @var string Working directory path.
     */
    public static $WORKDIR =
        __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'workdir' . DIRECTORY_SEPARATOR;

    /**
     * Regenerate the image before every tests.
     */
    public function setUp(): void
    {
        // From http://php.net/imagecreatefromstring
        $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
            . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
            . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
            . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
        self::$imageSource = base64_decode($data);
    }

    /**
     * Remove the test image in workdir after every test.
     * Ignore errors.
     */
    public function tearDown(): void
    {
        @unlink(self::$WORKDIR . 'file1.png');
    }

    /**
     * Test generating valid thumbnails.
     */
    public function testGenerateThumbnail(): void
    {
        $path = self::$WORKDIR . 'file1.png';
        // Original size.
        ImageUtils::generateThumbnail(self::$imageSource, $path, 28, 18);
        $im = imagecreatefromjpeg($path);
        $this->assertEquals(28, imagesx($im));
        $this->assertEquals(18, imagesy($im));
        unlink($path);

        // Reduce size.
        ImageUtils::generateThumbnail(self::$imageSource, $path, 14, 9);
        $im = imagecreatefromjpeg($path);
        $this->assertEquals(14, imagesx($im));
        $this->assertEquals(9, imagesy($im));
        unlink($path);

        // Bigger size: must be changed to original size.
        ImageUtils::generateThumbnail(self::$imageSource, $path, 56, 36);
        $im = imagecreatefromjpeg($path);
        $this->assertEquals(28, imagesx($im));
        $this->assertEquals(18, imagesy($im));
        unlink($path);

        // Only specify width.
        ImageUtils::generateThumbnail(self::$imageSource, $path, 14, 0);
        $im = imagecreatefromjpeg($path);
        $this->assertEquals(14, imagesx($im));
        $this->assertEquals(9, imagesy($im));
        unlink($path);

        // Only specify heigth.
        ImageUtils::generateThumbnail(self::$imageSource, $path, 0, 9);
        $im = imagecreatefromjpeg($path);
        $this->assertEquals(14, imagesx($im));
        $this->assertEquals(9, imagesy($im));
        unlink($path);
    }

    /**
     * Generate a thumbnail into a non existent folder => Exception.
     */
    public function testGenerateThumbnailUnwritable(): void
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Target file is not writable.');

        $path = self::$WORKDIR . 'nope' . DIRECTORY_SEPARATOR . 'file';
        @ImageUtils::generateThumbnail(self::$imageSource, $path, 28, 18);
    }

    /**
     * Generate a thumbnail from a string which is not an image => NotAnImageException.
     */
    public function testGenerateThumbnailNotAnImage(): void
    {
        $this->expectException(NotAnImageException::class);

        $path = self::$WORKDIR . 'file1.png';
        ImageUtils::generateThumbnail('virus.exe', $path, 28, 18);
    }

    /**
     * Generate a thumbnail with bad sizes => Exception.
     */
    public function testGenerateThumbnailBadSize(): void
    {
        $this->expectException(ImageConvertException::class);
        $this->expectExceptionMessage('Height and width must be zero or positive');

        $path = self::$WORKDIR . 'file1.png';
        @ImageUtils::generateThumbnail(self::$imageSource, $path, -1, -1);
    }

    /**
     * Generate a thumbnail with bad sizes (Double 0) => Exception.
     */
    public function testGenerateThumbnailBadSizeDoubleZero(): void
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('At least maxwidth or maxheight needs to be defined.');

        $path = self::$WORKDIR . 'file1.png';
        @ImageUtils::generateThumbnail(self::$imageSource, $path, 0, 0);
    }

    /**
     * Check that a string is an image.
     */
    public function testIsStringImageTrue(): void
    {
        $this->assertTrue(ImageUtils::isImageString(self::$imageSource));
    }

    /**
     * Check that a string is not an image.
     */
    public function testIsStringImageFalse(): void
    {
        $this->assertFalse(ImageUtils::isImageString('string'));
    }
}