2018-11-11 11:35:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace YusufKandemir\MicrodataParser\Tests;
|
|
|
|
|
2018-11-12 15:05:31 +00:00
|
|
|
use YusufKandemir\MicrodataParser\MicrodataDOMDocument;
|
2018-11-11 11:35:59 +00:00
|
|
|
use YusufKandemir\MicrodataParser\MicrodataParser;
|
|
|
|
|
2018-12-26 05:45:06 +00:00
|
|
|
class MicrodataParserTest extends DataDrivenTestCase
|
2018-11-11 11:35:59 +00:00
|
|
|
{
|
2018-11-12 08:31:45 +00:00
|
|
|
protected function getParser($data)
|
|
|
|
{
|
2018-11-12 15:05:31 +00:00
|
|
|
$dom = new MicrodataDOMDocument;
|
2018-11-12 08:30:42 +00:00
|
|
|
$dom->loadHTML($data['source']);
|
|
|
|
$dom->documentURI = $data['uri'];
|
|
|
|
|
|
|
|
return new MicrodataParser($dom);
|
|
|
|
}
|
|
|
|
|
2018-11-11 11:35:59 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider data
|
|
|
|
*/
|
2018-11-12 16:23:35 +00:00
|
|
|
public function testItConvertsMicrodataToObjectFormat($data)
|
2018-11-11 11:35:59 +00:00
|
|
|
{
|
2018-11-12 08:30:42 +00:00
|
|
|
$parser = $this->getParser($data);
|
|
|
|
|
|
|
|
$result = $parser->toObject();
|
|
|
|
|
|
|
|
$this->assertEquals(json_decode($data['result']), $result);
|
|
|
|
}
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-11-12 08:30:42 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider data
|
|
|
|
*/
|
2018-11-12 16:23:35 +00:00
|
|
|
public function testItConvertsMicrodataToArrayFormat($data)
|
2018-11-12 08:30:42 +00:00
|
|
|
{
|
|
|
|
$parser = $this->getParser($data);
|
|
|
|
|
|
|
|
$result = $parser->toArray();
|
|
|
|
|
|
|
|
$this->assertEquals(json_decode($data['result'], true), $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider data
|
|
|
|
*/
|
2018-11-12 16:23:35 +00:00
|
|
|
public function testItConvertsMicrodataToJsonFormat($data)
|
2018-11-12 08:30:42 +00:00
|
|
|
{
|
|
|
|
$parser = $this->getParser($data);
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-11-12 08:30:42 +00:00
|
|
|
$result = $parser->toJSON();
|
2018-11-11 11:35:59 +00:00
|
|
|
|
2018-11-12 08:30:42 +00:00
|
|
|
$this->assertJsonStringEqualsJsonString($data['result'], $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 = $this->getParser($data);
|
|
|
|
|
|
|
|
$resultBefore = $parser->toObject();
|
|
|
|
$resultBeforeUri = $resultBefore->items[0]->properties->work[0];
|
|
|
|
|
|
|
|
$this->assertNotContains($baseUri, $resultBeforeUri);
|
|
|
|
|
|
|
|
$parser->setAbsoluteUriHandler(
|
|
|
|
function (string $value, string $base) use ($baseUri) : string {
|
|
|
|
return $baseUri . $value;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$resultAfter = $parser->toObject();
|
|
|
|
$resultAfterUri = $resultAfter->items[0]->properties->work[0];
|
|
|
|
|
|
|
|
$this->assertContains($baseUri, $resultAfterUri);
|
|
|
|
}
|
2018-11-11 11:35:59 +00:00
|
|
|
}
|