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

MoveUploadedFileTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\UploadedFile;
6
use PHPUnit\Framework\TestCase;
7
use RuntimeException;
8
9
class MoveUploadedFileTest extends TestCase
10
{
11
    use AssertionTestSupportTrait;
12
13
    private UploadedFile $SUT;
14
    private string $file       = '/tmp/y4k9a7fm';
15
    private string $targetPath = '/tmp/test-moved-to/filename.txt';
16
17
    public function test_stream_move_to()
18
    {
19
        $this->SUT->moveTo($this->targetPath);
20
21
        $movedValue = $this->getObjectProperty($this->SUT, 'moved');
22
        $this->assertSame(true, $movedValue);
23
24
        $this->assertFileExists($this->targetPath);
25
        $this->assertFileDoesNotExist($this->file, 'Original file should be deleted after moving');
26
        $this->assertSame('hello', file_get_contents($this->targetPath));
27
28
        // After moving the file the stream is not available
29
        $this->expectException(RuntimeException::class);
30
        $this->expectExceptionMessage('Stream is not available, because the file was previously moved');
31
        $this->SUT->getStream();
32
    }
33
34
    public function test_stream_moved_file_cannot_be_moved_twice()
35
    {
36
        $this->expectException(RuntimeException::class);
37
        $this->expectExceptionMessage('File is not available, because it was previously moved');
38
39
        $this->SUT->moveTo($this->targetPath);
40
        $this->SUT->moveTo($this->targetPath);
41
    }
42
43
    protected function setUp(): void
44
    {
45
        file_put_contents($this->file, 'hello');
46
47
        if (false === is_readable($this->file)) {
48
            $this->markTestSkipped('Unt test failed to create a test file');
49
        }
50
51
        $data      = include __DIR__ . '/fixtures/simple-file-array.php';
52
        $this->SUT = new UploadedFile($data['test']);
53
    }
54
55
    protected function tearDown(): void
56
    {
57
        @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

57
        /** @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...
58
        @rmdir(dirname($this->targetPath));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). 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

58
        /** @scrutinizer ignore-unhandled */ @rmdir(dirname($this->targetPath));

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...
59
        parent::tearDown();
60
    }
61
}
62