2018-11-11 11:35:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace YusufKandemir\MicrodataParser\Tests;
|
|
|
|
|
|
|
|
use YusufKandemir\MicrodataParser\MicrodataParser;
|
2018-12-27 05:44:11 +00:00
|
|
|
use YusufKandemir\MicrodataParser\MicrodataDocumentParser;
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-12-26 05:45:06 +00:00
|
|
|
class MicrodataParserTest extends DataDrivenTestCase
|
2018-11-11 11:35:59 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-12-27 05:44:11 +00:00
|
|
|
* @param array $data
|
2018-11-11 11:35:59 +00:00
|
|
|
* @dataProvider data
|
|
|
|
*/
|
2018-12-27 05:44:11 +00:00
|
|
|
public function testItParsesHtml(array $data)
|
2018-11-11 11:35:59 +00:00
|
|
|
{
|
2018-12-27 05:44:11 +00:00
|
|
|
$parser = new MicrodataParser();
|
|
|
|
$result = $parser->parseHTML($data['source'], $data['uri']);
|
2018-11-12 08:30:42 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$expected = \json_decode($data['result']);
|
2018-11-12 08:30:42 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2018-11-12 08:30:42 +00:00
|
|
|
}
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-11-12 08:30:42 +00:00
|
|
|
/**
|
2018-12-27 05:44:11 +00:00
|
|
|
* @param array $data
|
2018-11-12 08:30:42 +00:00
|
|
|
* @dataProvider data
|
|
|
|
*/
|
2018-12-27 05:44:11 +00:00
|
|
|
public function testItParsesHtmlFile(array $data)
|
2018-11-12 08:30:42 +00:00
|
|
|
{
|
2018-12-27 05:44:11 +00:00
|
|
|
$parser = new MicrodataParser();
|
|
|
|
$result = $parser->parseHTMLFile($data['path'], $data['uri']);
|
2018-11-12 08:30:42 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$expected = \json_decode($data['result']);
|
2018-11-12 08:30:42 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2018-11-12 08:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 05:44:11 +00:00
|
|
|
* @param array $data
|
2018-11-12 08:30:42 +00:00
|
|
|
* @dataProvider data
|
|
|
|
*/
|
2018-12-27 05:44:11 +00:00
|
|
|
public function testItParsesDomDocument(array $data)
|
2018-11-12 08:30:42 +00:00
|
|
|
{
|
2018-12-27 05:44:11 +00:00
|
|
|
$dom = new \DOMDocument;
|
|
|
|
$dom->loadHTML($data['source']);
|
|
|
|
$dom->documentURI = $data['uri'];
|
|
|
|
|
|
|
|
$parser = new MicrodataParser();
|
|
|
|
$result = $parser->parseDOMDocument($dom);
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$expected = \json_decode($data['result']);
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2018-11-11 11:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 07:07:15 +00:00
|
|
|
public function testItUsesAbsoluteUriHandlerWhenHandlingAbsoluteUris()
|
|
|
|
{
|
|
|
|
$baseUri = 'https://absolute.uri.handler/';
|
|
|
|
$data = $this->data()['Itemref & src based tags'][0];
|
2018-12-27 05:44:11 +00:00
|
|
|
$parser = new MicrodataParser();
|
2018-12-18 07:07:15 +00:00
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$resultBefore = $parser->parseHTML($data['source'], $data['uri']);
|
2018-12-18 07:07:15 +00:00
|
|
|
$resultBeforeUri = $resultBefore->items[0]->properties->work[0];
|
|
|
|
|
|
|
|
$this->assertNotContains($baseUri, $resultBeforeUri);
|
|
|
|
|
2018-12-27 05:44:11 +00:00
|
|
|
$absoluteUriHandler = function (string $value, string $base) use ($baseUri) : string {
|
|
|
|
return $baseUri . $value;
|
|
|
|
};
|
2018-12-18 07:07:15 +00:00
|
|
|
|
2018-12-27 07:10:35 +00:00
|
|
|
$resultAfter = $parser
|
|
|
|
->setAbsoluteUriHandler($absoluteUriHandler)
|
|
|
|
->parseHTML($data['source'], $data['uri']);
|
2018-12-18 07:07:15 +00:00
|
|
|
$resultAfterUri = $resultAfter->items[0]->properties->work[0];
|
|
|
|
|
|
|
|
$this->assertContains($baseUri, $resultAfterUri);
|
|
|
|
}
|
2018-11-11 11:35:59 +00:00
|
|
|
}
|