/** * Handle body to stream object. * * @param string $body The body data. * * @return StreamInterface Converted to stream object. */ protected function handleBody($body) { $stream = new Stream('php://memory', 'rw+'); $stream->write($body); $stream->rewind(); return $stream; }
/** * Handle body to stream object. * * @param string $body The body data. * * @return StreamInterface Converted to stream object. */ protected function handleBody($body) { if (is_string($body)) { $stream = new Stream('php://temp', 'wb+'); $stream->write($body); $stream->rewind(); $body = $stream; } if (!$body instanceof StreamInterface) { throw new \InvalidArgumentException(sprintf('Invalid body content type %s, please provide string or StreamInterface', gettype($body))); } return $body; }
/** * Method to test getMetadata(). * * @return void * * @covers \Windwalker\Http\Stream::getMetadata */ public function testGetMetadata() { $this->createTempFile(); $resource = fopen($this->tmpnam, Stream::MODE_READ_WRITE_FROM_BEGIN); $this->instance->attach($resource); $this->assertEquals(stream_get_meta_data($resource), $this->instance->getMetadata()); $this->assertEquals(Stream::MODE_READ_WRITE_FROM_BEGIN, $this->instance->getMetadata('mode')); fclose($resource); }
/** * Read data from the stream. * * @param int $length Read up to $length bytes from the object and return * them. Fewer than $length bytes may be returned if underlying stream * call returns fewer bytes. * * @return string Returns the data read from the stream, or an empty string * if no bytes are available. * * @throws \RuntimeException if an error occurs. */ public function read($length) { $content = parent::read($length); if ($content && !static::$reachedEof) { static::$cache .= $content; } if ($this->eof()) { $this->reachedEof = true; } return $content; }
/** * testMoveInNotCli * * @return void */ public function testMoveInNotCli() { // Send stream $stream = new Stream('php://temp', 'wb+'); $stream->write('Foo bar!'); $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK); $upload->setSapi('cgi'); $this->tmpFile = $to = sys_get_temp_dir() . '/windwalker-' . uniqid(); $this->assertFalse(is_file($to)); $upload->moveTo($to); $this->assertTrue(is_file($to)); $contents = file_get_contents($to); $this->assertEquals($stream->__toString(), $contents); // Send string $uploadFile = sys_get_temp_dir() . '/upload-' . uniqid(); file_put_contents($uploadFile, 'Foo bar!'); $upload = new UploadedFile($uploadFile, 0, UPLOAD_ERR_OK); $upload->setSapi('cgi'); $this->tmpFile = $to = sys_get_temp_dir() . '/windwalker-' . uniqid(); $this->assertFalse(is_file($to)); $this->assertExpectedException(function () use($upload, $to) { $upload->moveTo($to); }, 'RuntimeException', 'Error moving uploaded file'); }
/** * Method to test copyFrom(). * * @return void * * @covers \Windwalker\Http\Helper\StreamHelper::copyFrom */ public function testCopyFrom() { StreamHelper::copyFrom(__FILE__, $dest = new Stream('php://memory', Stream::MODE_READ_WRITE_FROM_BEGIN)); $this->assertEquals(file_get_contents(__FILE__), $dest->__toString()); }