UtilsTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\nbaz\nbar");
  69. $this->assertEquals("foo\n", Utils::readline($s));
  70. $this->assertEquals("baz\n", Utils::readline($s));
  71. $this->assertEquals("bar", Utils::readline($s));
  72. }
  73. public function testReadsLinesUpToMaxLength()
  74. {
  75. $s = Stream::factory("12345\n");
  76. $this->assertEquals("123", Utils::readline($s, 4));
  77. $this->assertEquals("45\n", Utils::readline($s));
  78. }
  79. public function testReadsLineUntilFalseReturnedFromRead()
  80. {
  81. $s = $this->getMockBuilder('GuzzleHttp\Stream\Stream')
  82. ->setMethods(['read', 'eof'])
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $s->expects($this->exactly(2))
  86. ->method('read')
  87. ->will($this->returnCallback(function () {
  88. static $c = false;
  89. if ($c) {
  90. return false;
  91. }
  92. $c = true;
  93. return 'h';
  94. }));
  95. $s->expects($this->exactly(2))
  96. ->method('eof')
  97. ->will($this->returnValue(false));
  98. $this->assertEquals("h", Utils::readline($s));
  99. }
  100. public function testCalculatesHash()
  101. {
  102. $s = Stream::factory('foobazbar');
  103. $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
  104. }
  105. /**
  106. * @expectedException \GuzzleHttp\Stream\Exception\SeekException
  107. */
  108. public function testCalculatesHashThrowsWhenSeekFails()
  109. {
  110. $s = new NoSeekStream(Stream::factory('foobazbar'));
  111. $s->read(2);
  112. Utils::hash($s, 'md5');
  113. }
  114. public function testCalculatesHashSeeksToOriginalPosition()
  115. {
  116. $s = Stream::factory('foobazbar');
  117. $s->seek(4);
  118. $this->assertEquals(md5('foobazbar'), Utils::hash($s, 'md5'));
  119. $this->assertEquals(4, $s->tell());
  120. }
  121. public function testOpensFilesSuccessfully()
  122. {
  123. $r = Utils::open(__FILE__, 'r');
  124. $this->assertInternalType('resource', $r);
  125. fclose($r);
  126. }
  127. /**
  128. * @expectedException \RuntimeException
  129. * @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r
  130. */
  131. public function testThrowsExceptionNotWarning()
  132. {
  133. Utils::open('/path/to/does/not/exist', 'r');
  134. }
  135. public function testProxiesToFactory()
  136. {
  137. $this->assertEquals('foo', (string) Utils::create('foo'));
  138. }
  139. }