Skip to content

Commit

Permalink
Merge pull request #7 from sc0Vu/master
Browse files Browse the repository at this point in the history
Minor change
  • Loading branch information
sc0Vu authored Mar 8, 2018
2 parents 383af61 + a3f1795 commit c4b0e84
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Buffer implements ArrayAccess
*
* @var array
*/
protected $data=[];
protected $data = [];

/**
* encoding
*
* @var string
*/
protected $encoding='';
protected $encoding = '';

/**
* construct
Expand Down Expand Up @@ -135,7 +135,7 @@ public function toString(string $encoding='utf8')
}
break;
default:
$output = implode('', $input);
throw new InvalidArgumentException('ToString encoding must be valid.');
break;
}
return $output;
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function stringToData(string $input, string $encoding)
$output = unpack('C*', $input);
break;
default:
$output = str_split($input, 1);
throw new InvalidArgumentException('StringToData encoding must be valid.');
break;
}
return $output;
Expand Down
30 changes: 30 additions & 0 deletions test/unit/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ public function testCreateStringBuffer()
$this->assertEquals('我是測試', $buffer->toString('utf8'));
$this->assertEquals('e68891e698afe6b8ace8a9a6', $buffer->toString('hex'));
$this->assertEquals(12, $buffer->length());

try {
$this->assertEquals('我是測試', $buffer->toString('wrongencoding'));
} catch (InvalidArgumentException $e) {
$this->assertEquals('ToString encoding must be valid.', $e->getMessage());
}
try {
$buffer = new Buffer('Wrong encoding.', 'wrongencoding');
} catch (InvalidArgumentException $e) {
$this->assertEquals('StringToData encoding must be valid.', $e->getMessage());
}
}

/**
Expand Down Expand Up @@ -130,4 +141,23 @@ public function testSlice()
$this->assertEquals('', $buffer->slice(2, 0)->toString('ascii'));
$this->assertEquals(0, $buffer->slice(2, 0)->length());
}

/**
* testArrayAccess
*
* @return void
*/
public function testArrayAccess()
{
$buffer = new Buffer('Hello World', 'ascii');
$this->assertTrue(isset($buffer[10]));

$buffer[11] = 65;
$this->assertEquals('Hello WorldA', $buffer->toString('ascii'));
$this->assertEquals(12, $buffer->length());

unset($buffer[11]);
$this->assertEquals('Hello World', $buffer->toString('ascii'));
$this->assertEquals(11, $buffer->length());
}
}

0 comments on commit c4b0e84

Please sign in to comment.