1<?php 2namespace GuzzleHttp\Tests\Stream; 3 4use GuzzleHttp\Stream\LazyOpenStream; 5 6class LazyOpenStreamTest extends \PHPUnit_Framework_TestCase 7{ 8 private $fname; 9 10 public function setup() 11 { 12 $this->fname = tempnam('/tmp', 'tfile'); 13 14 if (file_exists($this->fname)) { 15 unlink($this->fname); 16 } 17 } 18 19 public function tearDown() 20 { 21 if (file_exists($this->fname)) { 22 unlink($this->fname); 23 } 24 } 25 26 public function testOpensLazily() 27 { 28 $l = new LazyOpenStream($this->fname, 'w+'); 29 $l->write('foo'); 30 $this->assertInternalType('array', $l->getMetadata()); 31 $this->assertFileExists($this->fname); 32 $this->assertEquals('foo', file_get_contents($this->fname)); 33 $this->assertEquals('foo', (string) $l); 34 } 35 36 public function testProxiesToFile() 37 { 38 file_put_contents($this->fname, 'foo'); 39 $l = new LazyOpenStream($this->fname, 'r'); 40 $this->assertEquals('foo', $l->read(4)); 41 $this->assertTrue($l->eof()); 42 $this->assertEquals(3, $l->tell()); 43 $this->assertTrue($l->isReadable()); 44 $this->assertTrue($l->isSeekable()); 45 $this->assertFalse($l->isWritable()); 46 $l->seek(1); 47 $this->assertEquals('oo', $l->getContents()); 48 $this->assertEquals('foo', (string) $l); 49 $this->assertEquals(3, $l->getSize()); 50 $this->assertInternalType('array', $l->getMetadata()); 51 $l->close(); 52 } 53 54 public function testDetachesUnderlyingStream() 55 { 56 file_put_contents($this->fname, 'foo'); 57 $l = new LazyOpenStream($this->fname, 'r'); 58 $r = $l->detach(); 59 $this->assertInternalType('resource', $r); 60 fseek($r, 0); 61 $this->assertEquals('foo', stream_get_contents($r)); 62 fclose($r); 63 } 64} 65