|
3 | 3 | namespace Tests; |
4 | 4 |
|
5 | 5 | use Aternos\CurlPsr\Psr17\Psr17Factory; |
| 6 | +use Aternos\CurlPsr\Psr7\Stream\StringStream; |
6 | 7 | use InvalidArgumentException; |
7 | 8 | use PHPUnit\Framework\TestCase; |
8 | 9 | use Psr\Http\Message\StreamInterface; |
@@ -66,11 +67,50 @@ public function testThrowWhenMovedToInvalidTarget(): void |
66 | 67 | $uploadedFile->moveTo($target); |
67 | 68 | } |
68 | 69 |
|
69 | | - public function testThrowIfStreamIsNotAFile(): void |
| 70 | + public function testWriteStreamContentIfStreamIsNotAFile(): void |
70 | 71 | { |
71 | 72 | $stream = $this->factory->createStream('test'); |
72 | | - $this->expectException(InvalidArgumentException::class); |
73 | | - $this->expectExceptionMessage('Stream uri not available'); |
74 | | - $this->factory->createUploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, "file.txt", "text/plain"); |
| 73 | + $upload = $this->factory->createUploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, "file.txt", "text/plain"); |
| 74 | + |
| 75 | + $this->target = tempnam(sys_get_temp_dir(), 'test'); |
| 76 | + $upload->moveTo($this->target); |
| 77 | + |
| 78 | + $this->assertFileExists($this->target); |
| 79 | + $this->assertEquals('test', file_get_contents($this->target)); |
| 80 | + } |
| 81 | + |
| 82 | + public function testThrowIfWriteTargetCannotBeOpened(): void |
| 83 | + { |
| 84 | + $stream = $this->factory->createStream('test'); |
| 85 | + $upload = $this->factory->createUploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, "file.txt", "text/plain"); |
| 86 | + |
| 87 | + $this->expectException(RuntimeException::class); |
| 88 | + $this->expectExceptionMessage('Target path could not be opened'); |
| 89 | + $upload->moveTo(sys_get_temp_dir() . "/non-existing-dir/file.txt"); |
| 90 | + } |
| 91 | + |
| 92 | + public function testRewindStreamIfNecessary(): void |
| 93 | + { |
| 94 | + $stream = $this->factory->createStream('test'); |
| 95 | + $stream->read(1); |
| 96 | + $upload = $this->factory->createUploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, "file.txt", "text/plain"); |
| 97 | + |
| 98 | + $this->target = tempnam(sys_get_temp_dir(), 'test'); |
| 99 | + $upload->moveTo($this->target); |
| 100 | + |
| 101 | + $this->assertFileExists($this->target); |
| 102 | + $this->assertEquals('test', file_get_contents($this->target)); |
| 103 | + } |
| 104 | + |
| 105 | + public function testThrowIfStreamNeedsRewindButIsNotSeekable(): void |
| 106 | + { |
| 107 | + $stream = new StringStream('test', false); |
| 108 | + $stream->read(1); |
| 109 | + $upload = $this->factory->createUploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, "file.txt", "text/plain"); |
| 110 | + $this->target = tempnam(sys_get_temp_dir(), 'test'); |
| 111 | + |
| 112 | + $this->expectException(RuntimeException::class); |
| 113 | + $this->expectExceptionMessage('Stream needs to be rewound, but is not seekable'); |
| 114 | + $upload->moveTo($this->target); |
75 | 115 | } |
76 | 116 | } |
0 commit comments