kodedphp /
http
| 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
|
|||||
| 58 | @rmdir(dirname($this->targetPath)); |
||||
|
0 ignored issues
–
show
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
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 |
If you suppress an error, we recommend checking for the error condition explicitly: