summaryrefslogtreecommitdiff
path: root/test/ReadabilityTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'test/ReadabilityTest.php')
-rw-r--r--test/ReadabilityTest.php80
1 files changed, 77 insertions, 3 deletions
diff --git a/test/ReadabilityTest.php b/test/ReadabilityTest.php
index ec96ef4..d019192 100644
--- a/test/ReadabilityTest.php
+++ b/test/ReadabilityTest.php
@@ -2,11 +2,85 @@
namespace andreskrey\Readability\Test;
+
+use andreskrey\Readability\Configuration;
+use andreskrey\Readability\Readability;
+
class ReadabilityTest extends \PHPUnit_Framework_TestCase
{
- public function testDummy()
+ /**
+ * @dataProvider getSamplePages
+ */
+ public function testReadabilityParsesHTML($html, $expectedResult, $expectedMetadata, $config, $expectedImages)
+ {
+ $options = ['originalURL' => 'http://fakehost/test/test.html',
+ 'fixRelativeURLs' => true,
+ 'substituteEntities' => true,
+ ];
+
+ if ($config) {
+ $options = array_merge($options, $config);
+ }
+
+ $configuration = new Configuration();
+
+ foreach($options as $key => $value){
+ $name = 'set' . $key;
+ $configuration->$name($value);
+ }
+
+ $readability = new Readability($configuration);
+ $result = $readability->parse($html);
+
+ $this->assertEquals($expectedResult, $result['html']);
+ }
+
+ /**
+ * @dataProvider getSamplePages
+ */
+ public function testHTMLParserParsesImages($html, $expectedResult, $expectedMetadata, $config, $expectedImages)
{
- //TODO
- $this->assertEquals(true, true);
+ $options = ['originalURL' => 'http://fakehost/test/test.html',
+ 'fixRelativeURLs' => true,
+ 'substituteEntities' => true,
+ ];
+
+ if ($config) {
+ $options = array_merge($options, $config);
+ }
+
+ $readability = new HTMLParser($options);
+ $result = $readability->parse($html);
+ $this->assertEquals($expectedImages, json_encode($result['images']));
+ }
+
+ public function getSamplePages()
+ {
+ $path = pathinfo(__FILE__, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . 'test-pages';
+ $testPages = scandir($path);
+ if (in_array('.DS_Store', $testPages)) {
+ unset($testPages[array_search('.DS_Store', $testPages)]);
+ }
+
+ $pages = [];
+
+ foreach (array_slice($testPages, 2) as $testPage) {
+ $source = file_get_contents($path . DIRECTORY_SEPARATOR . $testPage . DIRECTORY_SEPARATOR . 'source.html');
+ $expectedHTML = file_get_contents($path . DIRECTORY_SEPARATOR . $testPage . DIRECTORY_SEPARATOR . 'expected.html');
+ $expectedMetadata = file_get_contents($path . DIRECTORY_SEPARATOR . $testPage . DIRECTORY_SEPARATOR . 'expected-metadata.json');
+ $expectedImages = file_get_contents($path . DIRECTORY_SEPARATOR . $testPage . DIRECTORY_SEPARATOR . 'expected-images.json');
+
+ $config = null;
+ if (file_exists($path . DIRECTORY_SEPARATOR . $testPage . DIRECTORY_SEPARATOR . 'config.json')) {
+ $config = file_get_contents($path . DIRECTORY_SEPARATOR . $testPage . DIRECTORY_SEPARATOR . 'config.json');
+ if ($config) {
+ $config = json_decode($config, true);
+ }
+ }
+
+ $pages[$testPage] = [$source, $expectedHTML, $expectedMetadata, $config, $expectedImages];
+ }
+
+ return $pages;
}
}