StreamBufferTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
 * This file is part of graze/csv-token
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/csv-token/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/csv-token
12
 */
13
14
namespace Graze\CsvToken\Test\Unit\Buffer;
15
16
use Graze\CsvToken\Buffer\StreamBuffer;
17
use Graze\CsvToken\Test\TestCase;
18
19
class StreamBufferTest extends TestCase
20
{
21
    /**
22
     * @param string $string
23
     *
24
     * @return resource
25
     */
26
    private function makeStream($string)
27
    {
28
        $stream = fopen('php://memory', 'r+');
29
        fwrite($stream, $string);
30
        rewind($stream);
31
        return $stream;
32
    }
33
34
    public function testBasicProperties()
35
    {
36
        $stream = $this->makeStream('some text here and things');
37
        $buffer = new StreamBuffer($stream);
38
39
        static::assertEquals(0, $buffer->getPosition());
40
        static::assertEquals('', $buffer->getContents());
41
        static::assertEquals(0, $buffer->getLength());
42
        static::assertTrue($buffer->isEof());
43
        static::assertFalse($buffer->isSourceEof());
44
45
        $buffer->read();
46
47
        static::assertEquals(0, $buffer->getPosition());
48
        static::assertEquals('some text here and things', $buffer->getContents());
49
        static::assertEquals(25, $buffer->getLength());
50
        static::assertFalse($buffer->isEof());
51
        static::assertFalse($buffer->isSourceEof());
52
53
        $buffer->read();
54
55
        static::assertTrue($buffer->isSourceEof());
56
    }
57
58
    public function testReadSize()
59
    {
60
        $stream = $this->makeStream('first second third fourth fifth');
61
        $buffer = new StreamBuffer($stream, 6);
62
63
        $buffer->read();
64
65
        static::assertEquals('first ', $buffer->getContents());
66
    }
67
68
    public function testMinSize()
69
    {
70
        $stream = $this->makeStream('first second third fourth');
71
        $buffer = new StreamBuffer($stream, 10, 2);
72
73
        $buffer->read();
74
        static::assertEquals('first seco', $buffer->getContents());
75
        $buffer->read();
76
        static::assertEquals('first seco', $buffer->getContents());
77
78
        static::assertEquals(2, $buffer->getMinBufferSize());
79
        static::assertSame($buffer, $buffer->setMinBufferSize(-1));
80
        static::assertEquals(-1, $buffer->getMinBufferSize());
81
        $buffer->read();
82
        static::assertEquals('first second third f', $buffer->getContents());
83
    }
84
85
    public function testMove()
86
    {
87
        $stream = $this->makeStream('first second third fourth');
88
        $buffer = new StreamBuffer($stream, 10, 2);
89
90
        $buffer->read();
91
        static::assertEquals('first seco', $buffer->getContents());
92
        $buffer->move(9);
93
        static::assertEquals('o', $buffer->getContents());
94
        $buffer->read();
95
        static::assertEquals('ond third f', $buffer->getContents());
96
    }
97
}
98