LazyOpenStreamTest.php 1.8 KB

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