LazyOpenStreamTest.php 1.8 KB

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