From 112faa936d020424eeb8d8b799f39fdc02070d37 Mon Sep 17 00:00:00 2001 From: JanPetterMG Date: Mon, 4 Apr 2016 16:45:27 +0200 Subject: [PATCH] Bug fixes + more tests --- composer.json | 8 +++- src/SitemapParser.php | 7 +--- .../Exceptions/SitemapParserException.php | 0 tests/DownloadTest.php | 3 ++ tests/ExceptionEncodingTest.php | 18 +++++++++ tests/ExceptionMBStringTest.php | 18 +++++++++ tests/ExceptionSimpleXMLTest.php | 18 +++++++++ tests/InvalidURLTest.php | 38 +++++++++++++++++++ 8 files changed, 102 insertions(+), 8 deletions(-) rename src/{ => SitemapParser}/Exceptions/SitemapParserException.php (100%) create mode 100644 tests/ExceptionEncodingTest.php create mode 100644 tests/ExceptionMBStringTest.php create mode 100644 tests/ExceptionSimpleXMLTest.php create mode 100644 tests/InvalidURLTest.php diff --git a/composer.json b/composer.json index 780304d..cfcb66c 100644 --- a/composer.json +++ b/composer.json @@ -36,8 +36,12 @@ }, "autoload": { "psr-4": { - "vipnytt\\": "src/", - "vipnytt\\test\\": "test/" + "vipnytt\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "vipnytt\\SitemapParser\\Tests\\": "tests/" } } } \ No newline at end of file diff --git a/src/SitemapParser.php b/src/SitemapParser.php index af35268..3f19290 100644 --- a/src/SitemapParser.php +++ b/src/SitemapParser.php @@ -146,9 +146,7 @@ public function parse($url, $urlContent = null) $response = gzdecode($response); } $sitemapJson = $this->generateXMLObject($response); - if ($sitemapJson === false && empty($response)) { - throw new SitemapParserException("The XML found on the given URL doesn't appear to be valid according to simplexml_load_string/libxml"); - } elseif ($sitemapJson === false) { + if ($sitemapJson === false) { $this->parseString($response); return; } @@ -188,9 +186,6 @@ protected function getContent() } $client = new GuzzleHttp\Client(); $res = $client->request('GET', $this->currentURL, $this->config['guzzle']); - if ($res->getStatusCode() < 200 || $res->getStatusCode() >= 400) { - throw new SitemapParserException('The server responds with a bad status code: ' . $res->getStatusCode()); - } return $res->getBody(); } catch (GuzzleHttp\Exception\TransferException $e) { throw new SitemapParserException($e->getMessage()); diff --git a/src/Exceptions/SitemapParserException.php b/src/SitemapParser/Exceptions/SitemapParserException.php similarity index 100% rename from src/Exceptions/SitemapParserException.php rename to src/SitemapParser/Exceptions/SitemapParserException.php diff --git a/tests/DownloadTest.php b/tests/DownloadTest.php index a8be3b4..0a5ebab 100644 --- a/tests/DownloadTest.php +++ b/tests/DownloadTest.php @@ -44,6 +44,9 @@ function generateDataForTest() ], [ 'http://php.net/sitemap.xml', + ], + [ + 'https://www.yahoo.com/news/sitemaps/news-sitemap_index_US_en-US.xml.gz', ] ]; } diff --git a/tests/ExceptionEncodingTest.php b/tests/ExceptionEncodingTest.php new file mode 100644 index 0000000..cca2809 --- /dev/null +++ b/tests/ExceptionEncodingTest.php @@ -0,0 +1,18 @@ +expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException'); + new SitemapParser('SitemapParser'); + } + } +} diff --git a/tests/ExceptionMBStringTest.php b/tests/ExceptionMBStringTest.php new file mode 100644 index 0000000..eaa1253 --- /dev/null +++ b/tests/ExceptionMBStringTest.php @@ -0,0 +1,18 @@ +expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException'); + new SitemapParser('SitemapParser'); + } + } +} diff --git a/tests/ExceptionSimpleXMLTest.php b/tests/ExceptionSimpleXMLTest.php new file mode 100644 index 0000000..62e5878 --- /dev/null +++ b/tests/ExceptionSimpleXMLTest.php @@ -0,0 +1,18 @@ +expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException'); + new SitemapParser('SitemapParser'); + } + } +} diff --git a/tests/InvalidURLTest.php b/tests/InvalidURLTest.php new file mode 100644 index 0000000..241dafb --- /dev/null +++ b/tests/InvalidURLTest.php @@ -0,0 +1,38 @@ +expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException'); + $parser = new SitemapParser('SitemapParser'); + $this->assertInstanceOf('vipnytt\SitemapParser', $parser); + $parser->parse($url); + } + + /** + * Generate test data + * @return array + */ + public function generateDataForTest() + { + return [ + [ + 'htt://www.example.c/', + ], + [ + 'http:/www.example.com/', + ], + [ + 'https//www.example.com/', + ] + ]; + } +}