Skip to content

Commit 7a2d449

Browse files
authored
Add missing tests for inMemoryStream (#98)
1 parent 80ab7d6 commit 7a2d449

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

src/Stream/InMemoryStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public function slice(int $startByteOffset, int $endByteOffset): string {
4545
#[Override]
4646
public function chars(int $from, int $nrOfBytes): iterable {
4747
if ($from < 0) {
48-
throw new InvalidArgumentException(sprintf('StartOffset should be greater than zero, %d given', $from));
48+
throw new InvalidArgumentException(sprintf('$from must be greater than zero, %d given', $from));
4949
}
5050

5151
if ($nrOfBytes <= 0) {
52-
throw new InvalidArgumentException(sprintf('$nrOfBytes to read must be greater than 0, %d given', $nrOfBytes));
52+
throw new InvalidArgumentException(sprintf('$nrOfBytes to read must be greater than zero, %d given', $nrOfBytes));
5353
}
5454

5555
foreach (str_split(substr($this->content, $from, $nrOfBytes)) as $char) {

tests/Unit/Stream/InMemoryStreamTest.php

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,54 @@ public function testGetSizeInBytes(): void {
1414
static::assertSame(3, (new InMemoryStream('foo'))->getSizeInBytes());
1515
}
1616

17+
public function testReadThrowsExceptionWithNegativeBytes(): void {
18+
$this->expectException(InvalidArgumentException::class);
19+
$this->expectExceptionMessage('$nrOfBytes must be greater than 0, -1 given');
20+
(new InMemoryStream('foo'))
21+
->read(0, -1);
22+
}
23+
24+
public function testRead(): void {
25+
static::assertSame('foo', (new InMemoryStream('foo'))->read(0, 3));
26+
static::assertSame('o', (new InMemoryStream('foo'))->read(2, 1));
27+
}
28+
29+
public function testSliceThrowsExceptionOnInvalidStartByteOffset(): void {
30+
$stream = new InMemoryStream('foobar');
31+
$this->expectException(InvalidArgumentException::class);
32+
$this->expectExceptionMessage('$startByteOffset must be greater than 0, -1 given');
33+
$stream->slice(-1, 2);
34+
}
35+
36+
public function testSliceThrowsExceptionOnInvalidEndByteOffset(): void {
37+
$stream = new InMemoryStream('foobar');
38+
$this->expectException(InvalidArgumentException::class);
39+
$this->expectExceptionMessage('End byte offset 2 should be bigger than start byte offset 2');
40+
$stream->slice(2, 2);
41+
}
42+
43+
public function testSlice(): void {
44+
$stream = new InMemoryStream('foobar');
45+
static::assertSame('ob', $stream->slice(2, 4));
46+
}
47+
48+
public function testCharsThrowsExceptionOnInvalidFromValue(): void {
49+
$this->expectException(InvalidArgumentException::class);
50+
$this->expectExceptionMessage('$from must be greater than zero, -1 given');
51+
iterator_to_array((new InMemoryStream('foobar'))->chars(-1, 2));
52+
}
53+
54+
public function testCharsThrowsExceptionOnInvalidNrOfBytesValue(): void {
55+
$this->expectException(InvalidArgumentException::class);
56+
$this->expectExceptionMessage('$nrOfBytes to read must be greater than zero, -2 given');
57+
iterator_to_array((new InMemoryStream('foobar'))->chars(0, -2));
58+
}
59+
60+
public function testChars(): void {
61+
$stream = new InMemoryStream('foobar');
62+
static::assertSame(['o', 'b'], iterator_to_array($stream->chars(2, 2)));
63+
}
64+
1765
public function testFirstPos(): void {
1866
$stream = new InMemoryStream('123objxref');
1967
static::assertSame(
@@ -43,35 +91,4 @@ public function testLastPos(): void {
4391
$stream->lastPos(Marker::TRAILER, 0)
4492
);
4593
}
46-
47-
public function testSlice(): void {
48-
$stream = new InMemoryStream('foobar');
49-
static::assertSame('ob', $stream->slice(2, 4));
50-
}
51-
52-
public function testSliceThrowsExceptionOnInvalidStartByteOffset(): void {
53-
$stream = new InMemoryStream('foobar');
54-
$this->expectException(InvalidArgumentException::class);
55-
$this->expectExceptionMessage('$startByteOffset must be greater than 0, -1 given');
56-
$stream->slice(-1, 2);
57-
}
58-
59-
public function testSliceThrowsExceptionOnInvalidEndByteOffset(): void {
60-
$stream = new InMemoryStream('foobar');
61-
$this->expectException(InvalidArgumentException::class);
62-
$this->expectExceptionMessage('End byte offset 2 should be bigger than start byte offset 2');
63-
$stream->slice(2, 2);
64-
}
65-
66-
public function testChars(): void {
67-
$stream = new InMemoryStream('foobar');
68-
static::assertSame(['o', 'b'], iterator_to_array($stream->chars(2, 2)));
69-
}
70-
71-
public function testReadBytesThrowsExceptionWithNegativeBytes(): void {
72-
$this->expectException(InvalidArgumentException::class);
73-
$this->expectExceptionMessage('$nrOfBytes must be greater than 0, -1 given');
74-
(new InMemoryStream('foo'))
75-
->read(0, -1);
76-
}
7794
}

0 commit comments

Comments
 (0)