UtilsTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace GuzzleHttp\Tests\Stream;
  3. use GuzzleHttp\Stream\Exception\SeekException;
  4. use GuzzleHttp\Stream\FnStream;
  5. use GuzzleHttp\Stream\NoSeekStream;
  6. use GuzzleHttp\Stream\Stream;
  7. use GuzzleHttp\Stream\Utils;
  8. use PHPUnit\Framework\TestCase;
  9. use RuntimeException;
  10. class UtilsTest extends TestCase
  11. {
  12. public function testCopiesToString()
  13. {
  14. $s = Stream::factory('foobaz');
  15. $this->assertEquals('foobaz', Utils::copyToString($s));
  16. $s->seek(0);
  17. $this->assertEquals('foo', Utils::copyToString($s, 3));
  18. $this->assertEquals('baz', Utils::copyToString($s, 3));
  19. $this->assertEquals('', Utils::copyToString($s));
  20. }
  21. public function testCopiesToStringStopsWhenReadFails()
  22. {
  23. $s1 = Stream::factory('foobaz');
  24. $s1 = FnStream::decorate($s1, [
  25. 'read' => function () {
  26. return false;
  27. }
  28. ]);
  29. $result = Utils::copyToString($s1);
  30. $this->assertEquals('', $result);
  31. }
  32. public function testCopiesToStream()
  33. {
  34. $s1 = Stream::factory('foobaz');
  35. $s2 = Stream::factory('');
  36. Utils::copyToStream($s1, $s2);
  37. $this->assertEquals('foobaz', (string) $s2);
  38. $s2 = Stream::factory('');
  39. $s1->seek(0);
  40. Utils::copyToStream($s1, $s2, 3);
  41. $this->assertEquals('foo', (string) $s2);
  42. Utils::copyToStream($s1, $s2, 3);
  43. $this->assertEquals('foobaz', (string) $s2);
  44. }
  45. public function testStopsCopyToStreamWhenWriteFails()
  46. {
  47. $s1 = Stream::factory('foobaz');
  48. $s2 = Stream::factory('');
  49. $s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
  50. Utils::copyToStream($s1, $s2);
  51. $this->assertEquals('', (string) $s2);
  52. }
  53. public function testStopsCopyToSteamWhenWriteFailsWithMaxLen()
  54. {
  55. $s1 = Stream::factory('foobaz');
  56. $s2 = Stream::factory('');
  57. $s2 = FnStream::decorate($s2, ['write' => function () { return 0; }]);
  58. Utils::copyToStream($s1, $s2, 10);
  59. $this->assertEquals('', (string) $s2);
  60. }
  61. public function testStopsCopyToSteamWhenReadFailsWithMaxLen()
  62. {
  63. $s1 = Stream::factory('foobaz');
  64. $s1 = FnStream::decorate($s1, ['read' => function () { return ''; }]);
  65. $s2 = Stream::factory('');
  66. Utils::copyToStream($s1, $s2, 10);
  67. $this->assertEquals('', (string) $s2);
  68. }
  69. public function testReadsLines()
  70. {
  71. $s = Stream::factory("foo" . PHP_EOL . "baz" . PHP_EOL . "bar");
  72. $this->assertEquals("foo" . PHP_EOL, Utils::readline($s));
  73. $this->assertEquals("baz" . PHP_EOL, Utils::readline($s));
  74. $this->assertEquals("bar", Utils::readline($s));
  75. }
  76. public function testReadsLinesUpToMaxLength()
  77. {
  78. $s = Stream::factory("12345" . PHP_EOL);
  79. $this->assertEquals("123", Utils::readline($s, 3));
  80. $this->assertEquals("45" . PHP_EOL, Utils::readline($s));
  81. }
  82. public function testReadsLinesWithCustomEol()
  83. {
  84. $s = Stream::factory("foo\tbaz\t\tbar");
  85. $this->assertEquals("foo\tbaz\t\t", Utils::readline($s, null, "\t\t"));
  86. $this->assertEquals("bar", Utils::readline($s));
  87. }
  88. public function testReadsLineUntilFalseReturnedFromRead()
  89. {
  90. $s = $this->getMockBuilder('GuzzleHttp\Stream\Stream')
  91. ->setMethods(['read', 'eof'])
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $s->expects($this->exactly(2))
  95. ->method('read')
  96. ->will($this->returnCallback(function () {
  97. static $c = false;
  98. if ($c) {
  99. return false;
  100. }
  101. $c = true;
  102. return 'h';
  103. }));
  104. $s->expects($this->exactly(2))
  105. ->method('eof')
  106. ->will($this->returnValue(false));
  107. $this->assertEquals("h", Utils::readline($s));
  108. }
  109. public function testCalculatesHash()
  110. {
  111. $s = Stream::factory('foobazbar');
  112. $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
  113. }
  114. public function testCalculatesHashThrowsWhenSeekFails()
  115. {
  116. $this->expectException(SeekException::class);
  117. $s = new NoSeekStream(Stream::factory('foobazbar'));
  118. $s->read(2);
  119. Utils::hash($s, 'md5');
  120. }
  121. public function testCalculatesHashSeeksToOriginalPosition()
  122. {
  123. $s = Stream::factory('foobazbar');
  124. $s->seek(4);
  125. $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
  126. $this->assertEquals(4, $s->tell());
  127. }
  128. public function testOpensFilesSuccessfully()
  129. {
  130. $r = Utils::open(__FILE__, 'r');
  131. $this->assertIsResource($r);
  132. fclose($r);
  133. }
  134. public function testThrowsExceptionNotWarning()
  135. {
  136. $this->expectException(RuntimeException::class);
  137. $this->expectErrorMessage('Unable to open /path/to/does/not/exist using mode r');
  138. Utils::open('/path/to/does/not/exist', 'r');
  139. }
  140. public function testProxiesToFactory()
  141. {
  142. $this->assertEquals('foo', (string) Utils::create('foo'));
  143. }
  144. }