Skip to content

Commit 6cbc63a

Browse files
authored
Configurable function tests
Configurable function tests
2 parents 6bc1c4a + ba5428c commit 6cbc63a

18 files changed

+141
-45
lines changed

tests/Caxy/Tests/HtmlDiff/Functional/HtmlDiffFunctionalTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Caxy\Tests\HtmlDiff\Functional;
44

55
use Caxy\HtmlDiff\HtmlDiff;
6+
use Caxy\HtmlDiff\HtmlDiffConfig;
67
use Caxy\Tests\AbstractTest;
78
use Caxy\Tests\HtmlDiff\HtmlFileIterator;
89

@@ -11,19 +12,24 @@ class HtmlDiffFunctionalTest extends AbstractTest
1112
/**
1213
* @dataProvider diffContentProvider
1314
*
14-
* @param $oldText
15-
* @param $newText
16-
* @param $expected
15+
* @param array<string, int|bool|string> $options
1716
*/
18-
public function testHtmlDiff($oldText, $newText, $expected)
17+
public function testHtmlDiff(string $oldText, string $newText, string $expected, array $options)
1918
{
20-
$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', array());
19+
$diff = new HtmlDiff(trim($oldText), trim($newText), 'UTF-8', []);
20+
21+
foreach ($options as $option => $value) {
22+
$diff->getConfig()->{$option}($value);
23+
}
24+
2125
$output = $diff->build();
2226

23-
static::assertEquals(
24-
$this->stripExtraWhitespaceAndNewLines($expected),
25-
$this->stripExtraWhitespaceAndNewLines($output)
26-
);
27+
if (isset($options['setKeepNewLines']) === false || $options['setKeepNewLines'] === false) {
28+
$output = $this->stripExtraWhitespaceAndNewLines($output);
29+
$expected = $this->stripExtraWhitespaceAndNewLines($expected);
30+
}
31+
32+
static::assertEquals(trim($expected), trim($output));
2733
}
2834

2935
public function diffContentProvider()

tests/Caxy/Tests/HtmlDiff/HtmlFileIterator.php

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,55 @@
22

33
namespace Caxy\Tests\HtmlDiff;
44

5+
use Caxy\HtmlDiff\HtmlDiffConfig;
6+
use DOMDocument;
7+
use Exception;
8+
59
class HtmlFileIterator implements \Iterator
610
{
7-
protected $files = array();
8-
protected $key = 0;
9-
protected $loadedDiffs = array();
11+
protected $files = [];
12+
protected $key = 0;
13+
protected $loadedDiffs = [];
1014

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

1620
/**
17-
* Return the current element
18-
* @link http://php.net/manual/en/iterator.current.php
19-
* @return mixed Can return any type.
20-
* @since 5.0.0
21+
* {@inheritDoc}
2122
*/
2223
public function current()
2324
{
2425
return $this->loadHtmlFile($this->key);
2526
}
2627

2728
/**
28-
* Move forward to next element
29-
* @link http://php.net/manual/en/iterator.next.php
30-
* @return void Any returned value is ignored.
31-
* @since 5.0.0
29+
* {@inheritDoc}
3230
*/
3331
public function next()
3432
{
3533
$this->key++;
3634
}
3735

3836
/**
39-
* Return the key of the current element
40-
* @link http://php.net/manual/en/iterator.key.php
41-
* @return mixed scalar on success, or null on failure.
42-
* @since 5.0.0
37+
* {@inheritDoc}
4338
*/
4439
public function key()
4540
{
4641
return basename($this->files[$this->key]);
4742
}
4843

4944
/**
50-
* Checks if current position is valid
51-
* @link http://php.net/manual/en/iterator.valid.php
52-
* @return boolean The return value will be casted to boolean and then evaluated.
53-
* Returns true on success or false on failure.
54-
* @since 5.0.0
45+
* {@inheritDoc}
5546
*/
5647
public function valid()
5748
{
5849
return isset($this->files[$this->key]);
5950
}
6051

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

7866
$html = file_get_contents($filename);
7967

80-
$oldText = $this->parseTagContent('oldText', $html);
81-
$newText = $this->parseTagContent('newText', $html);
82-
$expected = $this->parseTagContent('expected', $html);
83-
84-
if (null === $expected) {
85-
throw new \Exception('HTML fixture content should have an <expected> tag.');
86-
}
68+
$this->loadedDiffs[$filename] = [
69+
$this->parseTagContent('oldText', $html),
70+
$this->parseTagContent('newText', $html),
71+
$this->parseTagContent('expected', $html),
72+
$this->configXmlToArray($this->parseTagContent('options', $html)),
73+
];
8774

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

9177
return $this->loadedDiffs[$filename];
9278
}
9379

94-
protected function parseTagContent($tagName, $html)
80+
/**
81+
* @return array<string, int|bool|string>
82+
*/
83+
protected function configXmlToArray(string $optionsXml) : array
84+
{
85+
$config = [];
86+
87+
$xml = sprintf('<root>%s</root>', $optionsXml);
88+
$dom = new DOMDocument('1.0', 'UTF-8');
89+
$dom->loadXML($xml);
90+
91+
foreach ($dom->getElementsByTagName('option') as $option) {
92+
$type = $option->getAttribute('type');
93+
94+
switch ($type) {
95+
case 'boolean':
96+
$config[$option->getAttribute('name')] = ($option->getAttribute('value') === 'true');
97+
98+
break;
99+
case 'integer':
100+
$config[$option->getAttribute('name')] = (int) $option->getAttribute('value');
101+
102+
break;
103+
default:
104+
$config[$option->getAttribute('name')] = (string) $option->getAttribute('value');
105+
106+
break;
107+
}
108+
}
109+
110+
return $config;
111+
}
112+
113+
protected function parseTagContent(string $tagName, string $content) : string
95114
{
96-
$matches = array();
97-
if (preg_match(sprintf('/<%s\s*[^>]*>(.*)<\/%s\s*>/is', $tagName, $tagName), $html, $matches)) {
115+
$matches = [];
116+
117+
if (preg_match(sprintf('/<%s\s*[^>]*>(.*)<\/%s\s*>/is', $tagName, $tagName), $content, $matches)) {
98118
return $matches[1];
99119
}
100120

101-
return null;
121+
throw new Exception('Fixture file should have an ' . $tagName . ' tag');
102122
}
103123
}

tests/fixtures/HtmlDiff/ICC-5136.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
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>
35
</oldText>

tests/fixtures/HtmlDiff/first-and-last.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
<ol>
35
<li>

tests/fixtures/HtmlDiff/issue-28-link-changes.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
Testing <a href="http://google.com">Link Changes</a>
35
And when the link <a href="http://samelink.com">stays the same</a>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<options>
2+
<option type="boolean" name="setKeepNewLines" value="true" />
3+
<option type="boolean" name="setPurifierEnabled" value="false" />
4+
</options>
5+
6+
<oldText>
7+
Please read the non-informative information underneath <br />
8+
9+
<pre>
10+
This is the first paragraph.
11+
12+
13+
14+
This is the second paragraph
15+
</pre>
16+
</oldText>
17+
18+
<newText>
19+
Please read the non-informative information underneath <br />
20+
21+
<pre>
22+
This is the primary paragraph.
23+
24+
25+
26+
This is the secondary paragraph
27+
</pre>
28+
</newText>
29+
30+
<expected>
31+
Please read the non-informative information underneath <br />
32+
33+
<pre>
34+
This is the <del class="diffmod">first</del><ins class="diffmod">primary</ins> paragraph.
35+
36+
37+
38+
This is the <del class="diffmod">second</del><ins class="diffmod">secondary</ins> paragraph
39+
</pre>
40+
</expected>

tests/fixtures/HtmlDiff/multibyte.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
Änderung
35
</oldText>

tests/fixtures/HtmlDiff/new-paragraph-and-list.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
<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>
35
</oldText>

tests/fixtures/HtmlDiff/override-2.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
<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>
35
<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>

tests/fixtures/HtmlDiff/override-3.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<options></options>
2+
13
<oldText>
24
<div>The path of egress travel to an exit shall not pass through more than one adjacent story.</div>
35
<div> </div>

0 commit comments

Comments
 (0)