diff --git a/src/Buffertools/Parser.php b/src/Buffertools/Parser.php index 1565bbe..5ca477c 100644 --- a/src/Buffertools/Parser.php +++ b/src/Buffertools/Parser.php @@ -26,25 +26,19 @@ class Parser /** * Instantiate class, optionally taking Buffer or HEX. * - * @param null|string|BufferInterface $input + * @param BufferInterface $input */ - public function __construct($input = null) + public function __construct(BufferInterface $input = null) { - if (null === $input) { - $input = ''; - } + $this->position = 0; - if (is_string($input)) { - $bin = Buffer::hex($input, null)->getBinary(); - } elseif ($input instanceof BufferInterface) { - $bin = $input->getBinary(); + if (null === $input) { + $this->string = ''; + $this->size = 0; } else { - throw new \InvalidArgumentException("Invalid argument to Parser"); + $this->string = $input->getBinary(); + $this->size = $input->getSize(); } - - $this->string = $bin; - $this->position = 0; - $this->size = strlen($this->string); } /** diff --git a/tests/ParserTest.php b/tests/ParserTest.php index e2dbc36..46757b0 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -39,20 +39,20 @@ public function testGetBufferEmptyNull() public function testWriteBytes() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); + $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex($bytes)); - $returned = $parser->getBuffer()->getHex(); - $this->assertSame($returned, '41424344'); + $parser->writeBytes(4, $bytes); + $this->assertTrue($parser->getBuffer()->equals($bytes)); } public function testWriteBytesFlip() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex($bytes), true); - $returned = $parser->getBuffer()->getHex(); - $this->assertSame($returned, '44434241'); + $parser->writeBytes(4, $bytes, true); + + $this->assertEquals('44434241', $parser->getBuffer()->getHex()); } public function testWriteBytesPadded() @@ -71,26 +71,24 @@ public function testWriteBytesFlipPadded() public function testReadBytes() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $read = $parser->readBytes(4); $this->assertInstanceOf(Buffer::class, $read); - $hex = $read->getHex(); - $this->assertSame($bytes, $hex); + $this->assertTrue($read->equals($bytes)); } public function testReadBytesFlip() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $read = $parser->readBytes(4, true); $this->assertInstanceOf(Buffer::class, $read); - $hex = $read->getHex(); - $this->assertSame('44434241', $hex); + $this->assertEquals($bytes->flip()->getHex(), $read->getHex()); } /** @@ -103,8 +101,7 @@ public function testReadBytesEmpty() // and length is zero. $parser = new Parser(); - $data = $parser->readBytes(0); - $this->assertFalse(!!$data); + $parser->readBytes(0); } /** * @expectedException \BitWasp\Buffertools\Exceptions\ParserOutOfRange @@ -112,7 +109,7 @@ public function testReadBytesEmpty() */ public function testReadBytesEndOfString() { - $parser = new Parser('4041414142414141'); + $parser = new Parser(Buffer::hex('4041414142414141')); $bytes1 = $parser->readBytes(4); $bytes2 = $parser->readBytes(4); $this->assertSame($bytes1->getHex(), '40414141'); @@ -125,7 +122,7 @@ public function testReadBytesEndOfString() */ public function testReadBytesBeyondLength() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $parser->readBytes(5); } diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 84f8c19..f42b409 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -29,7 +29,7 @@ public function testTemplate() public function testTemplateEmptyParse() { $template = new Template(); - $parser = new Parser('010203040a0b0c0d'); + $parser = new Parser(Buffer::hex('010203040a0b0c0d')); $template->parse($parser); }