2024-09-29 23:53:44 +00:00
< ? php
// BencodeSerialisableTest.php
// Created: 2024-09-29
2024-10-02 01:37:18 +00:00
// Updated: 2024-10-02
2024-09-29 23:53:44 +00:00
declare ( strict_types = 1 );
use PHPUnit\Framework\TestCase ;
use PHPUnit\Framework\Attributes\ { CoversClass , UsesClass };
2024-10-02 01:37:18 +00:00
use Index\Bencode\ { Bencode , BencodeProperty , BencodeSerializableTrait , IBencodeSerializable };
2024-09-29 23:53:44 +00:00
#[CoversClass(BencodeProperty::class)]
2024-10-02 01:37:18 +00:00
#[CoversClass(BencodeSerializableTrait::class)]
2024-09-29 23:53:44 +00:00
#[UsesClass(Bencode::class)]
2024-10-02 01:37:18 +00:00
#[UsesClass(IBencodeSerializable::class)]
2024-09-29 23:53:44 +00:00
final class BencodeSerialisableTest extends TestCase {
private const BASIC_BENCODED_ENCODED = 'd9:stringVal12:string value6:intVali1234e9:intStrVal4:56788:floatVal5:12.3411:floatStrVal5:56.787:trueVali1e22:truePossiblyOmittedVali1e8:falseVali0e14:nullValPresent10:scalarValsli0ei1234e5:12.343:stre16:stringVal_method12:string value9:getIntVali1234e11:getFloatVal5:12.3417:getFloatStringVal5:56.7810:getTrueVali1e25:getTruePossiblyOmittedVali1e11:getFalseVali0e17:getNullValPresent13:getScalarValsli0ei1234e5:12.343:stre5:innerd3:objd3:str21:wow this is illegibleee15:getIntStringVal4:5678e' ;
public function testBasicBencodedObject () : void {
2024-10-02 01:37:18 +00:00
$test = new class implements IBencodeSerializable {
use BencodeSerializableTrait ;
2024-09-29 23:53:44 +00:00
#[BencodeProperty]
public string $stringVal = 'string value' ;
#[BencodeProperty]
public int $intVal = 1234 ;
#[BencodeProperty(numbersAsString: true)]
public int $intStrVal = 5678 ;
#[BencodeProperty]
public float $floatVal = 12.34 ;
#[BencodeProperty(numbersAsString: true)]
public float $floatStrVal = 56.78 ;
#[BencodeProperty]
public bool $trueVal = true ;
#[BencodeProperty(omitIfValue: false)]
public bool $truePossiblyOmittedVal = true ;
#[BencodeProperty]
public bool $falseVal = false ;
#[BencodeProperty(omitIfValue: false)]
public bool $falseOmittedVal = false ;
#[BencodeProperty]
public mixed $nullValOmitted = null ;
#[BencodeProperty(omitIfNull: false)]
public mixed $nullValPresent = null ;
#[BencodeProperty]
public array $scalarVals = [ null , 0 , 1234 , 12.34 , 'str' , true , false ];
#[BencodeProperty('stringVal_method')]
public function getStringVal () : string {
return 'string value' ;
}
#[BencodeProperty]
public function getIntVal () : int {
return 1234 ;
}
#[BencodeProperty(order: 1, numbersAsString: true)]
public function getIntStringVal () : int {
return 5678 ;
}
#[BencodeProperty]
public function getFloatVal () : float {
return 12.34 ;
}
#[BencodeProperty(numbersAsString: true)]
public function getFloatStringVal () : float {
return 56.78 ;
}
#[BencodeProperty]
public function getTrueVal () : bool {
return true ;
}
#[BencodeProperty(omitIfValue: false)]
public function getTruePossiblyOmittedVal () : bool {
return true ;
}
#[BencodeProperty]
public function getFalseVal () : bool {
return false ;
}
#[BencodeProperty(omitIfValue: false)]
public function getFalseOmittedVal () : bool {
return false ;
}
#[BencodeProperty]
public function getNullValOmitted () : mixed {
return null ;
}
#[BencodeProperty(omitIfNull: false)]
public function getNullValPresent () : mixed {
return null ;
}
#[BencodeProperty]
public function getScalarVals () : array {
return [ null , 0 , 1234 , 12.34 , 'str' , true , false ];
}
#[BencodeProperty('inner')]
public function getObject () : object {
2024-10-02 01:37:18 +00:00
return new class implements IBencodeSerializable {
use BencodeSerializableTrait ;
2024-09-29 23:53:44 +00:00
#[BencodeProperty('obj')]
public function getObject () : object {
2024-10-02 01:37:18 +00:00
return new class implements IBencodeSerializable {
use BencodeSerializableTrait ;
2024-09-29 23:53:44 +00:00
#[BencodeProperty('str')]
public function getString () : string {
return 'wow this is illegible' ;
}
};
}
};
}
2024-09-30 17:34:51 +00:00
public function methodWithArguments ( string $meow ) : string {
return $meow ;
}
public static function staticMethodWithArguments ( string $meow ) : string {
return $meow ;
}
2024-09-29 23:53:44 +00:00
};
$this -> assertEquals ( self :: BASIC_BENCODED_ENCODED , Bencode :: encode ( $test ));
}
public function testDoubleBencodedProperty () : void {
$this -> expectException ( RuntimeException :: class );
2024-10-02 01:37:18 +00:00
$test = new class implements IBencodeSerializable {
use BencodeSerializableTrait ;
2024-09-29 23:53:44 +00:00
#[BencodeProperty('test1')]
#[BencodeProperty('test2')]
public string $stringVal = 'string value' ;
#[BencodeProperty('test3')]
#[BencodeProperty('test4')]
public function getIntVal () : int {
return 1234 ;
}
};
Bencode :: encode ( $test );
}
public function testDuplicateBencodedProperty () : void {
$this -> expectException ( RuntimeException :: class );
2024-10-02 01:37:18 +00:00
$test = new class implements IBencodeSerializable {
use BencodeSerializableTrait ;
2024-09-29 23:53:44 +00:00
#[BencodeProperty]
public string $test1 = 'string value' ;
#[BencodeProperty('test1')]
public function getIntVal () : int {
return 1234 ;
}
};
Bencode :: encode ( $test );
}
}