Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unparse url function #2

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4ac2840
add PHPUnit test for unparse_url function
Jan 16, 2025
88cc08d
add Urls.php in src
Jan 16, 2025
5a59dc4
Fix unparse_url to epmty string to null in line 13 and 14
Jan 17, 2025
6873c73
Fix TypeError in unparse_url caused by null values from query, line 23
Jan 17, 2025
99544ba
Add validation to unparse_url function to ensure input is array
Jan 20, 2025
1d567b6
Create boilerplate PHPUnit test class for unparse_url
Jan 21, 2025
2fbdce0
Add test case for empty string
Jan 21, 2025
d9290c2
Implement basic unparse_url to handle empty string
Jan 21, 2025
5b2ec82
Fix namespace mismatch in UrlsTest file
Jan 21, 2025
cce4b0f
Fix class name
Jan 21, 2025
823ae2c
Add parsing logic
Jan 21, 2025
877c16a
Add test for simple string
Jan 21, 2025
db2e81c
Add test for simple URL
Jan 22, 2025
bbe7b80
Add test for full URL
Jan 22, 2025
b7f93de
Add test for incomplete URL
Jan 22, 2025
800ac30
Add test for SSH URL
Jan 22, 2025
f9d297b
Add test for malformed URL
Jan 22, 2025
6635c8d
Add test for Url with empty user and password
Jan 22, 2025
8a930f1
Add test for Url with empty user
Jan 22, 2025
ebd8d5b
Implement unparse_url to handle path only URL
Jan 22, 2025
3f1d7af
Implement unparse_url to handle path only URL
Jan 22, 2025
c7170f6
Add scheme and host to unparse_url
Jan 22, 2025
fd3dc26
Add test for URL with host
Jan 22, 2025
a1cbaa1
Add port reconstruction
Jan 22, 2025
23f7d92
Add user and pass to URL
Jan 22, 2025
90fe1d5
Add query and fragment to reconstruction URL
Jan 22, 2025
92f936f
Refactor PHPUnit test class for unparse_url
Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Urls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Missing;

class Urls
{
public static function unparse_url(array $parsed): string
{
$scheme = isset($parsed['scheme']) ? $parsed['scheme'] . "://" : "";
$host = isset($parsed['host']) ? $parsed['host'] : "";
$path = $parsed['path'] ?? '';
$port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
$user = $parsed['user'] ?? '';
$pass = isset($parsed['pass']) ? ':' . $parsed['pass'] : '';
$auth = ($user || $pass) ? "$user$pass@" : '';
$query = isset($parsed['query']) ? "?" . $parsed['query'] : "";
$fragment = isset($parsed['fragment']) ? "#" . $parsed['fragment'] : "";

return "$scheme$auth$host$port$path$query$fragment";

}
}
33 changes: 33 additions & 0 deletions tests/urls_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Missing\tests;

use \PHPUnit\Framework\TestCase;
use Missing\Urls;

class Urls_test extends TestCase
{
public function test_unparse_url()
{
$urls = [
'',
'foo',
'http://www.google.com/',
'http://www.google.com:8080/',
'http://u:p@foo:1/path/path?q#frag',
'http://u:p@foo:1/path/path?#',
'ssh://root@host',
'://:@:1/?#',
'http://:@foo:1/path/path?#'
];

foreach ($urls as $url) {
$parsed = parse_url($url);
$rebuildUrl = Urls::unparse_url($parsed);
$parsedRebuilt = parse_url($rebuildUrl);

$this->assertEquals($parsed, $parsedRebuilt, "Failed to reconstruct URL: '{$url}'");

}
}
}