Skip to content

Commit

Permalink
Bug fixes + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanPetterMG committed Apr 4, 2016
1 parent 821e1ac commit 112faa9
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 8 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@
},
"autoload": {
"psr-4": {
"vipnytt\\": "src/",
"vipnytt\\test\\": "test/"
"vipnytt\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"vipnytt\\SitemapParser\\Tests\\": "tests/"
}
}
}
7 changes: 1 addition & 6 deletions src/SitemapParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/DownloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ExceptionEncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace vipnytt\SitemapParser\Tests;

use vipnytt\SitemapParser;

class ExceptionEncodingTest extends \PHPUnit_Framework_TestCase
{
/**
* Test if exception is thrown when extension `simpleXML` is not loaded
*/
public function testExceptionEncoding()
{
if (!mb_internal_encoding('UTF-8')) {
$this->expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException');
new SitemapParser('SitemapParser');
}
}
}
18 changes: 18 additions & 0 deletions tests/ExceptionMBStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace vipnytt\SitemapParser\Tests;

use vipnytt\SitemapParser;

class ExceptionMBStringTest extends \PHPUnit_Framework_TestCase
{
/**
* Test if exception is thrown when extension `simpleXML` is not loaded
*/
public function testExceptionMBString()
{
if (!extension_loaded('mbstring')) {
$this->expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException');
new SitemapParser('SitemapParser');
}
}
}
18 changes: 18 additions & 0 deletions tests/ExceptionSimpleXMLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace vipnytt\SitemapParser\Tests;

use vipnytt\SitemapParser;

class ExceptionSimpleXMLTest extends \PHPUnit_Framework_TestCase
{
/**
* Test if exception is thrown when extension `simpleXML` is not loaded
*/
public function testExceptionSimpleXML()
{
if (!extension_loaded('simplexml')) {
$this->expectException('\vipnytt\SitemapParser\Exceptions\SitemapParserException');
new SitemapParser('SitemapParser');
}
}
}
38 changes: 38 additions & 0 deletions tests/InvalidURLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace vipnytt\SitemapParser\Tests;

use vipnytt\SitemapParser;

class InvalidURLTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider generateDataForTest
* @param string $url URL
*/
public function testInvalidURL($url)
{
$this->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/',
]
];
}
}

0 comments on commit 112faa9

Please sign in to comment.