File: DefaultFinderTest.php

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

declare(strict_types=1);

namespace WebThumbnailer\Finder;

use WebThumbnailer\TestCase;

class DefaultFinderTest extends TestCase
{
    /**
     * PHP builtin local server URL.
     */
    protected const LOCAL_SERVER = 'http://localhost:8081/';

    /**
     * Test the default finder with URL which match an image (.png).
     */
    public function testDefaultFinderImage(): void
    {
        $url = 'http://domains.tld/image.png';
        $finder = new DefaultFinder('', $url, [], []);
        $this->assertEquals($url, $finder->find());

        $url = 'http://domains.tld/image.JPG';
        $finder = new DefaultFinder('', $url, [], []);
        $this->assertEquals($url, $finder->find());

        $url = 'http://domains.tld/image.svg';
        $finder = new DefaultFinder('', $url, [], []);
        $this->assertEquals($url, $finder->find());
    }

    /**
     * Test the default finder with URL which does NOT match an image.
     */
    public function testDefaultFinderNotImage(): void
    {
        $file = __DIR__ . '/../workdir/nope';
        touch($file);
        $finder = new DefaultFinder('', $file, [], []);
        $this->assertFalse($finder->find());
        @unlink($file);
    }

    /**
     * Test the default finder downloading an image without extension.
     */
    public function testDefautFinderRemoteImage(): void
    {
        $file = __DIR__ . '/../workdir/image';
        // From http://php.net/imagecreatefromstring
        $data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
            . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
            . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
            . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
        file_put_contents($file, base64_decode($data));
        $finder = new DefaultFinder('', $file, null, null);
        $this->assertEquals($file, $finder->find());
        @unlink($file);
    }

    /**
     * Test the default finder trying to find an open graph link.
     */
    public function testDefaultFinderOpenGraph(): void
    {
        $this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
        $url = __DIR__ . '/../resources/default/le-monde.html';
        $expected = 'https://img.lemde.fr/2016/10/21/107/0/1132/566/1440/720/60/0/fe3b107_3522-d2olbw.y93o25u3di.jpg';
        $finder = new DefaultFinder('', $url, null, null);
        $this->assertEquals($expected, $finder->find());
    }

    /**
     * Test the default finder trying to find an open graph link.
     */
    public function testDefaultFinderOpenGraphRemote(): void
    {
        $this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
        $url = self::LOCAL_SERVER . 'default/le-monde.html';
        $expected = 'https://img.lemde.fr/2016/10/21/107/0/1132/566/1440/720/60/0/fe3b107_3522-d2olbw.y93o25u3di.jpg';
        $finder = new DefaultFinder('', $url, null, null);
        $this->assertEquals($expected, $finder->find());
    }

    /**
     * Test the default finder trying to find an image mime-type.
     */
    public function testDefaultFinderImageMimetype(): void
    {
        $url = self::LOCAL_SERVER . 'default/image-mimetype.php';
        $expected = $url;
        $finder = new DefaultFinder('', $url, null, null);
        $this->assertEquals($expected, $finder->find());
    }

    /**
     * Test the default finder finding a non 200 status code.
     */
    public function testDefaultFinderStatusError(): void
    {
        $url = self::LOCAL_SERVER . 'default/status-ko.php';
        $finder = new DefaultFinder('', $url, null, null);
        $this->assertFalse($finder->find());
    }

    /**
     * Test getName().
     */
    public function testGetName(): void
    {
        $finder = new DefaultFinder('', '', [], []);
        $this->assertEquals('default', $finder->getName());
    }
}