Skip to content

Commit

Permalink
Add length functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Aug 8, 2024
1 parent 2294b72 commit 20ef831
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
23 changes: 16 additions & 7 deletions src/Connections/EPPTCPConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(EPPRegistry $registry){
* @param string $data
* @return int
*/
private function decodeInteger(string $data): int{
protected function decodeInteger(string $data): int{
return unpack('N',substr($data,0,4))[1] ?? -1;
}

Expand All @@ -29,14 +29,14 @@ private function decodeInteger(string $data): int{
* @param int $integer
* @return string
*/
private function encodeInteger(int $integer): string{
protected function encodeInteger(int $integer): string{
return pack('N',$integer);
}

/**
* Ensure if connection is not closed
*/
private function ensureConnection(): void{
protected function ensureConnection(): void{
if($this->isClosed()){
throw new RuntimeException('Connection closed');
}
Expand All @@ -62,19 +62,28 @@ public function open(): bool{
return $this->socket!==false;
}

public function readXML(): ?string{
protected function readLength(): int{
$this->ensureConnection();
$length = $this->decodeInteger(fread($this->socket,4))-4;
return $this->decodeInteger(fread($this->socket,4))-4;
}

public function readXML(): ?string{
$length = $this->readLength();
if($length<0){
return null;
}
return fread($this->socket,$length);
}

public function writeXML(string $xml): void{
protected function writeLength(int $length): void{
$this->ensureConnection();
fwrite($this->socket,$this->encodeInteger($length));
}

public function writeXML(string $xml): void{
$length = strlen($xml)+4;
fwrite($this->socket,$this->encodeInteger($length).$xml);
$this->writeLength($length);
fwrite($this->socket,$xml);
}

}
14 changes: 11 additions & 3 deletions tests/EPPConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace YOCLIB\EPP\Tests;

use PHPUnit\Framework\TestCase;
use RuntimeException;
use YOCLIB\EPP\Connections\EPPBaseConnection;
use YOCLIB\EPP\Connections\EPPTCPConnection;
use YOCLIB\EPP\Elements\EPPEppElement;
Expand All @@ -19,7 +20,7 @@ public function readXML(): ?string{
}

public function writeXML(string $xml): void{
assertEquals('<writeXML/>',$xml);
assertEquals('<?xml version="1.0"?>'."\n".'<writeXML/>'."\n",$xml);
}

public function close(): bool{
Expand All @@ -35,8 +36,11 @@ public function open(): bool{
}
};

$this->assertEquals('<readXML/>',$conn->readXML());
$conn->writeXML('<writeXML/>');
$this->assertEquals('readXML',$conn->readDocument()->documentElement->tagName);

$doc = EPPDocumentHelper::createEPPDocument();
$doc->loadXML('<writeXML/>');
$conn->writeDocument($doc);

$this->assertTrue($conn->isClosed());
$this->assertTrue($conn->open());
Expand Down Expand Up @@ -68,6 +72,10 @@ public function testTCPConnection(){
$this->assertFalse($conn->isClosed());
$this->assertTrue($conn->close());
$this->assertTrue($conn->isClosed());

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Connection closed');
$conn->readXML();
}

}

0 comments on commit 20ef831

Please sign in to comment.