Added Microdata class and test

Microdata class allows creating MicrodataParser from different sources.
This commit is contained in:
Yusuf Kandemir 2018-11-12 15:25:04 +03:00
parent 7c3700e8cf
commit daa8efa0fa
2 changed files with 65 additions and 0 deletions

29
src/Microdata.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace YusufKandemir\MicrodataParser;
abstract class Microdata
{
public static function fromHTML($html, $documentURI = '')
{
$dom = new \DOMDocument;
$dom->loadHTML($html, LIBXML_NOERROR);
$dom->documentURI = $documentURI;
return new MicrodataParser($dom);
}
public static function fromHTMLFile($filename, $documentURI = '')
{
$dom = new \DOMDocument;
$dom->loadHTMLFile($filename);
$dom->documentURI = $documentURI;
return new MicrodataParser($dom);
}
public static function fromDOMDocument(\DOMDocument $dom)
{
return new MicrodataParser($dom);
}
}

36
tests/MicrodataTest.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace YusufKandemir\MicrodataParser\Tests;
use YusufKandemir\MicrodataParser\Microdata;
use YusufKandemir\MicrodataParser\MicrodataParser;
class MicrodataTest extends \PHPUnit\Framework\TestCase
{
protected $htmlFileName = __DIR__ . '/data/W3C/source.html';
public function testFromHTML()
{
$html = file_get_contents($this->htmlFileName);
$microdata = Microdata::fromHTML($html);
$this->assertInstanceOf(MicrodataParser::class, $microdata);
}
public function testFromHTMLFile()
{
$microdata = Microdata::fromHTMLFile($this->htmlFileName);
$this->assertInstanceOf(MicrodataParser::class, $microdata);
}
public function testFromDOMDocument()
{
$dom = new \DOMDocument;
$dom->loadHTMLFile($this->htmlFileName);
$microdata = Microdata::fromDOMDocument($dom);
$this->assertInstanceOf(MicrodataParser::class, $microdata);
}
}