microdata-parser/tests/MicrodataParserTest.php

79 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2018-11-11 11:35:59 +00:00
<?php
namespace YusufKandemir\MicrodataParser\Tests;
use YusufKandemir\MicrodataParser\MicrodataParser;
use YusufKandemir\MicrodataParser\MicrodataDocumentParser;
2018-11-11 11:35:59 +00:00
class MicrodataParserTest extends DataDrivenTestCase
2018-11-11 11:35:59 +00:00
{
/**
* @param array $data
2018-11-11 11:35:59 +00:00
* @dataProvider data
*/
public function testItParsesHtml(array $data)
2018-11-11 11:35:59 +00:00
{
$parser = new MicrodataParser();
$result = $parser->parseHTML($data['source'], $data['uri']);
2018-11-12 08:30:42 +00:00
$expected = \json_decode($data['result']);
2018-11-12 08:30:42 +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
/**
* @param array $data
2018-11-12 08:30:42 +00:00
* @dataProvider data
*/
public function testItParsesHtmlFile(array $data)
2018-11-12 08:30:42 +00:00
{
$parser = new MicrodataParser();
$result = $parser->parseHTMLFile($data['path'], $data['uri']);
2018-11-12 08:30:42 +00:00
$expected = \json_decode($data['result']);
2018-11-12 08:30:42 +00:00
$this->assertEquals($expected, $result);
2018-11-12 08:30:42 +00:00
}
/**
* @param array $data
2018-11-12 08:30:42 +00:00
* @dataProvider data
*/
public function testItParsesDomDocument(array $data)
2018-11-12 08:30:42 +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
$expected = \json_decode($data['result']);
2018-11-11 11:35:59 +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];
$parser = new MicrodataParser();
2018-12-18 07:07:15 +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);
$absoluteUriHandler = function (string $value, string $base) use ($baseUri) : string {
return $baseUri . $value;
};
2018-12-18 07:07:15 +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
}