Esempio n. 1
0
 /**
  * Create a JSON response with the given data.
  *
  * Default JSON encoding is performed with the following options, which
  * produces RFC4627-compliant JSON, capable of embedding into HTML.
  *
  * - JSON_HEX_TAG
  * - JSON_HEX_APOS
  * - JSON_HEX_AMP
  * - JSON_HEX_QUOT
  * - JSON_UNESCAPED_SLASHES
  *
  * @param mixed $data            Data to convert to JSON.
  * @param int   $status          Integer status code for the response; 200 by default.
  * @param array $headers         Array of headers to use at initialization.
  * @param int   $encodingOptions JSON encoding options to use.
  *
  * @throws InvalidArgumentException if unable to encode the $data to JSON.
  */
 public function __construct($data, int $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS)
 {
     $body = new Stream(fopen('php://temp', 'wb+'));
     $body->write($this->jsonEncode($data, $encodingOptions));
     $body->rewind();
     $headers = $this->injectContentType('application/json', $headers);
     parent::__construct($status, $headers, $body);
 }
Esempio n. 2
0
 /**
  * Create the message body.
  *
  * @param string|StreamInterface $html
  *
  * @throws InvalidArgumentException if $html is neither a string or stream.
  *
  * @return StreamInterface
  */
 private function createBody($html) : StreamInterface
 {
     if ($html instanceof StreamInterface) {
         return $html;
     }
     if (!is_string($html)) {
         throw new InvalidArgumentException(sprintf('Invalid content (%s) provided to %s', is_object($html) ? get_class($html) : gettype($html), __CLASS__));
     }
     $body = new Stream(fopen('php://temp', 'wb+'));
     $body->write($html);
     $body->rewind();
     return $body;
 }
Esempio n. 3
0
 public function testCloseClearProperties()
 {
     $handle = fopen('php://temp', 'r+');
     $stream = new Stream($handle);
     $stream->close();
     $this->assertFalse($stream->isSeekable());
     $this->assertFalse($stream->isReadable());
     $this->assertFalse($stream->isWritable());
     $this->assertNull($stream->getSize());
     $this->assertEmpty($stream->getMetadata());
 }
Esempio n. 4
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Could not seek to stream offset 2
  */
 public function testThrowsWhenCurrentGreaterThanOffsetSeek()
 {
     $body = 'foo_bar';
     $stream = fopen('php://temp', 'r+');
     fwrite($stream, $body);
     fseek($stream, 0);
     $stream1 = new Stream($stream);
     $stream2 = new NoSeekStream($stream1);
     $stream3 = new LimitStream($stream2);
     $stream1->getContents();
     $stream3->setOffset(2);
 }
Esempio n. 5
0
 public function testSuccessful()
 {
     $body = 'Foo bar!';
     $stream = fopen('php://temp', 'r+');
     fwrite($stream, $body);
     fseek($stream, 0);
     $stream = new Stream($stream);
     $upload = new UploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, 'filename.txt', 'text/plain');
     $this->assertEquals($stream->getSize(), $upload->getSize());
     $this->assertEquals('filename.txt', $upload->getClientFilename());
     $this->assertEquals('text/plain', $upload->getClientMediaType());
     $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'successful');
     $upload->moveTo($to);
     $this->assertFileExists($to);
     $this->assertEquals($stream->__toString(), file_get_contents($to));
 }