Skip to content

Commit

Permalink
Add tests for new exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 23, 2025
1 parent f112646 commit 6e7594e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Fields/FQDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function makeRelative(FQDN $origin,?bool $ignoreCurrentState=false): FQDN
throw new DNSFieldException("FQDN already relative.");
}
for($i=0;$i<count($origin->value);$i++){
if(($origin->value[count($origin->value)-$i] ?? null)!==($this->value[count($this->value)-$i] ?? null)){
if(($origin->value[count($origin->value)-$i-1] ?? null)!==($this->value[count($this->value)-$i-1] ?? null)){
if($ignoreCurrentState){
return $this;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Fields/FQDNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public function testMakeAbsoluteOriginRelative(){
(new FQDN('example','com'))->makeAbsolute($origin,true);
}

/**
* @return void
* @throws DNSFieldException
*/
public function testMakeAbsoluteAlreadyAbsolute(){
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('FQDN already absolute.');

$origin = new FQDN('example','com','');

(new FQDN('example','com',''))->makeAbsolute($origin);
}

/**
* @return void
* @throws DNSFieldException
Expand All @@ -144,8 +157,10 @@ public function testMakeRelative(){

self::assertSame(['example','com'],(new FQDN('example','com','example','com',''))->makeRelative($origin,true)->getValue());
self::assertSame([],(new FQDN('example','com',''))->makeRelative($origin,true)->getValue());
self::assertSame(['example','com'],(new FQDN('example','com'))->makeRelative($origin,true)->getValue());
self::assertSame(['www','example','com'],(new FQDN('www','example','com','example','com',''))->makeRelative($origin,true)->getValue());
self::assertSame(['www'],(new FQDN('www','example','com',''))->makeRelative($origin,true)->getValue());
self::assertSame(['www','example','com'],(new FQDN('www','example','com'))->makeRelative($origin,true)->getValue());
}

/**
Expand All @@ -161,6 +176,32 @@ public function testMakeRelativeOriginRelative(){
(new FQDN('example','com','example','com',''))->makeRelative($origin,true);
}

/**
* @return void
* @throws DNSFieldException
*/
public function testMakeRelativeAlreadyRelative(){
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('FQDN already relative.');

$origin = new FQDN('example','com','');

(new FQDN('www'))->makeRelative($origin);
}

/**
* @return void
* @throws DNSFieldException
*/
public function testMakeRelativeNonSubordinate(){
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('FQDN is not subordinate to origin.');

$origin = new FQDN('test','example','com','');

(new FQDN('www','example','com',''))->makeRelative($origin);
}

/**
* @return void
* @throws DNSFieldException
Expand Down

0 comments on commit 6e7594e

Please sign in to comment.