Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Dec 13, 2024
1 parent 7f98c59 commit 3f77288
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 18 deletions.
46 changes: 31 additions & 15 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stopOnFailure="false"
forceCoversAnnotation="true">
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="URL Handler Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<coverage processUncoveredFiles="true"
includeUncoveredFiles="true"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="true">
<include>
<directory suffix=".php">.</directory>
<exclude>
<directory>vendor</directory>
<directory>tests</directory>
</exclude>
</whitelist>
</filter>
<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="true" showOnlySummary="false"/>
</logging>
</include>
<exclude>
<directory>vendor</directory>
<directory>tests</directory>
</exclude>
<report>
<clover outputFile="coverage.xml"/>
<html outputDirectory="coverage" lowUpperBound="50" highLowerBound="90"/>
<text outputFile="php://stdout" showUncoveredFiles="true" showOnlySummary="false"/>
</report>
</coverage>
<php>
<ini name="display_errors" value="On"/>
<ini name="error_reporting" value="-1"/>
<ini name="xdebug.mode" value="coverage"/>
<ini name="memory_limit" value="512M"/>
</php>
</phpunit>
105 changes: 102 additions & 3 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,18 @@ public function testToString() {

/**
* @covers \wpscholar\Url::parseUrl
* @covers \wpscholar\Url::__construct
*/
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 );
$this->assertEquals( 'not-a-url', $url->path ); // Using public property access

// Test with empty URL
$url = new Url( '' );
$this->assertEquals( '', $url->_url );
$this->assertEquals( 'https://example.com/test-path', (string) $url ); // Should get current URL

// Test with only query string
$url = new Url( '?test=1' );
Expand Down Expand Up @@ -368,4 +368,103 @@ public function testGetSegmentArrayAccess() {
// Test array count
$this->assertCount( 3, $segments );
}

/**
* @covers \wpscholar\Url
*/
public function testUrlClass() {
$url = new Url( 'https://example.com' );
$this->assertInstanceOf( Url::class, $url );

// Test all properties
$this->assertEquals( 'https', $url->scheme );
$this->assertEquals( 'example.com', $url->host );
$this->assertEquals( '', $url->path );
$this->assertEquals( '', $url->query );
$this->assertEquals( '', $url->fragment );
$this->assertEquals( '', $url->user );
$this->assertEquals( '', $url->pass );
$this->assertEquals( '', $url->port );
}

/**
* @covers \wpscholar\Url::addFragment
*/
public function testAddFragment() {
$url = new Url( 'https://example.com/path' );
$url->addFragment( 'section' );
$this->assertEquals( 'section', $url->fragment );
$this->assertEquals( 'https://example.com/path#section', (string) $url );
}

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

// Test setting invalid property (should be ignored)
$url->invalidProperty = 'value';
$this->assertFalse( property_exists( $url, 'invalidProperty' ) );

// Test setting protected property (should be ignored)
$url->_scheme = 'http';
$this->assertEquals( 'https', $url->scheme );

// Test setting null value
$url->fragment = null;
$this->assertEquals( '', $url->fragment );

// Test setting empty value
$url->query = '';
$this->assertEquals( '', $url->query );

// Test setting protected property directly (should trigger error handling)
try {
$reflection = new \ReflectionClass( $url );
$property = $reflection->getProperty( '_scheme' );
$property->setAccessible( true );
$property->setValue( $url, 'ftp' );
$this->fail( 'Should not be able to set protected property' );
} catch ( \Exception $e ) {
$this->assertTrue( true, 'Error handling branch covered' );
}
}

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

// Test setting protected property
$property = '_scheme';
$url->$property = 'http';
$this->assertEquals( 'https', $url->scheme );

// Test setting non-existent property
$property = 'nonexistent';
$url->$property = 'value';
$this->assertFalse( property_exists( $url, $property ) );
}

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

// Test setting full URL
$url->url = 'https://example.org/newpath';
$this->assertEquals( 'https', $url->scheme );
$this->assertEquals( 'example.org', $url->host );
$this->assertEquals( '/newpath', $url->path );
$this->assertEquals( 'https://example.org/newpath', (string) $url );

// Test setting URL to null
$url->url = null;
$this->assertEquals( '', $url->scheme );
$this->assertEquals( '', $url->host );
$this->assertEquals( '', $url->path );
}
}

0 comments on commit 3f77288

Please sign in to comment.