Passed
Pull Request — master (#4142)
by Owen
11:05
created

HtmlImage2Test::testCannotInsertImageBadProtocol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
6
7
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
9
use PHPUnit\Framework\TestCase;
10
11
class HtmlImage2Test extends TestCase
12
{
13
    public function testCanInsertImageGoodProtocol(): void
14
    {
15
        if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
16
            self::markTestSkipped('Skipped due to setting of environment variable');
17
        }
18
        $imagePath = 'https://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png';
19
        $html = '<table>
20
                    <tr>
21
                        <td><img src="' . $imagePath . '" alt="test image voilà"></td>
22
                    </tr>
23
                </table>';
24
        $filename = HtmlHelper::createHtml($html);
25
        $spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
26
        $firstSheet = $spreadsheet->getSheet(0);
27
28
        /** @var Drawing $drawing */
29
        $drawing = $firstSheet->getDrawingCollection()[0];
30
        self::assertEquals($imagePath, $drawing->getPath());
31
        self::assertEquals('A1', $drawing->getCoordinates());
32
    }
33
34
    public function testCannotInsertImageBadProtocol(): void
35
    {
36
        $this->expectException(SpreadsheetException::class);
37
        $this->expectExceptionMessage('Invalid protocol for linked drawing');
38
        $imagePath = 'httpx://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png';
39
        $html = '<table>
40
                    <tr>
41
                        <td><img src="' . $imagePath . '" alt="test image voilà"></td>
42
                    </tr>
43
                </table>';
44
        $filename = HtmlHelper::createHtml($html);
45
        HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
46
    }
47
}
48