Skip to content

Commit

Permalink
Test(web-twig): Introduce HTML format method for better snapshots
Browse files Browse the repository at this point in the history
  * formatting is done via `tidy` PHP extension
  • Loading branch information
literat committed Mar 30, 2023
1 parent c8bc461 commit ede292e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/web-twig-demo/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ RUN set -eux; \
zip \
apcu \
opcache \
tidy \
;

###> recipes ###
Expand Down
26 changes: 23 additions & 3 deletions packages/web-twig/tests/ComponentSnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function test(string $template): void
{
$html = $this->twig->render($template);

$html = $this->formatHtml($html);

$this->assertMatchesHtmlSnapshot($html);
}

Expand All @@ -45,11 +47,29 @@ public function snapshotComponentsDataProvider(): array
$dataToProvide = [];

foreach ($scannedDirectory as $fileName) {
if (!is_dir(self::SNAPSHOT_SOURCES . '/' . $fileName)) {
$dataToProvide[(string) $fileName] = [(string) $fileName];
}
if (! is_dir(self::SNAPSHOT_SOURCES . '/' . $fileName)) {
$dataToProvide[(string) $fileName] = [(string) $fileName];
}
}

return $dataToProvide;
}

private function formatHtml(string $html): string
{
// Specify configuration
$config = [
'indent' => true,
'output-xhtml' => true,
'wrap' => 120,
];

// Tidy
$tidy = new \tidy();
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();

// Output
return $tidy->root()->value;
}
}

0 comments on commit ede292e

Please sign in to comment.