1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* rdfa-lite-microdata |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
8
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Tests |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\RdfaLiteMicrodata\Tests\Application; |
38
|
|
|
|
39
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Context\RdfaLiteContext; |
40
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Contract\ElementProcessorInterface; |
41
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Parser\DOMIterator; |
42
|
|
|
use Jkphl\RdfaLiteMicrodata\Infrastructure\Parser\RdfaLiteElementProcessor; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* DOM node iterator tests |
46
|
|
|
* |
47
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
48
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Tests |
49
|
|
|
*/ |
50
|
|
|
class DOMNodeIteratorTest extends ParserIteratorTestBase |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* RdfaLiteContext |
54
|
|
|
* |
55
|
|
|
* @var RdfaLiteContext |
56
|
|
|
*/ |
57
|
|
|
protected $context; |
58
|
|
|
/** |
59
|
|
|
* Element processor mock |
60
|
|
|
* |
61
|
|
|
* @var ElementProcessorInterface |
62
|
|
|
*/ |
63
|
|
|
protected $elementProcessor; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test recursive DOM node iteration |
67
|
|
|
*/ |
68
|
|
|
public function testDomNodeIteration() |
69
|
|
|
{ |
70
|
|
|
$dom = new \DOMDocument(); |
71
|
|
|
$dom->loadHTML(self::$personRdfa); |
72
|
|
|
$context = new RdfaLiteContext(); |
73
|
|
|
$elementProcessor = $this->getMock(ElementProcessorInterface::class); |
74
|
|
|
$elementProcessor->method('processElement')->willReturn($context); |
75
|
|
|
$elementProcessor->method('processElementChildren')->willReturn($context); |
76
|
|
|
/** @var $elementProcessor ElementProcessorInterface */ |
77
|
|
|
$this->iterateDom($dom, $context, $elementProcessor); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Iterate through the dom |
82
|
|
|
* |
83
|
|
|
* @param \DOMDocument $dom DOM |
84
|
|
|
* @param RdfaLiteContext $context RdfaLiteContext |
85
|
|
|
* @param ElementProcessorInterface $elementProcessor Element processor |
86
|
|
|
*/ |
87
|
|
|
protected function iterateDom( |
88
|
|
|
\DOMDocument $dom, |
89
|
|
|
RdfaLiteContext $context, |
90
|
|
|
ElementProcessorInterface $elementProcessor |
91
|
|
|
) { |
92
|
|
|
$domNodeIterator = new DOMIterator($dom->childNodes, $context, $elementProcessor); |
93
|
|
|
$this->assertInstanceOf(DOMIterator::class, $domNodeIterator); |
94
|
|
|
|
95
|
|
|
$elements = ['html', 'head', 'title', 'body', 'h1', 'p', 'span', 'span', 'img', 'span']; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Recursively run through all child elements |
99
|
|
|
* |
100
|
|
|
* @var int $nodeIndex Element index |
101
|
|
|
* @var \DOMElement $node Element |
102
|
|
|
*/ |
103
|
|
|
foreach ($domNodeIterator->getRecursiveIterator() as $element) { |
104
|
|
|
if ($element instanceof \DOMElement) { |
105
|
|
|
$this->assertEquals($element->localName, array_shift($elements)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$this->assertEquals(0, count($elements)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Test the RDFa Lite element processor |
113
|
|
|
*/ |
114
|
|
|
public function testRdfaLiteProcessor() |
115
|
|
|
{ |
116
|
|
|
$dom = new \DOMDocument(); |
117
|
|
|
$dom->loadHTML(self::$personRdfa); |
118
|
|
|
$context = new RdfaLiteContext(); |
119
|
|
|
$this->iterateDom($dom, $context, (new RdfaLiteElementProcessor)->setHtml(true)); |
120
|
|
|
$this->validatePersonResult($context->getChildren()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Test an invalid node list |
125
|
|
|
* |
126
|
|
|
* @expectedException \Jkphl\RdfaLiteMicrodata\Application\Exceptions\RuntimeException |
127
|
|
|
* @expectedExceptionCode 1487461118 |
128
|
|
|
*/ |
129
|
|
|
public function testInvalidNodeList() |
130
|
|
|
{ |
131
|
|
|
$context = new RdfaLiteContext(); |
132
|
|
|
/** @var ElementProcessorInterface $elementProcessor */ |
133
|
|
|
$elementProcessor = $this->getMock(ElementProcessorInterface::class); |
134
|
|
|
new DOMIterator(null, $context, $elementProcessor); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|