Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Split integration tests in content and full test #687

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
<div class="col-lg-9">
{% block breadcrumb %}
{% endblock %}
<!-- content start -->
{% block body %}
{% endblock %}
<!-- content end -->
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
{%~ endblock %}
</head>
<body>
<!-- content start -->
{%~ block body %}
{%~ endblock %}
<!-- content end -->
</body>
</html>
74 changes: 72 additions & 2 deletions tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@
use function file_exists;
use function file_get_contents;
use function implode;
use function PHPUnit\Framework\assertIsString;
use function setlocale;
use function str_ends_with;
use function str_replace;
use function strlen;
use function strpos;
use function substr;
use function system;
use function trim;

use const LC_ALL;

class IntegrationTest extends ApplicationTestCase
{
private const CONTENT_START = '<!-- content start -->';
private const CONTENT_END = '<!-- content end -->';

protected function setUp(): void
{
setlocale(LC_ALL, 'en_US.utf8');
Expand All @@ -44,6 +51,23 @@ public function testHtmlIntegration(
string $outputPath,
array $compareFiles,
): void {
$this->compareHtmlIntegration($outputPath, $inputPath, $expectedPath, $compareFiles, true);
}

/** @param list<string> $compareFiles */
#[DataProvider('getTestsForDirectoryTestsFull')]
public function testHtmlIntegrationFullFile(
string $inputPath,
string $expectedPath,
string $outputPath,
array $compareFiles,
): void {
$this->compareHtmlIntegration($outputPath, $inputPath, $expectedPath, $compareFiles, false);
}

/** @param list<string> $compareFiles */
private function compareHtmlIntegration(string $outputPath, string $inputPath, string $expectedPath, array $compareFiles, bool $htmlOnlyBetweenMarkers): void
{
system('rm -rf ' . escapeshellarg($outputPath));
self::assertDirectoryExists($inputPath);
self::assertDirectoryExists($expectedPath);
Expand Down Expand Up @@ -91,6 +115,14 @@ public function testHtmlIntegration(
$outputFile = str_replace($expectedPath, $outputPath, $compareFile);
if (str_ends_with($compareFile, '.log')) {
self::assertFileContainsLines($compareFile, $outputFile);
} elseif ($htmlOnlyBetweenMarkers && str_ends_with($compareFile, '.html')) {
self::assertFileEqualsTrimmedBetweenMarkers(
$compareFile,
$outputFile,
'Expected file path: ' . $compareFile,
self::CONTENT_START,
self::CONTENT_END,
);
} else {
self::assertFileEqualsTrimmed($compareFile, $outputFile, 'Expected file path: ' . $compareFile);
}
Expand All @@ -106,6 +138,37 @@ public function testHtmlIntegration(
self::assertFalse($skip, 'Test passes while marked as SKIP.');
}

/**
* Asserts that each line of the expected file is contained in actual
*
* @throws ExpectationFailedException
*/
private static function assertFileEqualsTrimmedBetweenMarkers(string $expected, string $actual, string $message, string $startMarker, string $endMarker): void
{
self::assertFileExists($expected);
self::assertFileExists($actual);

$expectedContent = self::extractContentBetweenMarkers($expected, $startMarker, $endMarker);
$actualContent = self::extractContentBetweenMarkers($actual, $startMarker, $endMarker);

self::assertEquals(self::getTrimmedContent($expectedContent), self::getTrimmedContent($actualContent), $message);
}

/**
* Extract content between specified markers in a file.
*/
private static function extractContentBetweenMarkers(string $filePath, string $startMarker, string $endMarker): string
{
$fileContent = file_get_contents($filePath);
self::assertIsString($fileContent);
$startPos = strpos($fileContent, $startMarker);
self::assertIsInt($startPos, 'Start marker not found in file: ' . $filePath);
$endPos = strpos($fileContent, $endMarker, $startPos + strlen($startMarker));
self::assertIsInt($endPos, 'End marker not found in file: ' . $filePath);

return substr($fileContent, $startPos + strlen($startMarker), $endPos - $startPos - strlen($startMarker));
}

/**
* Asserts that each line of the expected file is contained in actual
*
Expand Down Expand Up @@ -145,6 +208,13 @@ public static function assertFileEqualsTrimmed(string $expected, string $actual,
public static function getTrimmedFileContent(string $file): string
{
$fileContent = file_get_contents($file);
assertIsString($fileContent);

return self::getTrimmedContent($fileContent);
}

public static function getTrimmedContent(string $fileContent): string
{
self::assertIsString($fileContent);
$contentArray = explode("\n", $fileContent);
array_walk($contentArray, static function (&$value): void {
Expand All @@ -164,9 +234,9 @@ public static function getTestsForDirectoryTest(): array
}

/** @return mixed[] */
public static function getTestsForLatex(): array
public static function getTestsForDirectoryTestsFull(): array
{
return self::getTestsForDirectory(__DIR__ . '/tests-latex');
return self::getTestsForDirectory(__DIR__ . '/tests-full');
}

/** @return mixed[] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ <h2>Additional Menu</h2>
</ol>
</nav>

<!-- content start -->


<div class="section" id="another-page">
Expand All @@ -88,6 +89,7 @@ <h1>Another Page</h1>
<p>Lorem Ipsum Dolor.</p>
</div>

<!-- content end -->
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ <h2>Additional Menu</h2>
</ol>
</nav>

<!-- content start -->


<div class="section" id="document-title">
Expand All @@ -89,6 +90,7 @@ <h1>Document Title</h1>

</div>

<!-- content end -->
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ <h2>Additional Menu</h2>
</ol>
</nav>

<!-- content start -->


<div class="section" id="some-page">
Expand All @@ -88,6 +89,7 @@ <h1>Some Page</h1>
<p>Lorem Ipsum <span class="custom">Dolor</span>.</p>
</div>

<!-- content end -->
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
</head>
<body>
<header class="">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">

<a class="navbar-brand" href="#">Navbar</a>
Expand All @@ -19,8 +20,7 @@
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">




<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a href="/anotherPage.html" class="nav-link " >
Expand All @@ -36,7 +36,8 @@
</a>
</li></ul>

</div>

</div>
</div>
</nav>
</header>
Expand All @@ -47,7 +48,7 @@
<div class="col-lg-3">

<nav class="nav flex-column">
<ul class="level-1">
<ul class="level-1">
<li><a href="/anotherPage.html"
class="nav-link ">
Another Page
Expand Down Expand Up @@ -75,6 +76,7 @@
</ol>
</nav>

<!-- content start -->


<div class="section" id="another-page">
Expand All @@ -83,6 +85,7 @@ <h1>Another Page</h1>
<p>Lorem Ipsum Dolor.</p>
</div>

<!-- content end -->
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
</head>
<body>
<header class="">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">

<a class="navbar-brand" href="#">Navbar</a>
Expand All @@ -19,8 +20,7 @@
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">




<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a href="/anotherPage.html" class="nav-link " >
Expand All @@ -36,7 +36,8 @@
</a>
</li></ul>

</div>

</div>
</div>
</nav>
</header>
Expand All @@ -47,7 +48,7 @@
<div class="col-lg-3">

<nav class="nav flex-column">
<ul class="level-1">
<ul class="level-1">
<li><a href="/anotherPage.html"
class="nav-link ">
Another Page
Expand All @@ -74,6 +75,7 @@
</ol>
</nav>

<!-- content start -->


<div class="section" id="document-title">
Expand All @@ -83,6 +85,7 @@ <h1>Document Title</h1>

</div>

<!-- content end -->
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
</head>
<body>
<header class="">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<div class="container">

<a class="navbar-brand" href="#">Navbar</a>
Expand All @@ -19,8 +20,7 @@
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">




<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a href="/anotherPage.html" class="nav-link " >
Expand All @@ -36,7 +36,8 @@
</a>
</li></ul>

</div>

</div>
</div>
</nav>
</header>
Expand All @@ -47,7 +48,7 @@
<div class="col-lg-3">

<nav class="nav flex-column">
<ul class="level-1">
<ul class="level-1">
<li><a href="/anotherPage.html"
class="nav-link ">
Another Page
Expand Down Expand Up @@ -75,6 +76,7 @@
</ol>
</nav>

<!-- content start -->


<div class="section" id="some-page">
Expand All @@ -83,6 +85,7 @@ <h1>Some Page</h1>
<p>Lorem Ipsum <span class="custom">Dolor</span>.</p>
</div>

<!-- content end -->
</div>
</div>
</div>
Expand Down
Loading