Passed
Push — master ( 1f33ad...fba3db )
by Petr
08:38
created

StreamToPosTest::testSimple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 4
dl 0
loc 14
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace TraitsTests;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Traits\TStreamToPos;
8
9
10
class StreamToPosTest extends \CommonTestClass
11
{
12
    /**
13
     * @param string $original
14
     * @param string $add
15
     * @param int|null $offset
16
     * @param string $expect
17
     * @throws FilesException
18
     * @dataProvider filterDataProvider
19
     */
20
    public function testSimple(string $original, string $add, ?int $offset, string $expect): void
21
    {
22
        $lib = new XToPos();
23
        // create streams
24
        $orig = fopen('php://memory', 'r+');
25
        fwrite($orig, $original);
26
        rewind($orig);
27
        $cont = fopen('php://memory', 'r+');
28
        fwrite($cont, $add);
29
        rewind($cont);
30
        // process
31
        $result = $lib->addStreamToPosition($orig, $cont, $offset);
32
        // get stream content
33
        $this->assertEquals($expect, stream_get_contents($result, -1, 0));
34
    }
35
36
    public function filterDataProvider(): array
37
    {
38
        return [
39
            ['abcdabcd', 'ABCD', null, 'ABCD'], // full replace
40
            ['abcdabcd', 'ABCD', 2, 'abABCDcd'], // replace inside
41
            ['abcd', 'ABCD', 6, "abcd\0\0ABCD"], // append at the end
42
            ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', null, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'], // full replace
43
            ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 13, 'abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZnopqrstuvwxy'], // replace inside
44
            ['abcdefghijklmnopqrstuvwxy', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 30, "abcdefghijklmnopqrstuvwxy\0\0\0\0\0ABCDEFGHIJKLMNOPQRSTUVWXYZ"], // append at the end
45
        ];
46
    }
47
}
48
49
50
class XToPos
51
{
52
    use TStreamToPos;
53
}
54