public function testMovesFileToDesignatedPath() { $stream = new Stream('php://temp', 'wb+'); $stream->write('Foo bar!'); $target = $this->tmp; $upload = new Upload($stream); $upload->moveTo($target); $this->assertTrue(file_exists($target)); $this->assertEquals($stream->__toString(), file_get_contents($target)); }
/** * Temporary method for db interaction * * @param Db $db * @return Response */ public function databaseAction(Db $db) { $db->query('CREATE TABLE users(name varchar(255));'); $db->query('INSERT INTO users VALUES (?)', ['matthew']); $results = $db->first("SELECT * FROM users WHERE name = ?", ['matthew']); /** Psr7 example */ $stream = new Stream('php://memory', 'wb+'); $stream->write(json_encode($results)); return (new Response())->withStatus(200)->withBody($stream)->withHeader('Content-Type', 'application/json'); }
public function testGetMetadataReturnsNullWithInvalidKey() { $this->assertNull($this->stream->getMetadata('foobar')); }
/** * {@inheritdoc} * * @see http://php.net/is_uploaded_file * @see http://php.net/move_uploaded_file * @param string $targetPath Path to which to move the uploaded file. * @throws \InvalidArgumentException if the $path specified is invalid. * @throws \RuntimeException on any error during the move operation, or on * the second or subsequent call to the method. */ public function moveTo($targetPath) { if ($this->moved === true) { throw new RuntimeException('This file has already been moved.'); } if (is_writable($targetPath) === false) { throw new InvalidArgumentException('Unable to write to target path'); } if (strpos(PHP_SAPI, 'cli') === 0) { $stream = new Stream($targetPath, 'wb+'); $this->getStream()->rewind(); $stream->write($this->stream->getContents()); } else { // @codeCoverageIgnoreStart if (move_uploaded_file($this->stream, $targetPath) === false) { throw new RuntimeException('There was a problem moving your uploaded file.'); } // @codeCoverageIgnoreEnd } $this->moved = true; }
/** * @param $body * @param $code * @return Response */ public static function create($body = '', $code = 200) { $stream = new Stream('php://memory', 'wb+'); $stream->write($body); return (new static())->withStatus($code)->withBody($stream); }