Passed
Pull Request — master (#17)
by Mihail
15:10
created

FileStreamTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 36
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A test_should_create_only_writable_stream() 0 14 1
A test_should_create_read_write_stream_by_default() 0 11 1
A tearDown() 0 3 1
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\FileStream;
6
use PHPUnit\Framework\TestCase;
7
use RuntimeException;
8
9
class FileStreamTest extends TestCase
10
{
11
    private string $file = '/tmp/test';
12
13
    public function test_should_create_only_writable_stream()
14
    {
15
        $this->expectException(RuntimeException::class);
16
        $this->expectExceptionMessage('The stream is not readable');
17
18
        $stream = new FileStream($this->file, 'w');
19
        $stream->write('hello world');
20
21
        $this->assertSame('w', $stream->getMetadata('mode'));
22
        $this->assertSame('', $stream->getContents());
23
24
        $this->assertFalse($stream->isReadable());
25
        $this->assertFalse($stream->isSeekable());
26
        $this->assertTrue($stream->isWritable());
27
    }
28
29
    public function test_should_create_read_write_stream_by_default()
30
    {
31
        $stream = new FileStream($this->file, 'w+');
32
        $stream->write('hello world');
33
34
        $this->assertSame('w+', $stream->getMetadata('mode'));
35
        $this->assertSame('hello world', $stream->getContents());
36
37
        $this->assertTrue($stream->isReadable());
38
        $this->assertTrue($stream->isSeekable());
39
        $this->assertTrue($stream->isWritable());
40
    }
41
42
    protected function tearDown(): void
43
    {
44
        @unlink($this->file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

44
        /** @scrutinizer ignore-unhandled */ @unlink($this->file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
    }
46
}
47