Skip to content

Commit

Permalink
Configurable function tests
Browse files Browse the repository at this point in the history
Configurable function tests
  • Loading branch information
SavageTiger committed Apr 5, 2021
2 parents 6bc1c4a + ba5428c commit 6cbc63a
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 45 deletions.
24 changes: 15 additions & 9 deletions tests/Caxy/Tests/HtmlDiff/Functional/HtmlDiffFunctionalTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Caxy\Tests\HtmlDiff\Functional;

use Caxy\HtmlDiff\HtmlDiff;
use Caxy\HtmlDiff\HtmlDiffConfig;
use Caxy\Tests\AbstractTest;
use Caxy\Tests\HtmlDiff\HtmlFileIterator;

Expand All @@ -11,19 +12,24 @@ class HtmlDiffFunctionalTest extends AbstractTest
/**
* @dataProvider diffContentProvider
*
* @param $oldText
* @param $newText
* @param $expected
* @param array<string, int|bool|string> $options
*/
public function testHtmlDiff($oldText, $newText, $expected)
public function testHtmlDiff(string $oldText, string $newText, string $expected, array $options)
{
$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', array());
$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', []);

foreach ($options as $option => $value) {
$diff->getConfig()->{$option}($value);
}

$output = $diff->build();

static::assertEquals(
$this->stripExtraWhitespaceAndNewLines($expected),
$this->stripExtraWhitespaceAndNewLines($output)
);
if (isset($options['setKeepNewLines']) === false || $options['setKeepNewLines'] === false) {
$output = $this->stripExtraWhitespaceAndNewLines($output);
$expected = $this->stripExtraWhitespaceAndNewLines($expected);
}

static::assertEquals(trim($expected), trim($output));
}

public function diffContentProvider()
Expand Down
92 changes: 56 additions & 36 deletions tests/Caxy/Tests/HtmlDiff/HtmlFileIterator.php
Expand Up @@ -2,67 +2,55 @@

namespace Caxy\Tests\HtmlDiff;

use Caxy\HtmlDiff\HtmlDiffConfig;
use DOMDocument;
use Exception;

class HtmlFileIterator implements \Iterator
{
protected $files = array();
protected $key = 0;
protected $loadedDiffs = array();
protected $files = [];
protected $key = 0;
protected $loadedDiffs = [];

public function __construct($directory)
{
$this->files = glob($directory.DIRECTORY_SEPARATOR."*.html");
}

/**
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
* {@inheritDoc}
*/
public function current()
{
return $this->loadHtmlFile($this->key);
}

/**
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
* {@inheritDoc}
*/
public function next()
{
$this->key++;
}

/**
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
* {@inheritDoc}
*/
public function key()
{
return basename($this->files[$this->key]);
}

/**
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
* @since 5.0.0
* {@inheritDoc}
*/
public function valid()
{
return isset($this->files[$this->key]);
}

/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
* {@inheritDoc}
*/
public function rewind()
{
Expand All @@ -77,27 +65,59 @@ protected function loadHtmlFile($key)

$html = file_get_contents($filename);

$oldText = $this->parseTagContent('oldText', $html);
$newText = $this->parseTagContent('newText', $html);
$expected = $this->parseTagContent('expected', $html);

if (null === $expected) {
throw new \Exception('HTML fixture content should have an <expected> tag.');
}
$this->loadedDiffs[$filename] = [
$this->parseTagContent('oldText', $html),
$this->parseTagContent('newText', $html),
$this->parseTagContent('expected', $html),
$this->configXmlToArray($this->parseTagContent('options', $html)),
];

$this->loadedDiffs[$filename] = array($oldText, $newText, $expected);
}

return $this->loadedDiffs[$filename];
}

protected function parseTagContent($tagName, $html)
/**
* @return array<string, int|bool|string>
*/
protected function configXmlToArray(string $optionsXml) : array
{
$config = [];

$xml = sprintf('<root>%s</root>', $optionsXml);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXML($xml);

foreach ($dom->getElementsByTagName('option') as $option) {
$type = $option->getAttribute('type');

switch ($type) {
case 'boolean':
$config[$option->getAttribute('name')] = ($option->getAttribute('value') === 'true');

break;
case 'integer':
$config[$option->getAttribute('name')] = (int) $option->getAttribute('value');

break;
default:
$config[$option->getAttribute('name')] = (string) $option->getAttribute('value');

break;
}
}

return $config;
}

protected function parseTagContent(string $tagName, string $content) : string
{
$matches = array();
if (preg_match(sprintf('/<%s\s*[^>]*>(.*)<\/%s\s*>/is', $tagName, $tagName), $html, $matches)) {
$matches = [];

if (preg_match(sprintf('/<%s\s*[^>]*>(.*)<\/%s\s*>/is', $tagName, $tagName), $content, $matches)) {
return $matches[1];
}

return null;
throw new Exception('Fixture file should have an ' . $tagName . ' tag');
}
}
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/ICC-5136.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
Compliance with this section requires that the provisions identified in Sections R401 through R404 labeled as "mandatory" and Section R403.5.3 be met. The building thermal envelope shall be greater than or equal to levels of efficiency and Solar Heat Gain Coefficient in Table 402.1.1 or 402.1.3 of the 2009 <em>International Energy Conservation Code</em>.<ul class="exception"><li><strong>Exception:</strong> Supply and return ducts not completely inside the building thermal envelope shall be insulated to a minimum of R-6.</li></ul>
</oldText>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/first-and-last.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<ol>
<li>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/issue-28-link-changes.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
Testing <a href="http://google.com">Link Changes</a>
And when the link <a href="http://samelink.com">stays the same</a>
Expand Down
40 changes: 40 additions & 0 deletions tests/fixtures/HtmlDiff/keep-newlines-test.html
@@ -0,0 +1,40 @@
<options>
<option type="boolean" name="setKeepNewLines" value="true" />
<option type="boolean" name="setPurifierEnabled" value="false" />
</options>

<oldText>
Please read the non-informative information underneath <br />

<pre>
This is the first paragraph.



This is the second paragraph
</pre>
</oldText>

<newText>
Please read the non-informative information underneath <br />

<pre>
This is the primary paragraph.



This is the secondary paragraph
</pre>
</newText>

<expected>
Please read the non-informative information underneath <br />

<pre>
This is the <del class="diffmod">first</del><ins class="diffmod">primary</ins> paragraph.



This is the <del class="diffmod">second</del><ins class="diffmod">secondary</ins> paragraph
</pre>
</expected>
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/multibyte.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
Änderung
</oldText>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/new-paragraph-and-list.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<i>Corridors</i> shall be fire-resistance rated in accordance with Table 1020.1. The <i>corridor</i> walls required to be fire-resistance rated shall comply with Section 708 for <i>fire partitions</i>.<ul class="exception"><li><b>Exceptions:</b><ol><li>A <i>fire-resistance rating</i> is not required for <i>corridors</i> in an occupancy in Group E where each room that is used for instruction has not less than one door opening directly to the exterior and rooms for assembly purposes have not less than one-half of the required <i>means of egress</i> doors opening directly to the exterior. Exterior doors specified in this exception are required to be at ground level.</li><li>A <i>fire-resistance rating</i> is not required for <i>corridors</i> contained within a <i>dwelling unit</i> or <i>sleeping unit</i> in an occupancy in Groups I-1 and R.</li><li>A <i>fire-resistance rating</i> is not required for <i>corridors</i> in <i>open parking garages</i>.</li><li>A <i>fire-resistance rating</i> is not required for <i>corridors</i> in an occupancy in Group B that is a space requiring only a single <i>means of egress</i> complying with Section 1006.2.</li><li><i>Corridors</i> adjacent to the <i>exterior walls</i> of buildings shall be permitted to have unprotected openings on unrated <i>exterior walls</i> where unrated walls are permitted by Table 602 and unprotected openings are permitted by Table 705.8.</li></ol></li></ul>
</oldText>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-2.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<p>Air handling equipment and HVAC equipment shall be designed and installed to limit the amount of airflow that bypasses the air filters and shall comply with the following: </p>
<ol><li>Channels, racks and other filter retaining constructions that do not seal tightly to the filter frame by means of a friction fit shall be provided with a means to seal the filter frame to the filter retaining construction.</li>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-3.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<div>The path of egress travel to an exit shall not pass through more than one adjacent story.</div>
<div> </div>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-4.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<em>Corridors</em> shall be fire-resistance rated in accordance with Table 1020.1. The <em>corridor</em> walls required to be fire-resistance rated shall comply with Section 708 for <em>fire partitions</em>.<br /><br />In addition, corridors in buildings of Types IIB, IIIB, and VB construction and assigned Risk Categories III and IV in Table 1604.5, other than Group I, shall have a fire resistance rating of not less than 1 hour where such buildings are any of the following:<br /><br /><ol><li>Assigned a Seismic Design Category C or D in Table 1613.3.5(1).</li>
<li>Located in a flood hazard area established in accordance with Section 1612.3.</li>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-5.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<p>In addition to the <em>means of egress</em> required by this chapter, provisions shall be made for <em>emergency escape and rescue openings</em> in Group R occupancies. <em>Basements</em> and sleeping rooms below the fourth story above <em>grade plane</em> shall have at least one exterior <em>emergency escape and rescue opening</em> in accordance with this section. Where <em>basements</em> contain one or more sleeping rooms, <em>emergency escape and rescue openings</em> shall be required in each sleeping room, but shall not be required in adjoining areas of the <em>basement</em>. Such openings shall open directly into a <em>public way</em> or to a <em>yard </em>or<em> court</em> that opens to a <em>public way</em>.</p>
<ul class="exception"><li><strong>Exceptions:</strong><ol><li><em>Groups R-1 and R-2 occupancies are not required to provide emergency and escape openings where they comply with all of the following:</em><ol><li><em>Each story has access to two or more means of egress.<br /></em></li>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-6.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<p>Return air openings for heating, ventilation and air-conditioning systems shall comply with all of the following:</p>
<ol><li>Openings shall not be located less than 10 feet (3048 mm) measured in any direction from an open combustion chamber or draft hood of another appliance located in the same room or space.</li>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-8.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<p>The manufacturer of PEX tubing shall have marked the outside of the tubing with the thermoplastic material designation code in accordance with ASTM F876. The designation code shall consist of the abbreviation "PEX" followed by four digits. The first digit shall represent a chlorine resistance rating as established by testing in accordance with ASTM F876.</p>
</oldText>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/override-9.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<i>Existing buildings </i>that undergo a change of group or occupancy shall comply with this section.<ul class="exception"><li><b>Exception: </b>Type B dwelling or sleeping units required by Section 1107 of the <i>International Building Code </i>are not required to be provided in <i>existing buildings </i>and facilities undergoing a <i>change of occupancy </i>in conjunction with <i>alterations </i>where the <i>work area </i>is 50 percent or less of the aggregate area of the building.</li></ul>
</oldText>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/simple-list.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<ol><li>List item one</li>
<li>List item two with subitems:
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/space-inside-isolated-tag.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
Testing <i>italic text </i>with intentional space and a (word) with parenthesis.
</oldText>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/HtmlDiff/table-whitespace-issue.html
@@ -1,3 +1,5 @@
<options></options>

<oldText>
<table class="Standaardtabel">
<tbody>
Expand Down

0 comments on commit 6cbc63a

Please sign in to comment.