Skip to content

Commit 8e4b223

Browse files
committed
ext/uri: increased test coverage
1 parent 48d2b45 commit 8e4b223

44 files changed

Lines changed: 1221 additions & 12 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Test HTTP stream URI parsing - error - invalid URI
3+
--INI--
4+
allow_url_fopen=1
5+
--FILE--
6+
<?php
7+
8+
// hits ext/uri parsing and fails
9+
// before connection is attempted
10+
var_dump(@fopen("http://", "r"));
11+
12+
?>
13+
--EXPECT--
14+
bool(false)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Test phpinfo() - URI module information
3+
--FILE--
4+
<?php
5+
6+
ob_start();
7+
phpinfo(INFO_MODULES);
8+
$info = ob_get_clean();
9+
10+
var_dump(str_contains($info, "URI support => active"));
11+
var_dump(
12+
str_contains($info, "uriparser bundled version") ||
13+
(
14+
str_contains($info, "uriparser compiled version")
15+
&&
16+
str_contains($info, "uriparser loaded version")
17+
)
18+
);
19+
20+
?>
21+
--EXPECT--
22+
bool(true)
23+
bool(true)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder::build() - error - invalid internal fragment
3+
--EXTENSIONS--
4+
reflection
5+
--FILE--
6+
<?php
7+
8+
$builder = new Uri\Rfc3986\UriBuilder();
9+
new ReflectionProperty($builder, 'fragment')->setValue($builder, '<');
10+
11+
try {
12+
$builder->build();
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), "\n";
15+
}
16+
17+
?>
18+
--EXPECT--
19+
Uri\InvalidUriException: The specified fragment is malformed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder::build() - error - invalid internal host
3+
--EXTENSIONS--
4+
reflection
5+
--FILE--
6+
<?php
7+
8+
$builder = new Uri\Rfc3986\UriBuilder();
9+
new ReflectionProperty($builder, 'host')->setValue($builder, '[');
10+
11+
try {
12+
$builder->build();
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), "\n";
15+
}
16+
17+
?>
18+
--EXPECT--
19+
Uri\InvalidUriException: The specified host is malformed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder::build() - error - invalid internal query
3+
--EXTENSIONS--
4+
reflection
5+
--FILE--
6+
<?php
7+
8+
$builder = new Uri\Rfc3986\UriBuilder();
9+
new ReflectionProperty($builder, 'query')->setValue($builder, '<');
10+
11+
try {
12+
$builder->build();
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), "\n";
15+
}
16+
17+
?>
18+
--EXPECT--
19+
Uri\InvalidUriException: The specified query is malformed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder::build() - error - invalid internal scheme
3+
--EXTENSIONS--
4+
reflection
5+
--FILE--
6+
<?php
7+
8+
$builder = new Uri\Rfc3986\UriBuilder();
9+
new ReflectionProperty($builder, 'scheme')->setValue($builder, ':');
10+
11+
try {
12+
$builder->build();
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), "\n";
15+
}
16+
17+
?>
18+
--EXPECT--
19+
Uri\InvalidUriException: The specified scheme is malformed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder::build() - success - referenced component
3+
--FILE--
4+
<?php
5+
6+
$scope = "\0Uri\\Rfc3986\\UriBuilder\0";
7+
8+
$serialised = serialize(new Uri\Rfc3986\UriBuilder());
9+
10+
$serialised = str_replace(
11+
"s:30:\"{$scope}scheme\";N;",
12+
"s:30:\"{$scope}scheme\";s:5:\"https\";",
13+
$serialised,
14+
);
15+
16+
$serialised = str_replace(
17+
"s:28:\"{$scope}host\";N;",
18+
"s:28:\"{$scope}host\";s:11:\"example.com\";",
19+
$serialised,
20+
);
21+
22+
$serialised = str_replace(
23+
"s:28:\"{$scope}path\";s:0:\"\";",
24+
"s:28:\"{$scope}path\";s:5:\"/path\";",
25+
$serialised,
26+
);
27+
28+
// make fragment share path value
29+
$serialised = str_replace(
30+
"s:32:\"{$scope}fragment\";N;",
31+
"s:32:\"{$scope}fragment\";R:6;",
32+
$serialised,
33+
);
34+
35+
$uri = unserialize($serialised)->build();
36+
37+
var_dump($uri->toRawString());
38+
var_dump($uri);
39+
var_dump($uri->equals(new Uri\Rfc3986\Uri($uri->toRawString())));
40+
41+
?>
42+
--EXPECTF--
43+
string(30) "https://example.com/path#/path"
44+
object(Uri\Rfc3986\Uri)#%d (%d) {
45+
["scheme"]=>
46+
string(5) "https"
47+
["username"]=>
48+
NULL
49+
["password"]=>
50+
NULL
51+
["host"]=>
52+
string(11) "example.com"
53+
["port"]=>
54+
NULL
55+
["path"]=>
56+
string(5) "/path"
57+
["query"]=>
58+
NULL
59+
["fragment"]=>
60+
string(5) "/path"
61+
}
62+
bool(true)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder::setHost() - success - empty string
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\Rfc3986\UriBuilder();
7+
$builder->setHost('');
8+
$uri = $builder->build();
9+
10+
var_dump($uri->toRawString());
11+
var_dump($uri);
12+
var_dump($uri->equals(new Uri\Rfc3986\Uri($uri->toRawString())));
13+
14+
?>
15+
--EXPECTF--
16+
string(2) "//"
17+
object(Uri\Rfc3986\Uri)#%d (%d) {
18+
["scheme"]=>
19+
NULL
20+
["username"]=>
21+
NULL
22+
["password"]=>
23+
NULL
24+
["host"]=>
25+
string(0) ""
26+
["port"]=>
27+
NULL
28+
["path"]=>
29+
string(0) ""
30+
["query"]=>
31+
NULL
32+
["fragment"]=>
33+
NULL
34+
}
35+
bool(true)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test Uri\Rfc3986\Uri component retrieval - userinfo - without password separator
3+
--FILE--
4+
<?php
5+
6+
$uri = new Uri\Rfc3986\Uri('https://user@example.com');
7+
8+
var_dump($uri->getUserInfo());
9+
var_dump($uri->getUsername());
10+
var_dump($uri->getPassword());
11+
12+
?>
13+
--EXPECT--
14+
string(4) "user"
15+
string(4) "user"
16+
string(0) ""
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test Uri\Rfc3986\Uri::withHost() - error - unsetting with port
3+
--FILE--
4+
<?php
5+
6+
$uri = new Uri\Rfc3986\Uri('https://example.com:8080');
7+
8+
try {
9+
$uri->withHost(null);
10+
} catch (Throwable $e) {
11+
echo $e::class, ': ', $e->getMessage(), "\n";
12+
}
13+
14+
?>
15+
--EXPECT--
16+
Uri\InvalidUriException: Cannot remove the host from a URI that has a port

0 commit comments

Comments
 (0)