diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ReadabilityTest.php | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/test/ReadabilityTest.php b/test/ReadabilityTest.php index 842d259..dafd540 100644 --- a/test/ReadabilityTest.php +++ b/test/ReadabilityTest.php @@ -40,6 +40,19 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase $expected_no_whitespace = preg_replace($from, $to, $testPage->getExpectedHTML()); $readability_no_whitespace = preg_replace($from, $to, $readability->getContent()); + if (getenv('output-changes') && $expected_no_whitespace !== $readability_no_whitespace) { + @mkdir(__DIR__.'/changed/'.$testPage->getSlug()); + $new_expected = __DIR__.'/changed/'.$testPage->getSlug().'/expected.html'; + $old_expected = __DIR__.'/test-pages/'.$testPage->getSlug().'/expected.html'; + //file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/readability.html', $readability_no_whitespace); + //file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/expected-current.html', $expected_no_whitespace); + file_put_contents($new_expected, $readability->getContent()); + if (getenv('output-diff')) { + file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/diff-expected.txt', shell_exec(sprintf('diff -u -d %s %s', $old_expected, $new_expected))); + } + + } + $this->assertSame($expected_no_whitespace, $readability_no_whitespace, 'Parsed text does not match the expected one.'); //$this->assertSame($testPage->getExpectedHTML(), $readability->getContent(), 'Parsed text does not match the expected one.'); @@ -68,6 +81,26 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase $readability = new Readability($configuration); $readability->parse($testPage->getSourceHTML()); + $metadata = [ + 'Author' => $readability->getAuthor(), + 'Direction' => $readability->getDirection(), + 'Excerpt' => $readability->getExcerpt(), + 'Image' => $readability->getImage(), + 'Title' => $readability->getTitle(), + 'SiteName' => $readability->getSiteName() + ]; + + if (getenv('output-changes') && (array)$testPage->getExpectedMetadata() !== $metadata) { + @mkdir(__DIR__.'/changed/'.$testPage->getSlug()); + $new_expected = __DIR__.'/changed/'.$testPage->getSlug().'/expected-metadata.json'; + $old_expected = __DIR__.'/test-pages/'.$testPage->getSlug().'/expected-metadata.json'; + //file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/expected-metadata-current.json', json_encode($testPage->getExpectedMetadata(), JSON_PRETTY_PRINT)); + file_put_contents($new_expected, json_encode((object)$metadata, JSON_PRETTY_PRINT)); + if (getenv('output-diff')) { + file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/diff-expected-metadata.txt', shell_exec(sprintf('diff -u -d %s %s', $old_expected, $new_expected))); + } + } + $this->assertSame($testPage->getExpectedMetadata()->Author, $readability->getAuthor(), 'Parsed Author does not match expected value.'); $this->assertSame($testPage->getExpectedMetadata()->Direction, $readability->getDirection(), 'Parsed Direction does not match expected value.'); $this->assertSame($testPage->getExpectedMetadata()->Excerpt, $readability->getExcerpt(), 'Parsed Excerpt does not match expected value.'); @@ -95,6 +128,17 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase $readability = new Readability($configuration); $readability->parse($testPage->getSourceHTML()); + if (getenv('output-changes') && $testPage->getExpectedImages() !== array_values($readability->getImages())) { + @mkdir(__DIR__.'/changed/'.$testPage->getSlug()); + $new_expected = __DIR__.'/changed/'.$testPage->getSlug().'/expected-images.json'; + $old_expected = __DIR__.'/test-pages/'.$testPage->getSlug().'/expected-images.json'; + //file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/expected-images-current.json', json_encode($testPage->getExpectedImages(), JSON_PRETTY_PRINT)); + file_put_contents($new_expected, json_encode(array_values($readability->getImages()), JSON_PRETTY_PRINT)); + if (getenv('output-diff')) { + file_put_contents(__DIR__.'/changed/'.$testPage->getSlug().'/diff-expected-images.txt', shell_exec(sprintf('diff -u -d %s %s', $old_expected, $new_expected))); + } + } + $this->assertSame($testPage->getExpectedImages(), array_values($readability->getImages())); } @@ -113,9 +157,9 @@ class ReadabilityTest extends \PHPUnit\Framework\TestCase $slug = $testPage; $source = file_get_contents($testCasePath . 'source.html'); - $expectedHTML = file_get_contents($testCasePath . 'expected.html'); - $expectedImages = json_decode(file_get_contents($testCasePath . 'expected-images.json'), true); - $expectedMetadata = json_decode(file_get_contents($testCasePath . 'expected-metadata.json')); + $expectedHTML = file_exists($testCasePath . 'expected.html') ? file_get_contents($testCasePath . 'expected.html') : ''; + $expectedImages = file_exists($testCasePath . 'expected-images.json') ? json_decode(file_get_contents($testCasePath . 'expected-images.json'), true) : []; + $expectedMetadata = file_exists($testCasePath . 'expected-metadata.json') ? json_decode(file_get_contents($testCasePath . 'expected-metadata.json')) : (object)[]; $configuration = file_exists($testCasePath . 'config.json') ? json_decode(file_get_contents($testCasePath . 'config.json'), true) : []; yield $testPage => [new TestPage($slug, $configuration, $source, $expectedHTML, $expectedImages, $expectedMetadata)]; |