Skip to content

Commit

Permalink
Require Buffer for Parser constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin committed Dec 30, 2017
1 parent 5c2c8c0 commit 01f393b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
22 changes: 8 additions & 14 deletions src/Buffertools/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
33 changes: 15 additions & 18 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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());
}

/**
Expand All @@ -103,16 +101,15 @@ 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
* @expectedExceptionMessage Could not parse string of required length (empty)
*/
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');
Expand All @@ -125,7 +122,7 @@ public function testReadBytesEndOfString()
*/
public function testReadBytesBeyondLength()
{
$bytes = '41424344';
$bytes = Buffer::hex('41424344');
$parser = new Parser($bytes);
$parser->readBytes(5);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 01f393b

Please sign in to comment.