Example #1
0
 /**
  * Creates a new instance of StreamInterface for the given body contents
  *
  * @param ReadableStreamInterface|string $body
  * @return StreamInterface
  */
 public function body($body)
 {
     if ($body instanceof ReadableStreamInterface) {
         return new ReadableBodyStream($body);
     }
     return RingCentral\Psr7\stream_for($body);
 }
Example #2
0
 public function testInflatesStreams()
 {
     $content = gzencode('test');
     $a = Psr7\stream_for($content);
     $b = new InflateStream($a);
     $this->assertEquals('test', (string) $b);
 }
Example #3
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Cannot write to a non-writable stream
  */
 public function testHandlesClose()
 {
     $s = Psr7\stream_for('foo');
     $wrapped = new NoSeekStream($s);
     $wrapped->close();
     $wrapped->write('foo');
 }
 public function setUp()
 {
     $this->c = fopen('php://temp', 'r+');
     fwrite($this->c, 'foo');
     fseek($this->c, 0);
     $this->a = Psr7\stream_for($this->c);
     $this->b = new Str($this->a);
 }
Example #5
0
 public function testDecoratesWithCustomizations()
 {
     $called = false;
     $a = Psr7\stream_for('foo');
     $b = FnStream::decorate($a, array('read' => function ($len) use(&$called, $a) {
         $called = true;
         return $a->read($len);
     }));
     $this->assertEquals('foo', $b->read(3));
     $this->assertTrue($called);
 }
Example #6
0
 public function testResource()
 {
     $stream = Psr7\stream_for('foo');
     $handle = StreamWrapper::getResource($stream);
     $this->assertSame('foo', fread($handle, 3));
     $this->assertSame(3, ftell($handle));
     $this->assertSame(3, fwrite($handle, 'bar'));
     $this->assertSame(0, fseek($handle, 0));
     $this->assertSame('foobar', fread($handle, 6));
     $this->assertSame('', fread($handle, 1));
     $this->assertTrue(feof($handle));
     // This fails on HHVM for some reason
     if (!defined('HHVM_VERSION')) {
         $this->assertEquals(array('dev' => 0, 'ino' => 0, 'mode' => 33206, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 6, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0, 0 => 0, 1 => 0, 2 => 33206, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 6, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0), fstat($handle));
     }
     $this->assertTrue(fclose($handle));
     $this->assertSame('foobar', (string) $stream);
 }
Example #7
0
 public function testDescribesCapabilities()
 {
     $p = Psr7\stream_for(function () {
     });
     $this->assertTrue($p->isReadable());
     $this->assertFalse($p->isSeekable());
     $this->assertFalse($p->isWritable());
     $this->assertNull($p->getSize());
     $this->assertEquals('', $p->getContents());
     $this->assertEquals('', (string) $p);
     $p->close();
     $this->assertEquals('', $p->read(10));
     $this->assertTrue($p->eof());
     try {
         $this->assertFalse($p->write('aa'));
         $this->fail();
     } catch (\RuntimeException $e) {
     }
 }
Example #8
0
 public function testCanDetermineSizeFromMultipleStreams()
 {
     $a = new AppendStream(array(Psr7\stream_for('foo'), Psr7\stream_for('bar')));
     $this->assertEquals(6, $a->getSize());
     $s = $this->getMockBuilder('Psr\\Http\\Message\\StreamInterface')->setMethods(array('isSeekable', 'isReadable'))->getMockForAbstractClass();
     $s->expects($this->once())->method('isSeekable')->will($this->returnValue(null));
     $s->expects($this->once())->method('isReadable')->will($this->returnValue(true));
     $a->addStream($s);
     $this->assertNull($a->getSize());
 }
Example #9
0
 public function testNewInstanceWhenNewBody()
 {
     $r = new Response(200, array(), 'foo');
     $b2 = Psr7\stream_for('abc');
     $this->assertNotSame($r, $r->withBody($b2));
 }
Example #10
0
 /**
  * @expectedException \RuntimeException
  */
 public function testThrowsWhenBodyCannotBeRewound()
 {
     $body = Psr7\stream_for('abc');
     $body->read(1);
     $body = FnStream::decorate($body, array('rewind' => function () {
         throw new \RuntimeException('a');
     }));
     $res = new Psr7\Response(200, array(), $body);
     Psr7\rewind_body($res);
 }
Example #11
0
 public function testClosesBothStreams()
 {
     $s = fopen('php://temp', 'r');
     $a = Psr7\stream_for($s);
     $d = new CachingStream($a);
     $d->close();
     $this->assertFalse(is_resource($s));
 }
Example #12
0
    public function testSerializesFilesWithCustomHeadersAndMultipleValues()
    {
        $f1 = Psr7\FnStream::decorate(Psr7\stream_for('foo'), array('getMetadata' => function () {
            return '/foo/bar.txt';
        }));
        $f2 = Psr7\FnStream::decorate(Psr7\stream_for('baz'), array('getMetadata' => function () {
            return '/foo/baz.jpg';
        }));
        $b = new MultipartStream(array(array('name' => 'foo', 'contents' => $f1, 'headers' => array('x-foo' => 'bar', 'content-disposition' => 'custom')), array('name' => 'foo', 'contents' => $f2, 'headers' => array('cOntenT-Type' => 'custom'))), 'boundary');
        $expected = <<<EOT
--boundary
x-foo: bar
content-disposition: custom
Content-Length: 3
Content-Type: text/plain

foo
--boundary
cOntenT-Type: custom
Content-Disposition: form-data; name="foo"; filename="baz.jpg"
Content-Length: 3

baz
--boundary--

EOT;
        $this->assertEquals($expected, str_replace("\r", '', $b));
    }
Example #13
0
 public function testLengthLessOffsetWhenNoLimitSize()
 {
     $a = Psr7\stream_for('foo_bar');
     $b = new LimitStream($a, -1, 4);
     $this->assertEquals(3, $b->getSize());
 }