microdata-parser/tests/MicrodataParserTest.php
Yusuf Kandemir 9b723a4c42 Extract data related testing logic to a TestCase
This is obviously for cleaner tests and reusability
2018-12-26 08:45:06 +03:00

78 lines
2.0 KiB
PHP

<?php
namespace YusufKandemir\MicrodataParser\Tests;
use YusufKandemir\MicrodataParser\MicrodataDOMDocument;
use YusufKandemir\MicrodataParser\MicrodataParser;
class MicrodataParserTest extends DataDrivenTestCase
{
protected function getParser($data)
{
$dom = new MicrodataDOMDocument;
$dom->loadHTML($data['source']);
$dom->documentURI = $data['uri'];
return new MicrodataParser($dom);
}
/**
* @dataProvider data
*/
public function testItConvertsMicrodataToObjectFormat($data)
{
$parser = $this->getParser($data);
$result = $parser->toObject();
$this->assertEquals(json_decode($data['result']), $result);
}
/**
* @dataProvider data
*/
public function testItConvertsMicrodataToArrayFormat($data)
{
$parser = $this->getParser($data);
$result = $parser->toArray();
$this->assertEquals(json_decode($data['result'], true), $result);
}
/**
* @dataProvider data
*/
public function testItConvertsMicrodataToJsonFormat($data)
{
$parser = $this->getParser($data);
$result = $parser->toJSON();
$this->assertJsonStringEqualsJsonString($data['result'], $result);
}
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);
}
}