summaryrefslogtreecommitdiff
path: root/vendor/soundasleep/html2text/tests/Html2TextTest.php
blob: 5e2e522c43a3340dbc17ea2cd18ae23dd62d63b4 (plain)
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
<?php

require(__DIR__ . "/../src/Html2Text.php");

class Html2TextTest extends \PHPUnit\Framework\TestCase {

	// delete all failures before we run
	public static function setUpBeforeClass(): void {
		foreach (new DirectoryIterator(__DIR__ . '/failures') as $fileInfo) {
			if ($fileInfo->getFileName()[0] != '.') {
				unlink($fileInfo->getPathname());
			}
		}
	}

	/**
	 * @dataProvider providerFiles
	 */
	public function testFile(string $test): void {
		$this->doTestWithResults($test, $test, []);
	}

	/** @param bool | array<string, bool | string> $options */
	function doTestWithResults(string $test, string $result, $options = []): void {
		$html = __DIR__ . "/html/$test.html";
		$txt = __DIR__ . "/txt/$result.txt";
		$this->assertTrue(file_exists($html), "File '{$html}' does not exist");
		$this->assertTrue(file_exists($txt), "File '{$txt}' does not exist");
		$input = file_get_contents($html);
		$expected = \Soundasleep\Html2Text::fixNewlines(file_get_contents($txt));

		$output = \Soundasleep\Html2Text::convert($input, $options);

		if ($output != $expected) {
			file_put_contents(__DIR__ . "/failures/$result.output", $output);
		}
		$this->assertEquals($expected, $output, "{$html} file failed to convert to {$txt}");
	}

	/** @return array<array<string>> */
	public function providerFiles(): array {
		return [
			['basic'],
			['anchors'],
			['more-anchors'],
			['test3'],
			['test4'],
			['table'],
			['nbsp'],
			['lists'],
			['pre'],
			['newlines'],
			['nested-divs'],
			['blockquotes'],
			['full_email'],
			['images'],
			['non-breaking-spaces'],
			['utf8-example'],
			['msoffice'],
			['dom-processing'],
			['empty'],
			['huge-msoffice'],
			['zero-width-non-joiners'],
		];
	}

	public function testInvalidXML(): void {
		$this->expectWarning();
		$this->doTestWithResults("invalid", "invalid", ['ignore_errors' => false]);
	}

	public function testInvalidXMLIgnore(): void {
		$this->doTestWithResults("invalid", "invalid", ['ignore_errors' => true]);
	}

	public function testInvalidXMLIgnoreOldSyntax(): void {
		// for BC, allow old #convert(text, bool) syntax
		$this->doTestWithResults("invalid", "invalid", true);
	}

	public function testInvalidOption(): void {
		$this->expectException(InvalidArgumentException::class);
		$this->doTestWithResults("basic", "basic", ['invalid_option' => true]);
	}

	public function testBasicDropLinks(): void {
		$this->doTestWithResults("basic", "basic.no-links", ['drop_links' => true]);
	}

	public function testAnchorsDropLinks(): void {
		$this->doTestWithResults("anchors", "anchors.no-links", ['drop_links' => true]);
	}

	public function testWindows1252(): void {
		$this->doTestWithResults("windows-1252-example", "windows-1252-example", ['char_set' => 'windows-1252']);
	}
}