Skip to content

Commit 7aeb68d

Browse files
authored
Merge pull request #1370 from phpDocumentor/task/remove-gajus-dindent
[TASK] Remove dependency on gajus/dindent
2 parents 7881615 + e867a11 commit 7aeb68d

3 files changed

Lines changed: 42 additions & 75 deletions

File tree

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"doctrine/coding-standard": "^13.0",
6363
"fakerphp/faker": "^1.24",
6464
"fig/log-test": "^1.0",
65-
"gajus/dindent": "^2.0.1",
6665
"jangregor/phpstan-prophecy": "^2.3",
6766
"league/csv": "^9.0",
6867
"league/flysystem-memory": "^1.0 || ^3.29",

composer.lock

Lines changed: 1 addition & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Functional/FunctionalTest.php

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
namespace phpDocumentor\Guides\Functional;
1515

16+
use DOMDocument;
1617
use Exception;
17-
use Gajus\Dindent\Indenter;
1818
use League\Flysystem\Filesystem;
1919
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
2020
use Monolog\Handler\TestHandler;
@@ -49,7 +49,9 @@
4949
use function implode;
5050
use function in_array;
5151
use function is_string;
52-
use function rtrim;
52+
use function libxml_clear_errors;
53+
use function libxml_use_internal_errors;
54+
use function preg_replace;
5355
use function setlocale;
5456
use function sprintf;
5557
use function str_replace;
@@ -65,8 +67,6 @@ class_alias('League\Flysystem\Memory\MemoryAdapter', 'League\Flysystem\InMemory\
6567

6668
final class FunctionalTest extends ApplicationTestCase
6769
{
68-
private const SKIP_INDENTER_FILES = ['code-block-diff'];
69-
7070
private const IGNORED_WARNINGS = ['Document has no title'];
7171

7272
protected function setUp(): void
@@ -81,7 +81,6 @@ public function testFunctional(
8181
string $format,
8282
string $rst,
8383
string $expected,
84-
bool $useIndenter = true,
8584
array $expectedLogs = [],
8685
): void {
8786
$expectedLines = explode("\n", $expected);
@@ -146,19 +145,40 @@ public function testFunctional(
146145
);
147146
}
148147

149-
if ($format === 'html' && $useIndenter) {
150-
$indenter = new Indenter();
151-
$rendered = $indenter->indent($rendered);
152-
}
153-
154148
if (isset($expectedExceptionMessage)) {
155149
return;
156150
}
157151

158-
self::assertSame(
159-
$this->trimTrailingWhitespace($expected),
160-
$this->trimTrailingWhitespace($rendered),
161-
);
152+
if ($format === 'html') {
153+
$rendered = $this->removeRedundantWhitespaceFromHtml($rendered);
154+
$expected = $this->removeRedundantWhitespaceFromHtml($expected);
155+
156+
$previousUseInternalErrors = libxml_use_internal_errors(true);
157+
try {
158+
$expectedDom = new DOMDocument();
159+
$expectedDom->loadHTML($expected);
160+
$expectedDom->preserveWhiteSpace = false;
161+
162+
$actualDom = new DOMDocument();
163+
$actualDom->loadHTML($rendered);
164+
$actualDom->preserveWhiteSpace = false;
165+
166+
$expectedHtml = $expectedDom->saveHTML();
167+
$actualHtml = $actualDom->saveHTML();
168+
169+
self::assertIsString($expectedHtml);
170+
self::assertIsString($actualHtml);
171+
172+
self::assertXmlStringEqualsXmlString($expectedHtml, $actualHtml);
173+
} catch (Throwable) {
174+
self::assertSame(trim($expected), trim($rendered));
175+
} finally {
176+
libxml_clear_errors();
177+
libxml_use_internal_errors($previousUseInternalErrors);
178+
}
179+
} else {
180+
self::assertSame(trim($expected), trim($rendered));
181+
}
162182

163183
$logHandler = $this->getContainer()->get(TestHandler::class);
164184
assert($logHandler instanceof TestHandler);
@@ -227,8 +247,6 @@ public static function getFunctionalTests(): array
227247

228248
$expected = $file->getContents();
229249

230-
$useIndenter = !in_array($basename, self::SKIP_INDENTER_FILES, true);
231-
232250
$logFile = $file->getPath() . '/' . $file->getFilenameWithoutExtension() . '.log';
233251
$logs = [];
234252
if (file_exists($logFile)) {
@@ -237,21 +255,20 @@ public static function getFunctionalTests(): array
237255
$logs = array_map(trim(...), $logFileContent);
238256
}
239257

240-
$tests[$basename . '_' . $format] = [$basename, $format, $rst, trim($expected), $useIndenter, $logs];
258+
$tests[$basename . '_' . $format] = [$basename, $format, $rst, trim($expected), $logs];
241259
}
242260
}
243261

244262
return $tests;
245263
}
246264

247-
private function trimTrailingWhitespace(string $string): string
265+
private function removeRedundantWhitespaceFromHtml(string $html): string
248266
{
249-
$lines = explode("\n", $string);
250-
251-
$lines = array_map(static function (string $line): string {
252-
return rtrim($line);
253-
}, $lines);
267+
$html = implode("\n", array_map('trim', explode("\n", $html)));
268+
$html = preg_replace('#\s+#', ' ', $html) ?? $html;
269+
$html = preg_replace('#\s<#', '<', $html) ?? $html;
270+
$html = preg_replace('#>\s#', '>', $html) ?? $html;
254271

255-
return trim(implode("\n", $lines));
272+
return $html;
256273
}
257274
}

0 commit comments

Comments
 (0)