Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Dec 13, 2024
1 parent 7efe633 commit f086fb2
Showing 1 changed file with 94 additions and 13 deletions.
107 changes: 94 additions & 13 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,38 @@

class UrlTest extends TestCase {

protected function setUp(): void {
// Simulate server variables for getCurrentUrl tests
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/test-path';
$_SERVER['HTTPS'] = 'on';
}

protected function tearDown(): void {
// Clean up server variables
unset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $_SERVER['HTTPS'] );
}

public function testUrlParsing() {
$url = new Url( 'https://example.com/path?param=value#section' );
$url = new Url( 'https://user:pass@example.com:8080/path?param=value#section' );

$this->assertEquals( 'https', $url->scheme );
$this->assertEquals( 'example.com', $url->host );
$this->assertEquals( 'user', $url->user );
$this->assertEquals( 'pass', $url->pass );
$this->assertEquals( '8080', $url->port );
$this->assertEquals( '/path', $url->path );
$this->assertEquals( 'param=value', $url->query );
$this->assertEquals( 'section', $url->fragment );
}

public function testEmptyUrlDefaultsToCurrentUrl() {
$url = new Url();
$this->assertEquals( 'https://example.com/test-path', $url->toString() );
}

public function testQueryParameterManipulation() {
$url = new Url( 'https://example.com/path?param=value' );
$url = new Url( 'https://example.com/path?param=value&existing=test' );

// Test adding query parameter
$url->addQueryVar( 'new_param', 'value' );
Expand All @@ -29,28 +49,43 @@ public function testQueryParameterManipulation() {
$this->assertNull( $url->getQueryVar( 'param' ) );

// Test getting all query parameters
$expected = array( 'new_param' => 'value' );
$expected = array(
'existing' => 'test',
'new_param' => 'value',
);
$this->assertEquals( $expected, $url->getQueryVars() );

// Test array query parameters
$url->addQueryVar( 'array_param', array( 'one', 'two' ) );
$this->assertEquals( array( 'one', 'two' ), $url->getQueryVar( 'array_param' ) );
}

public function testStaticHelpers() {
// Test stripping query string
$urlString = 'https://example.com/path?param=value';
$this->assertEquals( 'https://example.com/path', Url::stripQueryString( $urlString ) );
$urlString = 'https://example.com/path?param=value#fragment';
$this->assertEquals( 'https://example.com/path#fragment', Url::stripQueryString( $urlString ) );

// Test building URL from parts
// Test building URL from parts with all components
$urlParts = array(
'scheme' => 'https',
'host' => 'example.com',
'path' => '/path',
'query' => 'param=value',
'scheme' => 'https',
'user' => 'username',
'pass' => 'password',
'host' => 'example.com',
'port' => '8080',
'path' => '/path',
'query' => 'param=value',
'fragment' => 'section',
);
$expected = 'https://example.com/path?param=value';
$expected = 'https://username:password@example.com:8080/path?param=value#section';
$this->assertEquals( $expected, Url::buildUrl( $urlParts ) );

// Test building URL with minimal parts
$minimalParts = array( 'host' => 'example.com' );
$this->assertEquals( 'example.com', Url::buildUrl( $minimalParts ) );
}

public function testPathManipulation() {
$url = new Url( 'https://example.com/blog/2023/post-title' );
$url = new Url( 'https://example.com/blog/2023/post-title/' );

// Test getting all segments
$expectedSegments = array( 'blog', '2023', 'post-title' );
Expand All @@ -60,10 +95,18 @@ public function testPathManipulation() {
$this->assertEquals( 'blog', $url->getSegment( 0 ) );
$this->assertEquals( '2023', $url->getSegment( 1 ) );
$this->assertEquals( 'post-title', $url->getSegment( 2 ) );
$this->assertNull( $url->getSegment( 5 ) ); // Non-existent segment

// Test trailing slash detection
$this->assertTrue( $url->hasTrailingSlash() );

// Test URL without trailing slash
$url2 = new Url( 'https://example.com/blog/2023/post-title' );
$this->assertFalse( $url2->hasTrailingSlash() );
}

public function testUrlOutput() {
$urlString = 'https://example.com/path?param=value';
$urlString = 'https://example.com/path?param=value#fragment';
$url = new Url( $urlString );

// Test toString() method
Expand All @@ -79,5 +122,43 @@ public function testUrlOutput() {
$this->assertEquals( 'example.com', $urlParts['host'] );
$this->assertEquals( '/path', $urlParts['path'] );
$this->assertEquals( 'param=value', $urlParts['query'] );
$this->assertEquals( 'fragment', $urlParts['fragment'] );
}

public function testGetCurrentScheme() {
// Test HTTPS via server variable
$_SERVER['HTTPS'] = 'on';
$this->assertEquals( 'https', Url::getCurrentScheme() );

// Test HTTPS = 1
$_SERVER['HTTPS'] = '1';
$this->assertEquals( 'https', Url::getCurrentScheme() );

// Test port 443
unset( $_SERVER['HTTPS'] );
$_SERVER['SERVER_PORT'] = '443';
$this->assertEquals( 'https', Url::getCurrentScheme() );

// Test forwarded proto
unset( $_SERVER['SERVER_PORT'] );
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$this->assertEquals( 'https', Url::getCurrentScheme() );

// Test default to http
unset( $_SERVER['HTTP_X_FORWARDED_PROTO'] );
$this->assertEquals( 'http', Url::getCurrentScheme() );
}

public function testBuildPath() {
// Test with segments
$segments = array( 'blog', '2023', 'post-title' );
$this->assertEquals( '/blog/2023/post-title', Url::buildPath( $segments ) );

// Test with trailing slash
$this->assertEquals( '/blog/2023/post-title/', Url::buildPath( $segments, true ) );

// Test empty segments
$this->assertEquals( '', Url::buildPath( array() ) );
$this->assertEquals( '/', Url::buildPath( array(), true ) );
}
}

0 comments on commit f086fb2

Please sign in to comment.