1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Psr7;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
use RuntimeException;
use Slim\Psr7\Stream;
use function fopen;
use function popen;
use function stream_get_contents;
use function trim;
class StreamTest extends TestCase
{
/**
* @var resource pipe stream file handle
*/
private $pipeFh;
private Stream $pipeStream;
public function tearDown(): void
{
if ($this->pipeFh != null) {
// prevent broken pipe error message
stream_get_contents($this->pipeFh);
}
}
protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
{
// only if PHP version < 8.1
if (PHP_VERSION_ID > 80100) {
return;
}
$property->setAccessible($accessible);
}
public function testIsPipe()
{
$this->openPipeStream();
$this->assertTrue($this->pipeStream->isPipe());
$this->pipeStream->detach();
$this->assertFalse($this->pipeStream->isPipe());
$fhFile = fopen(__FILE__, 'r');
$fileStream = new Stream($fhFile);
$this->assertFalse($fileStream->isPipe());
}
public function testIsPipeReadable()
{
$this->openPipeStream();
$this->assertTrue($this->pipeStream->isReadable());
}
public function testPipeIsNotSeekable()
{
$this->openPipeStream();
$this->assertFalse($this->pipeStream->isSeekable());
}
public function testCannotSeekPipe()
{
$this->expectException(RuntimeException::class);
$this->openPipeStream();
$this->pipeStream->seek(0);
}
public function testCannotTellPipe()
{
$this->expectException(RuntimeException::class);
$this->openPipeStream();
$this->pipeStream->tell();
}
public function testCannotRewindPipe()
{
$this->expectException(RuntimeException::class);
$this->openPipeStream();
$this->pipeStream->rewind();
}
public function testPipeGetSizeYieldsNull()
{
$this->openPipeStream();
$this->assertNull($this->pipeStream->getSize());
}
public function testClosePipe()
{
$this->openPipeStream();
// prevent broken pipe error message
stream_get_contents($this->pipeFh);
$this->pipeStream->close();
$this->pipeFh = null;
$this->assertFalse($this->pipeStream->isPipe());
}
public function testPipeToString()
{
$this->openPipeStream();
$content = trim((string) $this->pipeStream);
$this->assertSame('12', $content);
}
public function testConvertsToStringPartiallyReadNonSeekableStream()
{
$this->openPipeStream();
$head = $this->pipeStream->read(1);
$tail = trim((string) $this->pipeStream);
$this->assertSame('1', $head);
$this->assertSame('2', $tail);
}
public function testPipeGetContents()
{
$this->openPipeStream();
$contents = trim($this->pipeStream->getContents());
$this->assertSame('12', $contents);
}
public function testIsWriteable()
{
$resource = fopen('php://temp', 'w');
$stream = new Stream($resource);
$this->assertEquals(13, $stream->write('Hello, world!'));
$this->assertTrue($stream->isWritable());
}
public function testIsReadable()
{
$resource = fopen('php://temp', 'r');
$stream = new Stream($resource);
$this->assertTrue($stream->isReadable());
$this->assertFalse($stream->isWritable());
}
public function testIsWritableAndReadable()
{
$resource = fopen('php://temp', 'w+');
$stream = new Stream($resource);
$stream->write('Hello, world!');
$this->assertEquals('Hello, world!', $stream);
$this->assertTrue($stream->isWritable());
$this->assertTrue($stream->isReadable());
}
/**
* Test that a call to the protected method `attach` would invoke `detach`.
*
* @throws ReflectionException
*/
public function testAttachAgain()
{
$this->openPipeStream();
$stream = $this->createMock(Stream::class);
$stream->expects($this->once())
->method('detach');
$streamProperty = new ReflectionProperty(Stream::class, 'stream');
$this->setAccessible($streamProperty);
$streamProperty->setValue($stream, $this->pipeFh);
$attachMethod = new ReflectionMethod(Stream::class, 'attach');
$this->setAccessible($attachMethod);
$attachMethod->invoke($stream, $this->pipeFh);
}
public function testGetMetaDataReturnsNullIfStreamIsDetached()
{
$resource = fopen('php://temp', 'rw+');
$stream = new Stream($resource);
$stream->detach();
$this->assertNull($stream->getMetadata());
}
private function openPipeStream()
{
$this->pipeFh = popen('echo 12', 'r');
$this->pipeStream = new Stream($this->pipeFh);
}
public function testReadOnlyCachedStreamsAreDisallowed()
{
$resource = fopen('php://temp', 'w+');
$cache = new Stream(fopen('php://temp', 'r'));
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cache stream must be seekable and writable');
new Stream($resource, $cache);
}
public function testNonSeekableCachedStreamsAreDisallowed()
{
$resource = fopen('php://temp', 'w+');
$cache = new Stream(fopen('php://output', 'w'));
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cache stream must be seekable and writable');
new Stream($resource, $cache);
}
public function testCachedStreamsGetsContentFromTheCache()
{
$resource = popen('echo HelloWorld', 'r');
$stream = new Stream($resource, new Stream(fopen('php://temp', 'w+')));
$this->assertEquals("HelloWorld\n", $stream->getContents());
$this->assertEquals("HelloWorld\n", $stream->getContents());
}
public function testCachedStreamsFillsCacheOnRead()
{
$resource = fopen('data://,0', 'r');
$stream = new Stream($resource, new Stream(fopen('php://temp', 'w+')));
$this->assertEquals("0", $stream->read(100));
$this->assertEquals("0", $stream->__toString());
}
public function testDetachingStreamDropsCache()
{
$cache = new Stream(fopen('php://temp', 'w+'));
$resource = fopen('data://,foo', 'r');
$stream = new Stream($resource, $cache);
$stream->detach();
$cacheProperty = new ReflectionProperty(Stream::class, 'cache');
$this->setAccessible($cacheProperty);
$finishedProperty = new ReflectionProperty(Stream::class, 'finished');
$this->setAccessible($finishedProperty);
$this->assertNull($cacheProperty->getValue($stream));
$this->assertFalse($finishedProperty->getValue($stream));
}
public function testCachedStreamsRewindIfFinishedOnToString()
{
$resource = fopen('data://,foo', 'r');
$stream = new Stream($resource, new Stream(fopen('php://temp', 'w+')));
$this->assertEquals('foo', (string)$stream);
$this->assertEquals('foo', (string)$stream);
}
}
|