UtilsTest.php 5.0 KB

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