Completed
Pull Request — master (#8)
by Harry
28:53
created

StreamBufferTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 79
wmc 5
lcom 1
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A makeStream() 0 7 1
A testBasicProperties() 0 23 1
A testReadSize() 0 9 1
A testMinSize() 0 16 1
A testMove() 0 12 1
1
<?php
2
3
namespace Graze\CsvToken\Test\Unit\Buffer;
4
5
use Graze\CsvToken\Buffer\StreamBuffer;
6
use Graze\CsvToken\Test\TestCase;
7
8
class StreamBufferTest extends TestCase
9
{
10
    /**
11
     * @param string $string
12
     *
13
     * @return resource
14
     */
15
    private function makeStream($string)
16
    {
17
        $stream = fopen('php://memory', 'r+');
18
        fwrite($stream, $string);
19
        rewind($stream);
20
        return $stream;
21
    }
22
23
    public function testBasicProperties()
24
    {
25
        $stream = $this->makeStream('some text here and things');
26
        $buffer = new StreamBuffer($stream);
27
28
        static::assertEquals(0, $buffer->getPosition());
29
        static::assertEquals('', $buffer->getContents());
30
        static::assertEquals(0, $buffer->getLength());
31
        static::assertTrue($buffer->isEof());
32
        static::assertFalse($buffer->isSourceEof());
33
34
        $buffer->read();
35
36
        static::assertEquals(0, $buffer->getPosition());
37
        static::assertEquals('some text here and things', $buffer->getContents());
38
        static::assertEquals(25, $buffer->getLength());
39
        static::assertFalse($buffer->isEof());
40
        static::assertFalse($buffer->isSourceEof());
41
42
        $buffer->read();
43
44
        static::assertTrue($buffer->isSourceEof());
45
    }
46
47
    public function testReadSize()
48
    {
49
        $stream = $this->makeStream('first second third fourth fifth');
50
        $buffer = new StreamBuffer($stream, 6);
51
52
        $buffer->read();
53
54
        static::assertEquals('first ', $buffer->getContents());
55
    }
56
57
    public function testMinSize()
58
    {
59
        $stream = $this->makeStream('first second third fourth');
60
        $buffer = new StreamBuffer($stream, 10, 2);
61
62
        $buffer->read();
63
        static::assertEquals('first seco', $buffer->getContents());
64
        $buffer->read();
65
        static::assertEquals('first seco', $buffer->getContents());
66
67
        static::assertEquals(2, $buffer->getMinBufferSize());
68
        static::assertSame($buffer, $buffer->setMinBufferSize(-1));
69
        static::assertEquals(-1, $buffer->getMinBufferSize());
70
        $buffer->read();
71
        static::assertEquals('first second third f', $buffer->getContents());
72
    }
73
74
    public function testMove()
75
    {
76
        $stream = $this->makeStream('first second third fourth');
77
        $buffer = new StreamBuffer($stream, 10, 2);
78
79
        $buffer->read();
80
        static::assertEquals('first seco', $buffer->getContents());
81
        $buffer->move(9);
82
        static::assertEquals('o', $buffer->getContents());
83
        $buffer->read();
84
        static::assertEquals('ond third f', $buffer->getContents());
85
    }
86
}
87