Added a test and a test data
This commit is contained in:
parent
8d8503661f
commit
dbfce2ae54
62
tests/MicrodataParserTest.php
Normal file
62
tests/MicrodataParserTest.php
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace YusufKandemir\MicrodataParser\Tests;
|
||||||
|
|
||||||
|
use YusufKandemir\MicrodataParser\MicrodataParser;
|
||||||
|
|
||||||
|
class MicrodataParserTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
libxml_use_internal_errors(true); // Ignore warnings of DOMDocument::loadHTML check
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data
|
||||||
|
*/
|
||||||
|
public function testExtractData($data)
|
||||||
|
{
|
||||||
|
$dom = new \DOMDocument;
|
||||||
|
$dom->loadHTML($data['source']);
|
||||||
|
$dom->documentURI = $data['uri'];
|
||||||
|
|
||||||
|
$parser = new MicrodataParser($dom);
|
||||||
|
|
||||||
|
$result = $parser->extractMicrodata();
|
||||||
|
|
||||||
|
$this->assertEquals($data['result'], $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo Provide more test data
|
||||||
|
*/
|
||||||
|
public function data()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// https://www.w3.org/TR/microdata/#ex-jsonconv
|
||||||
|
'W3C Example' => [
|
||||||
|
$this->getTestData('W3C', 'source.html', 'result.json')
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTestData($folderName, $sourceName, $resultName)
|
||||||
|
{
|
||||||
|
$folderPath = __DIR__.'/data/'.$folderName.'/';
|
||||||
|
|
||||||
|
$source = file_get_contents($folderPath . $sourceName);
|
||||||
|
$result = file_get_contents($folderPath . $resultName);
|
||||||
|
|
||||||
|
$uri = '';
|
||||||
|
// Set $uri if URI specified in test data
|
||||||
|
if (preg_match('/<!-- URI: (.*) -->/', $source, $matches)) {
|
||||||
|
$uri = $matches[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'uri' => $uri,
|
||||||
|
'source' => $source,
|
||||||
|
'result' => json_decode($result),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
44
tests/data/W3C/result.json
Normal file
44
tests/data/W3C/result.json
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": [ "https://schema.org/BlogPosting" ],
|
||||||
|
"properties": {
|
||||||
|
"headline": [ "Progress report" ],
|
||||||
|
"datePublished": [ "2013-08-29" ],
|
||||||
|
"url": [ "http://blog.example.com/progress-report?comments=0" ],
|
||||||
|
"comment": [
|
||||||
|
{
|
||||||
|
"type": [ "https://schema.org/Comment" ],
|
||||||
|
"properties": {
|
||||||
|
"url": [ "http://blog.example.com/progress-report#c1" ],
|
||||||
|
"creator": [
|
||||||
|
{
|
||||||
|
"type": [ "https://schema.org/Person" ],
|
||||||
|
"properties": {
|
||||||
|
"name": [ "Greg" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dateCreated": [ "2013-08-29" ]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": [ "https://schema.org/Comment" ],
|
||||||
|
"properties": {
|
||||||
|
"url": [ "http://blog.example.com/progress-report#c2" ],
|
||||||
|
"creator": [
|
||||||
|
{
|
||||||
|
"type": [ "https://schema.org/Person" ],
|
||||||
|
"properties": {
|
||||||
|
"name": [ "Charlotte" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dateCreated": [ "2013-08-29" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
35
tests/data/W3C/source.html
Normal file
35
tests/data/W3C/source.html
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<!-- URI: http://blog.example.com/progress-report -->
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<title>My Blog</title>
|
||||||
|
<article itemscope itemtype="https://schema.org/BlogPosting">
|
||||||
|
<header>
|
||||||
|
<h1 itemprop="headline">Progress report</h1>
|
||||||
|
<p><time itemprop="datePublished" datetime="2013-08-29">today</time></p>
|
||||||
|
<link itemprop="url" href="?comments=0">
|
||||||
|
</header>
|
||||||
|
<p>All in all, he's doing well with his swim lessons. The biggest thing was he had trouble
|
||||||
|
putting his head in, but we got it down.</p>
|
||||||
|
<section>
|
||||||
|
<h1>Comments</h1>
|
||||||
|
<article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c1">
|
||||||
|
<link itemprop="url" href="#c1">
|
||||||
|
<footer>
|
||||||
|
<p>Posted by: <span itemprop="creator" itemscope itemtype="https://schema.org/Person">
|
||||||
|
<span itemprop="name">Greg</span>
|
||||||
|
</span></p>
|
||||||
|
<p><time itemprop="dateCreated" datetime="2013-08-29">15 minutes ago</time></p>
|
||||||
|
</footer>
|
||||||
|
<p>Ha!</p>
|
||||||
|
</article>
|
||||||
|
<article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c2">
|
||||||
|
<link itemprop="url" href="#c2">
|
||||||
|
<footer>
|
||||||
|
<p>Posted by: <span itemprop="creator" itemscope itemtype="https://schema.org/Person">
|
||||||
|
<span itemprop="name">Charlotte</span>
|
||||||
|
</span></p>
|
||||||
|
<p><time itemprop="dateCreated" datetime="2013-08-29">5 minutes ago</time></p>
|
||||||
|
</footer>
|
||||||
|
<p>When you say "we got it down"...</p>
|
||||||
|
</article>
|
||||||
|
</section>
|
||||||
|
</article>
|
Loading…
Reference in New Issue
Block a user