Passed
Push — master ( 2ddebf...84c74c )
by Petr
09:17 queued 01:35
created

ToResourceTest::testResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace ChecksTests\TraitsTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mime\Check\Traits\TToResource;
8
use kalanis\kw_mime\MimeException;
9
10
11
class ToResourceTest extends CommonTestClass
12
{
13
    /**
14
     * @throws MimeException
15
     */
16
    public function testNone1(): void
17
    {
18
        $lib = new XToResource();
19
        $this->expectException(MimeException::class);
20
        $lib->convert('file', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type resource|string expected by parameter $content of ChecksTests\TraitsTests\XToResource::convert(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        $lib->convert('file', /** @scrutinizer ignore-type */ false);
Loading history...
21
    }
22
23
    /**
24
     * @throws MimeException
25
     */
26
    public function testNone2(): void
27
    {
28
        $lib = new XToResource();
29
        $this->expectException(MimeException::class);
30
        $lib->convert('file', null);
31
    }
32
33
    /**
34
     * @throws MimeException
35
     */
36
    public function testString(): void
37
    {
38
        $lib = new XToResource();
39
        $this->assertEquals('test data', stream_get_contents($lib->convert('data', 'test data'), -1, 0));
40
        $this->assertEquals('123456', stream_get_contents($lib->convert('nums', 123456), -1, 0));
41
    }
42
43
    /**
44
     * @throws MimeException
45
     */
46
    public function testResource(): void
47
    {
48
        $lib = new XToResource();
49
        $res = @fopen('php://memory', 'r+');
50
        fwrite($res, 'okmijnuhb');
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $stream of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        fwrite(/** @scrutinizer ignore-type */ $res, 'okmijnuhb');
Loading history...
51
        $this->assertEquals('okmijnuhb', stream_get_contents($lib->convert('res', $res), -1, 0));
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type false; however, parameter $content of ChecksTests\TraitsTests\XToResource::convert() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->assertEquals('okmijnuhb', stream_get_contents($lib->convert('res', /** @scrutinizer ignore-type */ $res), -1, 0));
Loading history...
52
    }
53
}
54
55
56
class XToResource
57
{
58
    use TToResource;
59
60
    /**
61
     * @param string $name
62
     * @param string|resource $content
63
     * @throws MimeException
64
     * @return resource
65
     */
66
    public function convert(string $name, $content)
67
    {
68
        return $this->readSourceToResource($name, $content);
69
    }
70
}
71