Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Dec 13, 2024
1 parent 035ce66 commit 7f98c59
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 9 deletions.
5 changes: 3 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stopOnFailure="false">
stopOnFailure="false"
forceCoversAnnotation="true">
<testsuites>
<testsuite name="URL Handler Test Suite">
<directory>tests</directory>
Expand All @@ -20,6 +21,6 @@
<logging>
<log type="coverage-clover" target="coverage.xml"/>
<log type="coverage-html" target="coverage" lowUpperBound="50" highLowerBound="90"/>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false" showOnlySummary="true"/>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" showOnlySummary="false"/>
</logging>
</phpunit>
161 changes: 154 additions & 7 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PHPUnit\Framework\TestCase;
use wpscholar\Url;

/**
* @covers \wpscholar\Url
*/
class UrlTest extends TestCase {

protected function setUp(): void {
Expand All @@ -19,6 +22,10 @@ protected function tearDown(): void {
unset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $_SERVER['HTTPS'] );
}

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

Expand All @@ -38,6 +45,10 @@ public function testUrlParsing() {
$this->assertEquals( 'not-a-valid-url', $url->path );
}

/**
* @covers \wpscholar\Url::__get
* @covers \wpscholar\Url::__set
*/
public function testMagicMethods() {
$url = new Url( 'https://example.com/path' );

Expand All @@ -46,24 +57,39 @@ public function testMagicMethods() {
$this->assertEquals( '', $url->user ); // Non-existent component
$this->assertEquals( '', $url->nonexistent ); // Non-existent property

// Test __toString
$this->assertEquals( 'https://example.com/path', (string) $url );

// Test property modification through magic methods
// Test __set
$url->scheme = 'http';
$this->assertEquals( 'http', $url->scheme );

// Test setting non-existent property (should be ignored)
$url->nonexistent = 'value';
$this->assertEquals( '', $url->nonexistent );

// Test setting empty values
$url->query = '';
$this->assertEquals( '', $url->query );
$this->assertEquals( 'http://example.com/path', (string) $url );

$url->query = 'test=value';
$this->assertEquals( 'test=value', $url->query );
$this->assertEquals( 'http://example.com/path?test=value', (string) $url );
// Test setting null values
$url->fragment = null;
$this->assertEquals( '', $url->fragment );
}

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

/**
* @covers \wpscholar\Url::addQueryVar
* @covers \wpscholar\Url::removeQueryVar
* @covers \wpscholar\Url::getQueryVar
* @covers \wpscholar\Url::getQueryVars
*/
public function testQueryParameterManipulation() {
$url = new Url( 'https://example.com/path?param=value&existing=test' );

Expand Down Expand Up @@ -91,6 +117,10 @@ public function testQueryParameterManipulation() {
$this->assertEmpty( $url->getQueryVars() );
}

/**
* @covers \wpscholar\Url::stripQueryString
* @covers \wpscholar\Url::buildUrl
*/
public function testStaticHelpers() {
// Test stripping query string
$urlString = 'https://example.com/path?param=value#fragment';
Expand Down Expand Up @@ -122,6 +152,11 @@ public function testStaticHelpers() {
$this->assertEquals( 'user:pass@', Url::buildUrl( $authParts ) );
}

/**
* @covers \wpscholar\Url::getSegments
* @covers \wpscholar\Url::getSegment
* @covers \wpscholar\Url::hasTrailingSlash
*/
public function testPathManipulation() {
$url = new Url( 'https://example.com/blog/2023/post-title/' );

Expand All @@ -147,6 +182,10 @@ public function testPathManipulation() {
$this->assertEmpty( $url3->getSegments() );
}

/**
* @covers \wpscholar\Url::toString
* @covers \wpscholar\Url::toArray
*/
public function testUrlOutput() {
$urlString = 'https://example.com/path?param=value#fragment';
$url = new Url( $urlString );
Expand Down Expand Up @@ -175,6 +214,9 @@ public function testUrlOutput() {
$this->assertEmpty( $parts['fragment'] );
}

/**
* @covers \wpscholar\Url::getCurrentScheme
*/
public function testGetCurrentScheme() {
// Test HTTPS via server variable
$_SERVER['HTTPS'] = 'on';
Expand All @@ -199,6 +241,9 @@ public function testGetCurrentScheme() {
$this->assertEquals( 'http', Url::getCurrentScheme() );
}

/**
* @covers \wpscholar\Url::buildPath
*/
public function testBuildPath() {
// Test with segments
$segments = array( 'blog', '2023', 'post-title' );
Expand All @@ -212,6 +257,9 @@ public function testBuildPath() {
$this->assertEquals( '/', Url::buildPath( array(), true ) );
}

/**
* @covers \wpscholar\Url::getCurrentUrl
*/
public function testGetCurrentUrl() {
$expected = 'https://example.com/test-path';
$this->assertEquals( $expected, Url::getCurrentUrl() );
Expand All @@ -221,4 +269,103 @@ public function testGetCurrentUrl() {
$expected = 'http://example.com/test-path';
$this->assertEquals( $expected, Url::getCurrentUrl() );
}

/**
* @covers \wpscholar\Url::getSegment
*/
public function testGetSegment() {
$url = new Url( 'https://example.com/blog/2023/post-title' );

// Test valid segments
$this->assertEquals( 'blog', $url->getSegment( 0 ) );
$this->assertEquals( '2023', $url->getSegment( 1 ) );
$this->assertEquals( 'post-title', $url->getSegment( 2 ) );

// Test non-existent segments
$this->assertNull( $url->getSegment( -1 ) ); // Negative index
$this->assertNull( $url->getSegment( 5 ) ); // Out of bounds

// Test with empty path
$url = new Url( 'https://example.com' );
$this->assertNull( $url->getSegment( 0 ) );
}

/**
* @covers \wpscholar\Url::__toString
*/
public function testToString() {
// Test full URL
$url = new Url( 'https://user:pass@example.com:8080/path?param=value#fragment' );
$this->assertEquals(
'https://user:pass@example.com:8080/path?param=value#fragment',
(string) $url
);

// Test minimal URL
$url = new Url( 'http://example.com' );
$this->assertEquals( 'http://example.com', (string) $url );

// Test URL with empty components
$url = new Url( 'http://example.com' );
$url->query = '';
$url->fragment = '';
$this->assertEquals( 'http://example.com', (string) $url );
}

/**
* @covers \wpscholar\Url::parseUrl
*/
public function testParseUrlEdgeCases() {
// Test with malformed URL
$url = new Url( 'not-a-url' );
$this->assertEquals( 'not-a-url', $url->_url );
$this->assertEquals( '', $url->scheme );
$this->assertEquals( '', $url->host );
$this->assertEquals( 'not-a-url', $url->path );

// Test with empty URL
$url = new Url( '' );
$this->assertEquals( '', $url->_url );

// Test with only query string
$url = new Url( '?test=1' );
$this->assertEquals( '', $url->scheme );
$this->assertEquals( '', $url->host );
$this->assertEquals( '', $url->path );
$this->assertEquals( 'test=1', $url->query );
}

/**
* @covers \wpscholar\Url::getCurrentUrl
*/
public function testGetCurrentUrlEdgeCases() {
// Test with minimal server variables
unset( $_SERVER['HTTPS'] );
unset( $_SERVER['SERVER_PORT'] );
unset( $_SERVER['HTTP_X_FORWARDED_PROTO'] );
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/test';

$this->assertEquals( 'http://example.com/test', Url::getCurrentUrl() );

// Test with empty REQUEST_URI
$_SERVER['REQUEST_URI'] = '';
$this->assertEquals( 'http://example.com', Url::getCurrentUrl() );
}

/**
* @covers \wpscholar\Url::getSegment
*/
public function testGetSegmentArrayAccess() {
$url = new Url( 'https://example.com/blog/2023/post-title' );
$segments = $url->getSegments();

// Test array access
$this->assertEquals( 'blog', $segments[0] );
$this->assertEquals( '2023', $segments[1] );
$this->assertEquals( 'post-title', $segments[2] );

// Test array count
$this->assertCount( 3, $segments );
}
}

0 comments on commit 7f98c59

Please sign in to comment.