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