From dbfce2ae5481270b5eae74c0fe1717711b0c0a81 Mon Sep 17 00:00:00 2001 From: Yusuf Kandemir Date: Sun, 11 Nov 2018 14:35:59 +0300 Subject: [PATCH] Added a test and a test data --- tests/MicrodataParserTest.php | 62 +++++++++++++++++++++++++++++++++++ tests/data/W3C/result.json | 44 +++++++++++++++++++++++++ tests/data/W3C/source.html | 35 ++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 tests/MicrodataParserTest.php create mode 100644 tests/data/W3C/result.json create mode 100644 tests/data/W3C/source.html diff --git a/tests/MicrodataParserTest.php b/tests/MicrodataParserTest.php new file mode 100644 index 0000000..8939d53 --- /dev/null +++ b/tests/MicrodataParserTest.php @@ -0,0 +1,62 @@ +loadHTML($data['source']); + $dom->documentURI = $data['uri']; + + $parser = new MicrodataParser($dom); + + $result = $parser->extractMicrodata(); + + $this->assertEquals($data['result'], $result); + } + + /** + * @todo Provide more test data + */ + public function data() + { + return [ + // https://www.w3.org/TR/microdata/#ex-jsonconv + 'W3C Example' => [ + $this->getTestData('W3C', 'source.html', 'result.json') + ], + ]; + } + + private function getTestData($folderName, $sourceName, $resultName) + { + $folderPath = __DIR__.'/data/'.$folderName.'/'; + + $source = file_get_contents($folderPath . $sourceName); + $result = file_get_contents($folderPath . $resultName); + + $uri = ''; + // Set $uri if URI specified in test data + if (preg_match('//', $source, $matches)) { + $uri = $matches[1]; + } + + return [ + 'uri' => $uri, + 'source' => $source, + 'result' => json_decode($result), + ]; + } +} diff --git a/tests/data/W3C/result.json b/tests/data/W3C/result.json new file mode 100644 index 0000000..91272d0 --- /dev/null +++ b/tests/data/W3C/result.json @@ -0,0 +1,44 @@ +{ + "items": [ + { + "type": [ "https://schema.org/BlogPosting" ], + "properties": { + "headline": [ "Progress report" ], + "datePublished": [ "2013-08-29" ], + "url": [ "http://blog.example.com/progress-report?comments=0" ], + "comment": [ + { + "type": [ "https://schema.org/Comment" ], + "properties": { + "url": [ "http://blog.example.com/progress-report#c1" ], + "creator": [ + { + "type": [ "https://schema.org/Person" ], + "properties": { + "name": [ "Greg" ] + } + } + ], + "dateCreated": [ "2013-08-29" ] + } + }, + { + "type": [ "https://schema.org/Comment" ], + "properties": { + "url": [ "http://blog.example.com/progress-report#c2" ], + "creator": [ + { + "type": [ "https://schema.org/Person" ], + "properties": { + "name": [ "Charlotte" ] + } + } + ], + "dateCreated": [ "2013-08-29" ] + } + } + ] + } + } + ] +} diff --git a/tests/data/W3C/source.html b/tests/data/W3C/source.html new file mode 100644 index 0000000..35b5393 --- /dev/null +++ b/tests/data/W3C/source.html @@ -0,0 +1,35 @@ + + +My Blog +
+
+

Progress report

+

+ +
+

All in all, he's doing well with his swim lessons. The biggest thing was he had trouble + putting his head in, but we got it down.

+
+

Comments

+
+ +
+

Posted by: + Greg +

+

+
+

Ha!

+
+
+ +
+

Posted by: + Charlotte +

+

+
+

When you say "we got it down"...

+
+
+