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 © 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\Micrometa\Tests\Application; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Contract\ParsingResultInterface; |
40
|
|
|
use Jkphl\Micrometa\Application\Item\Item; |
41
|
|
|
use Jkphl\Micrometa\Application\Service\ExtractorService; |
42
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\DocumentFactory; |
43
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\Microdata; |
44
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\RdfaLite; |
45
|
|
|
use League\Uri\Schemes\Http; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Extractor tests |
49
|
|
|
* |
50
|
|
|
* @package Jkphl\Micrometa |
51
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
52
|
|
|
*/ |
53
|
|
|
class ExtractorTest extends \PHPUnit_Framework_TestCase |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* RDFa Lite 1.1 HTML document |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
const RDFA_LITE_HTML_URL = 'http://localhost:1349/article-rdfa-lite.html'; |
61
|
|
|
/** |
62
|
|
|
* Microdata HTML document |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
const MICRODATA_HTML_URL = 'http://localhost:1349/article-microdata.html'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test the RDFa Lite 1.1 extraction |
70
|
|
|
*/ |
71
|
|
|
public function testRdfaLiteExtraction() |
72
|
|
|
{ |
73
|
|
|
// Create a DOM with RDFa Lite 1.1 markup |
74
|
|
|
$rdfaLite = file_get_contents( |
75
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'article-rdfa-lite.html' |
76
|
|
|
); |
77
|
|
|
$rdfaLiteDom = DocumentFactory::createFromString($rdfaLite); |
78
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $rdfaLiteDom); |
79
|
|
|
|
80
|
|
|
// Create an RDFa Lite 1.1 parser |
81
|
|
|
$rdfaLiteUri = Http::createFromString(self::RDFA_LITE_HTML_URL); |
82
|
|
|
$rdfaLiteParser = new RdfaLite($rdfaLiteUri); |
83
|
|
|
$this->assertEquals($rdfaLiteUri, $rdfaLiteParser->getUri()); |
84
|
|
|
|
85
|
|
|
// Create an extractor service |
86
|
|
|
$extractorService = new ExtractorService(); |
87
|
|
|
$rdfaLiteItems = $extractorService->extract($rdfaLiteDom, $rdfaLiteParser); |
88
|
|
|
$this->assertInstanceOf(ParsingResultInterface::class, $rdfaLiteItems); |
89
|
|
|
$this->assertEquals(1, count($rdfaLiteItems->getItems())); |
90
|
|
|
$this->assertEquals(0, count($rdfaLiteItems->getExtra())); |
91
|
|
|
$this->assertInstanceOf(Item::class, $rdfaLiteItems->getItems()[0]); |
92
|
|
|
$this->assertEquals(RdfaLite::FORMAT, $rdfaLiteItems->getItems()[0]->getFormat()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Test the HTML Microdata extraction |
97
|
|
|
*/ |
98
|
|
|
public function testMicrodataExtraction() |
99
|
|
|
{ |
100
|
|
|
// Create a DOM with HTML Microdata markup |
101
|
|
|
$microdata = file_get_contents( |
102
|
|
|
dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'article-microdata.html' |
103
|
|
|
); |
104
|
|
|
$microdataDom = DocumentFactory::createFromString($microdata); |
105
|
|
|
$this->assertInstanceOf(\DOMDocument::class, $microdataDom); |
106
|
|
|
|
107
|
|
|
// Create an RDFa Lite 1.1 parser |
108
|
|
|
$microdataUri = Http::createFromString(self::MICRODATA_HTML_URL); |
109
|
|
|
$microdataParser = new Microdata($microdataUri); |
110
|
|
|
$this->assertEquals($microdataUri, $microdataParser->getUri()); |
111
|
|
|
|
112
|
|
|
// Create an extractor service |
113
|
|
|
$extractorService = new ExtractorService(); |
114
|
|
|
$microdataItems = $extractorService->extract($microdataDom, $microdataParser); |
115
|
|
|
$this->assertInstanceOf(ParsingResultInterface::class, $microdataItems); |
116
|
|
|
$this->assertEquals(1, count($microdataItems->getItems())); |
117
|
|
|
$this->assertEquals(0, count($microdataItems->getExtra())); |
118
|
|
|
$this->assertInstanceOf(Item::class, $microdataItems->getItems()[0]); |
119
|
|
|
$this->assertEquals(microdata::FORMAT, $microdataItems->getItems()[0]->getFormat()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testMicroformatExtraction() { |
123
|
|
|
// $composer = new Composer |
124
|
|
|
// \Composer\Factory::getComposerFile() |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|