Skip to content

Commit 3dc950d

Browse files
committed
Fix Parser.
1 parent e7b7241 commit 3dc950d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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)