Skip to content

Commit 9db2bfa

Browse files
committed
Merge pull request #65 from jeremykendall/fix/54-no-scheme-should-return-null-for-scheme
The rest of PR 64
2 parents e102cba + 241141a commit 9db2bfa

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/Pdp/Exception/SeriouslyMalformedUrlException.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
class SeriouslyMalformedUrlException extends \InvalidArgumentException implements PdpException
2222
{
2323
/**
24-
* Public constructor
24+
* Public constructor.
2525
*
26-
* @param string $malformedUrl URL that caused pdp_parse_url() to return false
27-
* @param int $code The Exception code
28-
* @param \Exception $previous The previous exception used for the exception chaining
26+
* @param string $malformedUrl URL that caused pdp_parse_url() to return false
27+
* @param int $code The Exception code
28+
* @param \Exception $previous The previous exception used for the exception chaining
2929
*/
30-
public function __construct($malformedUrl = "", $code = 0, $previous = null)
30+
public function __construct($malformedUrl = '', $code = 0, $previous = null)
3131
{
3232
$message = sprintf('"%s" is one seriously malformed url.', $malformedUrl);
3333
parent::__construct($message, $code, $previous);

src/Pdp/Parser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace Pdp;
1212

13+
use Pdp\Exception\SeriouslyMalformedUrlException;
1314
use Pdp\Uri\Url;
1415
use Pdp\Uri\Url\Host;
1516

@@ -63,6 +64,7 @@ public function __construct(PublicSuffixList $publicSuffixList)
6364
*/
6465
public function parseUrl($url)
6566
{
67+
$rawUrl = $url;
6668
$elem = array(
6769
'scheme' => null,
6870
'user' => null,
@@ -83,7 +85,7 @@ public function parseUrl($url)
8385
$parts = pdp_parse_url($url);
8486

8587
if ($parts === false) {
86-
throw new \InvalidArgumentException(sprintf('Invalid url %s', $url));
88+
throw new SeriouslyMalformedUrlException($rawUrl);
8789
}
8890

8991
if ($parts['scheme'] === 'php-lt-5.4.7-hack') {

tests/src/Pdp/ParserTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,36 @@ public function testIsSuffixValidTrue()
4949
* @covers ::pdp_parse_url
5050
*/
5151
public function testParseBadUrlThrowsInvalidArgumentException()
52+
{
53+
$url = 'http:///example.com';
54+
55+
$this->setExpectedException(
56+
'Pdp\Exception\SeriouslyMalformedUrlException',
57+
sprintf('"%s" is one seriously malformed url.', $url)
58+
);
59+
60+
$this->parser->parseUrl($url);
61+
}
62+
63+
/**
64+
* If an empty string is passed to the parser then the hacky scheme from
65+
* issue 49 should not appear in the Exception message.
66+
*
67+
* @group issue54
68+
*
69+
* @see https://github.com/jeremykendall/php-domain-parser/issues/54
70+
*
71+
* @covers Pdp\Parser::parseUrl()
72+
* @covers ::pdp_parse_url
73+
*/
74+
public function testParseEmptyStringThrowsInvalidArgumentExceptionWithoutWackySchemeInMessage()
5275
{
5376
$this->setExpectedException(
54-
'\InvalidArgumentException',
55-
'Invalid url http:///example.com'
77+
'Pdp\Exception\SeriouslyMalformedUrlException',
78+
'"" is one seriously malformed url.'
5679
);
5780

58-
$this->parser->parseUrl('http:///example.com');
81+
$this->parser->parseUrl('');
5982
}
6083

6184
/**

0 commit comments

Comments
 (0)