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

XToStream::toStr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\TToStream;
8
9
10
class ToStreamTest extends \CommonTestClass
11
{
12
    /**
13
     * @param mixed $ext
14
     * @param string $result
15
     * @throws FilesException
16
     * @dataProvider filterDataProvider
17
     */
18
    public function testSimple($ext, string $result): void
19
    {
20
        $lib = new XToStream();
21
        $str = $lib->toStr($ext);
22
        $this->assertEquals($result, stream_get_contents($str, -1, 0));
23
    }
24
25
    public function filterDataProvider(): array
26
    {
27
        $stream1 = fopen('php://memory', 'r+');
28
        fwrite($stream1,'Just for unable read');
29
        return [
30
            [$stream1, 'Just for unable read'],
31
            ['there is no string', 'there is no string'],
32
            [123456, '123456'],
33
            [123.456, '123.456'],
34
        ];
35
    }
36
37
    /**
38
     * @param mixed $ext
39
     * @throws FilesException
40
     * @dataProvider dieProvider
41
     */
42
    public function testDie($ext): void
43
    {
44
        $lib = new XToStream();
45
        $this->expectException(FilesException::class);
46
        $lib->toStr($ext);
47
    }
48
49
    public function dieProvider(): array
50
    {
51
        return [
52
            [new StrObj2()],
53
            [new \stdClass()],
54
            [true],
55
            [false],
56
            [null],
57
        ];
58
    }
59
}
60
61
62
class XToStream
63
{
64
    use TToStream;
65
66
    /**
67
     * @param mixed $content
68
     * @throws FilesException
69
     * @return resource
70
     */
71
    public function toStr($content)
72
    {
73
        return $this->toStream('test', $content);
74
    }
75
}
76
77
78
class StrObj2
79
{
80
    public function __toString(): string
81
    {
82
        return 'test';
83
    }
84
}