From 782218f781343969b797ec00b7e5f397cb6a10ed Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 21 May 2020 00:24:46 -0400 Subject: [PATCH] Add tests for Network\Probe::getFeedLink --- tests/src/Network/ProbeTest.php | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/src/Network/ProbeTest.php diff --git a/tests/src/Network/ProbeTest.php b/tests/src/Network/ProbeTest.php new file mode 100644 index 000000000..2dfc0e2ac --- /dev/null +++ b/tests/src/Network/ProbeTest.php @@ -0,0 +1,108 @@ + + + + Example Blog + + + + +

Hello World!

+ +'; + + const TEMPLATEBASE = ' + + + + Example Blog + + + + + +

Hello World!

+ +'; + + const EXPECTED = [ + 'https://example.org/path/to/blog/index.php' => [ + 'index.xml' => 'https://example.org/path/to/blog/index.xml', + './index.xml' => 'https://example.org/path/to/blog/index.xml', + '../index.xml' => 'https://example.org/path/to/index.xml', + '/index.xml' => 'https://example.org/index.xml', + '//example.com/index.xml' => 'https://example.com/index.xml', + ], + 'https://example.org/path/to/blog/' => [ + 'index.xml' => 'https://example.org/path/to/blog/index.xml', + './index.xml' => 'https://example.org/path/to/blog/index.xml', + '../index.xml' => 'https://example.org/path/to/index.xml', + '/index.xml' => 'https://example.org/index.xml', + '//example.com/index.xml' => 'https://example.com/index.xml', + ], + 'https://example.org/blog/' => [ + 'index.xml' => 'https://example.org/blog/index.xml', + './index.xml' => 'https://example.org/blog/index.xml', + '../index.xml' => 'https://example.org/index.xml', + '/index.xml' => 'https://example.org/index.xml', + '//example.com/index.xml' => 'https://example.com/index.xml', + ], + 'https://example.org' => [ + 'index.xml' => 'https://example.org/index.xml', + './index.xml' => 'https://example.org/index.xml', + '../index.xml' => 'https://example.org/index.xml', + '/index.xml' => 'https://example.org/index.xml', + '//example.com/index.xml' => 'https://example.com/index.xml', + ], + ]; + + private function replaceMacros($template, $vars) + { + foreach ($vars as $var => $value) { + $template = str_replace('{{' . $var . '}}', $value, $template); + } + + return $template; + } + + /** + * @small + */ + public function testGetFeedLinkNoBase() + { + foreach (self::EXPECTED as $url => $hrefs) { + foreach ($hrefs as $href => $expected) { + $body = $this->replaceMacros(self::TEMPLATENOBASE, ['$link' => $href]); + + $feedLink = Probe::getFeedLink($url, $body); + + $this->assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href); + } + } + } + + /** + * @small + */ + public function testGetFeedLinkBase() + { + foreach (self::EXPECTED as $url => $hrefs) { + foreach ($hrefs as $href => $expected) { + $body = $this->replaceMacros(self::TEMPLATEBASE, ['$url' => $url, '$link' => $href]); + + $feedLink = Probe::getFeedLink('http://example.com', $body); + + $this->assertEquals($expected, $feedLink, 'base url = ' . $url . ' | href = ' . $href); + } + } + } +}