|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* micrometa |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Micrometa |
|
8
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2018 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 © 2018 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\Micrometa\Tests\Application; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Domfactory\Ports\Dom; |
|
40
|
|
|
use Jkphl\Micrometa\Application\Contract\ParsingResultInterface; |
|
41
|
|
|
use Jkphl\Micrometa\Application\Item\Item; |
|
42
|
|
|
use Jkphl\Micrometa\Application\Service\ExtractorService; |
|
43
|
|
|
use Jkphl\Micrometa\Infrastructure\Logger\ExceptionLogger; |
|
44
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\Microdata; |
|
45
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\Microformats; |
|
46
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\RdfaLite; |
|
47
|
|
|
use Jkphl\Micrometa\Tests\AbstractTestBase; |
|
48
|
|
|
use League\Uri\Http; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Extractor tests |
|
52
|
|
|
* |
|
53
|
|
|
* @package Jkphl\Micrometa |
|
54
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
|
55
|
|
|
*/ |
|
56
|
|
|
class ExtractorTest extends AbstractTestBase |
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* Test the RDFa Lite 1.1 extraction |
|
60
|
|
|
*/ |
|
61
|
|
|
public function testRdfaLiteExtraction() |
|
62
|
|
|
{ |
|
63
|
|
|
// Create a DOM with RDFa Lite 1.1 markup |
|
64
|
|
|
list($rdfaLiteUri, $rdfaLiteDom) = $this->getUriFixture('rdfa-lite/article-rdfa-lite.html'); |
|
65
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $rdfaLiteDom); |
|
66
|
|
|
|
|
67
|
|
|
// Create an RDFa Lite 1.1 parser |
|
68
|
|
|
$rdfaLiteParser = new RdfaLite($rdfaLiteUri, self::getLogger()); |
|
69
|
|
|
$this->assertEquals($rdfaLiteUri, $rdfaLiteParser->getUri()); |
|
70
|
|
|
|
|
71
|
|
|
// Create an extractor service |
|
72
|
|
|
$extractorService = new ExtractorService(); |
|
73
|
|
|
$rdfaLiteItems = $extractorService->extract($rdfaLiteDom, $rdfaLiteParser); |
|
74
|
|
|
$this->assertInstanceOf(ParsingResultInterface::class, $rdfaLiteItems); |
|
75
|
|
|
$this->assertEquals(1, count($rdfaLiteItems->getItems())); |
|
76
|
|
|
$this->assertInstanceOf(Item::class, $rdfaLiteItems->getItems()[0]); |
|
77
|
|
|
$this->assertEquals(RdfaLite::FORMAT, $rdfaLiteItems->getItems()[0]->getFormat()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Test the HTML Microdata extraction |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testMicrodataExtraction() |
|
84
|
|
|
{ |
|
85
|
|
|
// Create a DOM with HTML Microdata markup |
|
86
|
|
|
list($microdataUri, $microdataDom) = $this->getUriFixture('html-microdata/article-microdata.html'); |
|
87
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $microdataDom); |
|
88
|
|
|
|
|
89
|
|
|
// Create an HTML microdata parser |
|
90
|
|
|
$microdataParser = new Microdata($microdataUri, new ExceptionLogger()); |
|
91
|
|
|
$this->assertEquals($microdataUri, $microdataParser->getUri()); |
|
92
|
|
|
|
|
93
|
|
|
// Create an extractor service |
|
94
|
|
|
$extractorService = new ExtractorService(); |
|
95
|
|
|
$microdataItems = $extractorService->extract($microdataDom, $microdataParser); |
|
96
|
|
|
$this->assertInstanceOf(ParsingResultInterface::class, $microdataItems); |
|
97
|
|
|
$this->assertEquals(1, count($microdataItems->getItems())); |
|
98
|
|
|
$this->assertInstanceOf(Item::class, $microdataItems->getItems()[0]); |
|
99
|
|
|
$this->assertEquals(microdata::FORMAT, $microdataItems->getItems()[0]->getFormat()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Test the Microformats extraction |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testMicroformatsExtraction() |
|
106
|
|
|
{ |
|
107
|
|
|
$microformatsTests = \ComposerLocator::getPath('mf2/tests').DIRECTORY_SEPARATOR.'tests'. |
|
108
|
|
|
DIRECTORY_SEPARATOR.'microformats-v2'.DIRECTORY_SEPARATOR; |
|
109
|
|
|
|
|
110
|
|
|
$this->getAndTestMicroformatsExtractionBase( |
|
111
|
|
|
$microformatsTests.'h-product'.DIRECTORY_SEPARATOR.'aggregate.html' |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Run a microformats base test on a file and return the items |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $file File name |
|
119
|
|
|
* |
|
120
|
|
|
* @return ParsingResultInterface |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getAndTestMicroformatsExtractionBase($file) |
|
123
|
|
|
{ |
|
124
|
|
|
// Create a DOM with Microformats markup |
|
125
|
|
|
$microformats = file_get_contents($file); |
|
126
|
|
|
$microformatsDom = Dom::createFromString($microformats); |
|
127
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $microformatsDom); |
|
128
|
|
|
|
|
129
|
|
|
// Create a Microformats 2 parser |
|
130
|
|
|
$microformatsUri = Http::createFromString('http://localhost:1349/aggregate.html'); |
|
131
|
|
|
$microformatsParser = new Microformats($microformatsUri, new ExceptionLogger()); |
|
132
|
|
|
$this->assertEquals($microformatsUri, $microformatsParser->getUri()); |
|
133
|
|
|
|
|
134
|
|
|
// Create and run an extractor service |
|
135
|
|
|
$extractorService = new ExtractorService(); |
|
136
|
|
|
$microformatsItems = $extractorService->extract($microformatsDom, $microformatsParser); |
|
137
|
|
|
$this->assertInstanceOf(ParsingResultInterface::class, $microformatsItems); |
|
138
|
|
|
$this->assertEquals(1, count($microformatsItems->getItems())); |
|
139
|
|
|
$this->assertInstanceOf(Item::class, $microformatsItems->getItems()[0]); |
|
140
|
|
|
$this->assertEquals(Microformats::FORMAT, $microformatsItems->getItems()[0]->getFormat()); |
|
141
|
|
|
|
|
142
|
|
|
return $microformatsItems; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Test the Microformats extraction |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testNestedMicroformatsExtraction() |
|
149
|
|
|
{ |
|
150
|
|
|
$microformatsItems = $this->getAndTestMicroformatsExtractionBase( |
|
151
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR. |
|
152
|
|
|
'microformats'.DIRECTORY_SEPARATOR.'nested-events.html' |
|
153
|
|
|
); |
|
154
|
|
|
$this->assertEquals(2, count($microformatsItems->getItems()[0]->getChildren())); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|