PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/api/vendor/guzzle/guzzle/tests/Guzzle/Tests/Http/CachingEntityBodyTest.php

https://gitlab.com/x33n/respond
PHP | 249 lines | 195 code | 30 blank | 24 comment | 0 complexity | af3bcef364f1dff4c939d80db749dbad MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Http;
  3. use Guzzle\Http\EntityBody;
  4. use Guzzle\Http\CachingEntityBody;
  5. /**
  6. * @covers Guzzle\Http\CachingEntityBody
  7. */
  8. class CachingEntityBodyTest extends \Guzzle\Tests\GuzzleTestCase
  9. {
  10. /** @var CachingEntityBody */
  11. protected $body;
  12. /** @var EntityBody */
  13. protected $decorated;
  14. public function setUp()
  15. {
  16. $this->decorated = EntityBody::factory('testing');
  17. $this->body = new CachingEntityBody($this->decorated);
  18. }
  19. public function testUsesRemoteSizeIfPossible()
  20. {
  21. $body = EntityBody::factory('test');
  22. $caching = new CachingEntityBody($body);
  23. $this->assertEquals(4, $caching->getSize());
  24. $this->assertEquals(4, $caching->getContentLength());
  25. }
  26. /**
  27. * @expectedException \Guzzle\Common\Exception\RuntimeException
  28. * @expectedExceptionMessage does not support custom stream rewind
  29. */
  30. public function testDoesNotAllowRewindFunction()
  31. {
  32. $this->body->setRewindFunction(true);
  33. }
  34. /**
  35. * @expectedException \Guzzle\Common\Exception\RuntimeException
  36. * @expectedExceptionMessage Cannot seek to byte 10
  37. */
  38. public function testCannotSeekPastWhatHasBeenRead()
  39. {
  40. $this->body->seek(10);
  41. }
  42. /**
  43. * @expectedException \Guzzle\Common\Exception\RuntimeException
  44. * @expectedExceptionMessage supports only SEEK_SET and SEEK_CUR
  45. */
  46. public function testCannotUseSeekEnd()
  47. {
  48. $this->body->seek(2, SEEK_END);
  49. }
  50. public function testChangingUnderlyingStreamUpdatesSizeAndStream()
  51. {
  52. $size = filesize(__FILE__);
  53. $s = fopen(__FILE__, 'r');
  54. $this->body->setStream($s, $size);
  55. $this->assertEquals($size, $this->body->getSize());
  56. $this->assertEquals($size, $this->decorated->getSize());
  57. $this->assertSame($s, $this->body->getStream());
  58. $this->assertSame($s, $this->decorated->getStream());
  59. }
  60. public function testRewindUsesSeek()
  61. {
  62. $a = EntityBody::factory('foo');
  63. $d = $this->getMockBuilder('Guzzle\Http\CachingEntityBody')
  64. ->setMethods(array('seek'))
  65. ->setConstructorArgs(array($a))
  66. ->getMock();
  67. $d->expects($this->once())
  68. ->method('seek')
  69. ->with(0)
  70. ->will($this->returnValue(true));
  71. $d->rewind();
  72. }
  73. public function testCanSeekToReadBytes()
  74. {
  75. $this->assertEquals('te', $this->body->read(2));
  76. $this->body->seek(0);
  77. $this->assertEquals('test', $this->body->read(4));
  78. $this->assertEquals(4, $this->body->ftell());
  79. $this->body->seek(2);
  80. $this->assertEquals(2, $this->body->ftell());
  81. $this->body->seek(2, SEEK_CUR);
  82. $this->assertEquals(4, $this->body->ftell());
  83. $this->assertEquals('ing', $this->body->read(3));
  84. }
  85. public function testWritesToBufferStream()
  86. {
  87. $this->body->read(2);
  88. $this->body->write('hi');
  89. $this->body->rewind();
  90. $this->assertEquals('tehiing', (string) $this->body);
  91. }
  92. public function testReadLinesFromBothStreams()
  93. {
  94. $this->body->seek($this->body->ftell());
  95. $this->body->write("test\n123\nhello\n1234567890\n");
  96. $this->body->rewind();
  97. $this->assertEquals("test\n", $this->body->readLine(7));
  98. $this->assertEquals("123\n", $this->body->readLine(7));
  99. $this->assertEquals("hello\n", $this->body->readLine(7));
  100. $this->assertEquals("123456", $this->body->readLine(7));
  101. $this->assertEquals("7890\n", $this->body->readLine(7));
  102. // We overwrote the decorated stream, so no more data
  103. $this->assertEquals('', $this->body->readLine(7));
  104. }
  105. public function testSkipsOverwrittenBytes()
  106. {
  107. $decorated = EntityBody::factory(
  108. implode("\n", array_map(function ($n) {
  109. return str_pad($n, 4, '0', STR_PAD_LEFT);
  110. }, range(0, 25)))
  111. );
  112. $body = new CachingEntityBody($decorated);
  113. $this->assertEquals("0000\n", $body->readLine());
  114. $this->assertEquals("0001\n", $body->readLine());
  115. // Write over part of the body yet to be read, so skip some bytes
  116. $this->assertEquals(5, $body->write("TEST\n"));
  117. $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
  118. // Read, which skips bytes, then reads
  119. $this->assertEquals("0003\n", $body->readLine());
  120. $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
  121. $this->assertEquals("0004\n", $body->readLine());
  122. $this->assertEquals("0005\n", $body->readLine());
  123. // Overwrite part of the cached body (so don't skip any bytes)
  124. $body->seek(5);
  125. $this->assertEquals(5, $body->write("ABCD\n"));
  126. $this->assertEquals(0, $this->readAttribute($body, 'skipReadBytes'));
  127. $this->assertEquals("TEST\n", $body->readLine());
  128. $this->assertEquals("0003\n", $body->readLine());
  129. $this->assertEquals("0004\n", $body->readLine());
  130. $this->assertEquals("0005\n", $body->readLine());
  131. $this->assertEquals("0006\n", $body->readLine());
  132. $this->assertEquals(5, $body->write("1234\n"));
  133. $this->assertEquals(5, $this->readAttribute($body, 'skipReadBytes'));
  134. // Seek to 0 and ensure the overwritten bit is replaced
  135. $body->rewind();
  136. $this->assertEquals("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", $body->read(50));
  137. // Ensure that casting it to a string does not include the bit that was overwritten
  138. $this->assertContains("0000\nABCD\nTEST\n0003\n0004\n0005\n0006\n1234\n0008\n0009\n", (string) $body);
  139. }
  140. public function testWrapsContentType()
  141. {
  142. $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
  143. ->setMethods(array('getContentType'))
  144. ->setConstructorArgs(array(fopen(__FILE__, 'r')))
  145. ->getMock();
  146. $a->expects($this->once())
  147. ->method('getContentType')
  148. ->will($this->returnValue('foo'));
  149. $d = new CachingEntityBody($a);
  150. $this->assertEquals('foo', $d->getContentType());
  151. }
  152. public function testWrapsContentEncoding()
  153. {
  154. $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
  155. ->setMethods(array('getContentEncoding'))
  156. ->setConstructorArgs(array(fopen(__FILE__, 'r')))
  157. ->getMock();
  158. $a->expects($this->once())
  159. ->method('getContentEncoding')
  160. ->will($this->returnValue('foo'));
  161. $d = new CachingEntityBody($a);
  162. $this->assertEquals('foo', $d->getContentEncoding());
  163. }
  164. public function testWrapsMetadata()
  165. {
  166. $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
  167. ->setMethods(array('getMetadata', 'getWrapper', 'getWrapperData', 'getStreamType', 'getUri'))
  168. ->setConstructorArgs(array(fopen(__FILE__, 'r')))
  169. ->getMock();
  170. $a->expects($this->once())
  171. ->method('getMetadata')
  172. ->will($this->returnValue(array()));
  173. // Called twice for getWrapper and getWrapperData
  174. $a->expects($this->exactly(1))
  175. ->method('getWrapper')
  176. ->will($this->returnValue('wrapper'));
  177. $a->expects($this->once())
  178. ->method('getWrapperData')
  179. ->will($this->returnValue(array()));
  180. $a->expects($this->once())
  181. ->method('getStreamType')
  182. ->will($this->returnValue('baz'));
  183. $a->expects($this->once())
  184. ->method('getUri')
  185. ->will($this->returnValue('path/to/foo'));
  186. $d = new CachingEntityBody($a);
  187. $this->assertEquals(array(), $d->getMetaData());
  188. $this->assertEquals('wrapper', $d->getWrapper());
  189. $this->assertEquals(array(), $d->getWrapperData());
  190. $this->assertEquals('baz', $d->getStreamType());
  191. $this->assertEquals('path/to/foo', $d->getUri());
  192. }
  193. public function testWrapsCustomData()
  194. {
  195. $a = $this->getMockBuilder('Guzzle\Http\EntityBody')
  196. ->setMethods(array('getCustomData', 'setCustomData'))
  197. ->setConstructorArgs(array(fopen(__FILE__, 'r')))
  198. ->getMock();
  199. $a->expects($this->exactly(1))
  200. ->method('getCustomData')
  201. ->with('foo')
  202. ->will($this->returnValue('bar'));
  203. $a->expects($this->exactly(1))
  204. ->method('setCustomData')
  205. ->with('foo', 'bar')
  206. ->will($this->returnSelf());
  207. $d = new CachingEntityBody($a);
  208. $this->assertSame($d, $d->setCustomData('foo', 'bar'));
  209. $this->assertEquals('bar', $d->getCustomData('foo'));
  210. }
  211. public function testClosesBothStreams()
  212. {
  213. $s = fopen('php://temp', 'r');
  214. $a = EntityBody::factory($s);
  215. $d = new CachingEntityBody($a);
  216. $d->close();
  217. $this->assertFalse(is_resource($s));
  218. }
  219. }