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\Ports; |
38
|
|
|
|
39
|
|
|
use Jkphl\RdfaLiteMicrodata\Infrastructure\Factories\HtmlDocumentFactory; |
40
|
|
|
use Jkphl\RdfaLiteMicrodata\Ports\Parser\RdfaLite; |
41
|
|
|
use Jkphl\RdfaLiteMicrodata\Tests\AbstractTest; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* RDFa Lite parser tests |
45
|
|
|
* |
46
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
47
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Tests |
48
|
|
|
*/ |
49
|
|
|
class RdfaLiteParserTest extends AbstractTest |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Test parsing an RDFa Lite HTML file |
53
|
|
|
*/ |
54
|
|
|
public function testRdfaLiteHtmlFile() |
55
|
|
|
{ |
56
|
|
|
$things = (new RdfaLite())->parseHtmlFile( |
57
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.html' |
58
|
|
|
); |
59
|
|
|
$this->assertArrayEquals( |
60
|
|
|
$this->castArray( |
61
|
|
|
json_decode( |
62
|
|
|
file_get_contents( |
63
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.json' |
64
|
|
|
) |
65
|
|
|
) |
66
|
|
|
), |
67
|
|
|
$this->castArray($things) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test parsing an RDFa Lite HTML file with types / properties as IRIs |
73
|
|
|
*/ |
74
|
|
|
public function testRdfaLiteHtmlFileWithIris() |
75
|
|
|
{ |
76
|
|
|
$things = (new RdfaLite(true))->parseHtmlFile( |
77
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.html' |
78
|
|
|
); |
79
|
|
|
$this->assertArrayEquals( |
80
|
|
|
$this->castArray( |
81
|
|
|
json_decode( |
82
|
|
|
file_get_contents( |
83
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR |
84
|
|
|
.'article-rdfa-lite-iri.json' |
85
|
|
|
) |
86
|
|
|
) |
87
|
|
|
), |
88
|
|
|
$this->castArray($things) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test parsing an RDFa Lite DOM |
94
|
|
|
*/ |
95
|
|
|
public function testRdfaLiteDom() |
96
|
|
|
{ |
97
|
|
|
$htmlDomFactory = new HtmlDocumentFactory(); |
98
|
|
|
$dom = $htmlDomFactory->createDocumentFromSource( |
99
|
|
|
file_get_contents( |
100
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.html' |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
$things = (new RdfaLite())->parseDom($dom); |
104
|
|
|
$this->assertArrayEquals( |
105
|
|
|
$this->castArray( |
106
|
|
|
json_decode( |
107
|
|
|
file_get_contents( |
108
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.json' |
109
|
|
|
) |
110
|
|
|
) |
111
|
|
|
), |
112
|
|
|
$this->castArray($things) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Test an invalid file |
118
|
|
|
* |
119
|
|
|
* @expectedException \Jkphl\RdfaLiteMicrodata\Ports\Exceptions\RuntimeException |
120
|
|
|
* @expectedExceptionCode 1487190674 |
121
|
|
|
*/ |
122
|
|
|
public function testInvalidFile() |
123
|
|
|
{ |
124
|
|
|
(new RdfaLite())->parseHtmlFile('invalid'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Test an XML runtime exception |
129
|
|
|
* |
130
|
|
|
* @expectedException \Jkphl\RdfaLiteMicrodata\Ports\Exceptions\RuntimeException |
131
|
|
|
*/ |
132
|
|
|
public function testHTMLRuntimeException() |
133
|
|
|
{ |
134
|
|
|
(new RdfaLite())->parseHtmlFile( |
135
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'empty-default-vocab-rdfa-lite.html' |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Test an HTML out of bounds exception |
141
|
|
|
* |
142
|
|
|
* @expectedException \Jkphl\RdfaLiteMicrodata\Ports\Exceptions\OutOfBoundsException |
143
|
|
|
*/ |
144
|
|
|
public function testHTMLOutOfBoundsException() |
145
|
|
|
{ |
146
|
|
|
(new RdfaLite())->parseHtmlFile( |
147
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'unknown-vocab-prefix-rdfa-lite.html' |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Test parsing an RDFa Lite XML (XHTML) file |
153
|
|
|
*/ |
154
|
|
|
public function testRdfaLiteXmlFile() |
155
|
|
|
{ |
156
|
|
|
$things = (new RdfaLite())->parseXmlFile( |
157
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'typed-property-rdfa-lite.xhtml' |
158
|
|
|
); |
159
|
|
|
$this->assertArrayEquals( |
160
|
|
|
$this->castArray( |
161
|
|
|
json_decode( |
162
|
|
|
file_get_contents( |
163
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures' |
164
|
|
|
.DIRECTORY_SEPARATOR.'typed-property-rdfa-lite.json' |
165
|
|
|
) |
166
|
|
|
) |
167
|
|
|
), |
168
|
|
|
$this->castArray($things) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Test an XML runtime exception |
175
|
|
|
* |
176
|
|
|
* @expectedException \Jkphl\RdfaLiteMicrodata\Ports\Exceptions\RuntimeException |
177
|
|
|
*/ |
178
|
|
|
public function testXMLRuntimeException() |
179
|
|
|
{ |
180
|
|
|
(new RdfaLite())->parseXmlFile( |
181
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'empty-default-vocab-rdfa-lite.html' |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Test an XML out of bounds exception |
187
|
|
|
* |
188
|
|
|
* @expectedException \Jkphl\RdfaLiteMicrodata\Ports\Exceptions\OutOfBoundsException |
189
|
|
|
*/ |
190
|
|
|
public function testXMLOutOfBoundsException() |
191
|
|
|
{ |
192
|
|
|
(new RdfaLite())->parseXmlFile( |
193
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'unknown-vocab-prefix-rdfa-lite.html' |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|