1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace dlindberg\BlobChunk\Manager; |
6
|
|
|
|
7
|
|
|
use dlindberg\BlobChunk\Manager\Config as Config; |
8
|
|
|
use dlindberg\BlobChunk\Manager\Nested as Nested; |
9
|
|
|
use dlindberg\DOMDocumentFactory\DOMDocumentFactory; |
10
|
|
|
use dlindberg\DOMDocumentFactory\DOMDocumentFactoryConfig; |
11
|
|
|
|
12
|
|
|
final class NestedAltDataTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $testData = [ |
18
|
|
|
[ |
19
|
|
|
'group' => 'parents', |
20
|
|
|
'attributes' => [['name' => 'id', 'value' => "a"], ['name' => 'class', 'value' => "b"],], |
21
|
|
|
], |
22
|
|
|
[ |
23
|
|
|
'group' => 'specials', |
24
|
|
|
'attributes' => [['name' => 'id', 'value' => 'c'], ['name' => 'class', 'value' => 'd'],], |
25
|
|
|
], |
26
|
|
|
[ |
27
|
|
|
'group' => 'rowCol', |
28
|
|
|
[ |
29
|
|
|
'parent' => ['type' => 'attribute', 'name' => 'class', 'value' => 'e',], |
30
|
|
|
'colDefs' => ['type' => 'tag', 'value' => 'thead',], |
31
|
|
|
'rowsGroup' => ['type' => 'tag', 'value' => 'tbody',], |
32
|
|
|
'row' => ['type' => 'tag', 'value' => 'tr',], |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
[ |
36
|
|
|
'group' => 'recursive', |
37
|
|
|
'parents' => [ |
38
|
|
|
'attributes' => [ |
39
|
|
|
['name' => 'id', 'value' => 'f'], |
40
|
|
|
['name' => 'class', 'value' => 'g'], |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
'children' => ['tags' => ['li',],], |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
'group' => 'pairs', |
47
|
|
|
[ |
48
|
|
|
'parent' => ['type' => 'attribute', 'name' => 'id', 'value' => 'h',], |
49
|
|
|
'a' => ['type' => 'tag', 'value' => 'dt',], |
50
|
|
|
'b' => ['type' => 'tag', 'value' => 'dd',], |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
'group' => 'splits', |
55
|
|
|
'what' => [ |
56
|
|
|
'tags' => ['p'], |
57
|
|
|
'attributes' => [['name' => 'id', 'value' => 'i'], ['name' => 'class', 'value' => 'j'],], |
58
|
|
|
], |
59
|
|
|
'on' => ['. ', '? ', '! '], |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Config |
65
|
|
|
*/ |
66
|
|
|
private $config; |
67
|
|
|
/** |
68
|
|
|
* @var DOMDocumentFactory |
69
|
|
|
*/ |
70
|
|
|
private $docFactory; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string[] |
74
|
|
|
*/ |
75
|
|
|
private $samples = [ |
76
|
|
|
'<ul><li>sample list</li></ul><ol><li>sample ordered item</li></ol>', |
77
|
|
|
'<table><tr><td>test</td><td>test</td></tr></table>', |
78
|
|
|
'<dl><dt>sample term</dt><dd>sample def</dd></dl><dl>', |
79
|
|
|
'<div id="a">Test side one</div><div class="b">test side tow</div>', |
80
|
|
|
'<div id="c">Test side one</div><div class="d">test side two</div>', |
81
|
|
|
'<div id="i">Test side one</div><div class="j">test side two</div><div class="e">test e table</div>', |
82
|
|
|
'<div id="f">Test side one</div><div class="g">test side two</div><div id="h">test side three</div>', |
83
|
|
|
]; |
84
|
|
|
/** |
85
|
|
|
* @var \DOMElement[] |
86
|
|
|
*/ |
87
|
|
|
private $samplesDocs = []; |
88
|
|
|
|
89
|
|
|
public function __construct(?string $name = null, array $data = [], string $dataName = '') |
90
|
|
|
{ |
91
|
|
|
parent::__construct($name, $data, $dataName); |
92
|
|
|
$this->config = new Config($this->testData); |
93
|
|
|
$pureConfig = \HTMLPurifier_Config::createDefault(); |
94
|
|
|
$pureConfig->set('Attr.EnableID', true); |
95
|
|
|
$this->docFactory = new DOMDocumentFactory( |
96
|
|
|
(new DOMDocumentFactoryConfig())->setInputPurifier(new \HTMLPurifier($pureConfig)) |
97
|
|
|
); |
98
|
|
|
$this->samplesDocs = \array_map(function ($sample) { |
99
|
|
|
return $this->docFactory->getNode($sample); |
100
|
|
|
}, $this->samples); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testNameGroup() |
104
|
|
|
{ |
105
|
|
|
$nested = new Nested($this->config); |
106
|
|
|
$this->assertEquals('recursive', $nested->nameGroup($this->samplesDocs[6]->firstChild)); |
107
|
|
|
$this->assertEquals('rowCol', $nested->nameGroup($this->samplesDocs[5]->childNodes->item(2))); |
108
|
|
|
$this->assertEquals('pairs', $nested->nameGroup($this->samplesDocs[6]->childNodes->item(2))); |
109
|
|
|
$this->assertNull($nested->nameGroup($this->samplesDocs[0]->firstChild)); |
110
|
|
|
$this->assertNull($nested->nameGroup($this->samplesDocs[1]->firstChild)); |
111
|
|
|
$this->assertNull($nested->nameGroup($this->samplesDocs[2]->firstChild)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|