PageRenderTime 51ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/api/tests/Http/BodyTest.php

https://gitlab.com/jatasya/testSlimfrmwk
PHP | 401 lines | 293 code | 81 blank | 27 comment | 3 complexity | a4278175cd4b2417aa72fbc4cdc75223 MD5 | raw file
  1. <?php
  2. /**
  3. * Slim Framework (http://slimframework.com)
  4. *
  5. * @link https://github.com/slimphp/Slim
  6. * @copyright Copyright (c) 2011-2015 Josh Lockhart
  7. * @license https://github.com/slimphp/Slim/blob/master/LICENSE.md (MIT License)
  8. */
  9. namespace Slim\Tests\Http;
  10. use ReflectionProperty;
  11. use Slim\Http\Body;
  12. class BodyTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var string
  16. */
  17. protected $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
  18. /**
  19. * @var resource
  20. */
  21. protected $stream;
  22. protected function tearDown()
  23. {
  24. if (is_resource($this->stream) === true) {
  25. fclose($this->stream);
  26. }
  27. }
  28. /**
  29. * This method creates a new resource, and it seeds
  30. * the resource with lorem ipsum text. The returned
  31. * resource is readable, writable, and seekable.
  32. *
  33. * @param string $mode
  34. *
  35. * @return resource
  36. */
  37. public function resourceFactory($mode = 'r+')
  38. {
  39. $stream = fopen('php://temp', $mode);
  40. fwrite($stream, $this->text);
  41. rewind($stream);
  42. return $stream;
  43. }
  44. public function testConstructorAttachesStream()
  45. {
  46. $this->stream = $this->resourceFactory();
  47. $body = new Body($this->stream);
  48. $bodyStream = new ReflectionProperty($body, 'stream');
  49. $bodyStream->setAccessible(true);
  50. $this->assertSame($this->stream, $bodyStream->getValue($body));
  51. }
  52. /**
  53. * @expectedException \InvalidArgumentException
  54. */
  55. public function testConstructorInvalidStream()
  56. {
  57. $this->stream = 'foo';
  58. $body = new Body($this->stream);
  59. }
  60. public function testGetMetadata()
  61. {
  62. $this->stream = $this->resourceFactory();
  63. $body = new Body($this->stream);
  64. $this->assertTrue(is_array($body->getMetadata()));
  65. }
  66. public function testGetMetadataKey()
  67. {
  68. $this->stream = $this->resourceFactory();
  69. $body = new Body($this->stream);
  70. $this->assertEquals('php://temp', $body->getMetadata('uri'));
  71. }
  72. public function testGetMetadataKeyNotFound()
  73. {
  74. $this->stream = $this->resourceFactory();
  75. $body = new Body($this->stream);
  76. $this->assertNull($body->getMetadata('foo'));
  77. }
  78. public function testDetach()
  79. {
  80. $this->stream = $this->resourceFactory();
  81. $body = new Body($this->stream);
  82. $bodyStream = new ReflectionProperty($body, 'stream');
  83. $bodyStream->setAccessible(true);
  84. $bodyMetadata = new ReflectionProperty($body, 'meta');
  85. $bodyMetadata->setAccessible(true);
  86. $bodyReadable = new ReflectionProperty($body, 'readable');
  87. $bodyReadable->setAccessible(true);
  88. $bodyWritable = new ReflectionProperty($body, 'writable');
  89. $bodyWritable->setAccessible(true);
  90. $bodySeekable = new ReflectionProperty($body, 'seekable');
  91. $bodySeekable->setAccessible(true);
  92. $result = $body->detach();
  93. $this->assertSame($this->stream, $result);
  94. $this->assertNull($bodyStream->getValue($body));
  95. $this->assertNull($bodyMetadata->getValue($body));
  96. $this->assertNull($bodyReadable->getValue($body));
  97. $this->assertNull($bodyWritable->getValue($body));
  98. $this->assertNull($bodySeekable->getValue($body));
  99. }
  100. public function testToStringAttached()
  101. {
  102. $this->stream = $this->resourceFactory();
  103. $body = new Body($this->stream);
  104. $this->assertEquals($this->text, (string)$body);
  105. }
  106. public function testToStringAttachedRewindsFirst()
  107. {
  108. $this->stream = $this->resourceFactory();
  109. $body = new Body($this->stream);
  110. $this->assertEquals($this->text, (string)$body);
  111. $this->assertEquals($this->text, (string)$body);
  112. $this->assertEquals($this->text, (string)$body);
  113. }
  114. public function testToStringDetached()
  115. {
  116. $this->stream = $this->resourceFactory();
  117. $body = new Body($this->stream);
  118. $bodyStream = new ReflectionProperty($body, 'stream');
  119. $bodyStream->setAccessible(true);
  120. $bodyStream->setValue($body, null);
  121. $this->assertEquals('', (string)$body);
  122. }
  123. public function testClose()
  124. {
  125. $this->stream = $this->resourceFactory();
  126. $body = new Body($this->stream);
  127. $body->close();
  128. $this->assertAttributeEquals(null, 'stream', $body);
  129. //$this->assertFalse($body->isAttached()); #1269
  130. }
  131. public function testGetSizeAttached()
  132. {
  133. $this->stream = $this->resourceFactory();
  134. $body = new Body($this->stream);
  135. $this->assertEquals(mb_strlen($this->text), $body->getSize());
  136. }
  137. public function testGetSizeDetached()
  138. {
  139. $this->stream = $this->resourceFactory();
  140. $body = new Body($this->stream);
  141. $bodyStream = new ReflectionProperty($body, 'stream');
  142. $bodyStream->setAccessible(true);
  143. $bodyStream->setValue($body, null);
  144. $this->assertNull($body->getSize());
  145. }
  146. public function testTellAttached()
  147. {
  148. $this->stream = $this->resourceFactory();
  149. $body = new Body($this->stream);
  150. fseek($this->stream, 10);
  151. $this->assertEquals(10, $body->tell());
  152. }
  153. public function testTellDetachedThrowsRuntimeException()
  154. {
  155. $this->stream = $this->resourceFactory();
  156. $body = new Body($this->stream);
  157. $bodyStream = new ReflectionProperty($body, 'stream');
  158. $bodyStream->setAccessible(true);
  159. $bodyStream->setValue($body, null);
  160. $this->setExpectedException('\RuntimeException');
  161. $body->tell();
  162. }
  163. public function testEofAttachedFalse()
  164. {
  165. $this->stream = $this->resourceFactory();
  166. $body = new Body($this->stream);
  167. fseek($this->stream, 10);
  168. $this->assertFalse($body->eof());
  169. }
  170. public function testEofAttachedTrue()
  171. {
  172. $this->stream = $this->resourceFactory();
  173. $body = new Body($this->stream);
  174. while (feof($this->stream) === false) {
  175. fread($this->stream, 1024);
  176. }
  177. $this->assertTrue($body->eof());
  178. }
  179. public function testEofDetached()
  180. {
  181. $this->stream = $this->resourceFactory();
  182. $body = new Body($this->stream);
  183. $bodyStream = new ReflectionProperty($body, 'stream');
  184. $bodyStream->setAccessible(true);
  185. $bodyStream->setValue($body, null);
  186. $this->assertTrue($body->eof());
  187. }
  188. public function isReadableAttachedTrue()
  189. {
  190. $this->stream = $this->resourceFactory();
  191. $body = new Body($this->stream);
  192. $this->assertTrue($body->isReadable());
  193. }
  194. public function isReadableAttachedFalse()
  195. {
  196. $stream = fopen('php://temp', 'w');
  197. $body = new Body($this->stream);
  198. $this->assertFalse($body->isReadable());
  199. fclose($stream);
  200. }
  201. public function testIsReadableDetached()
  202. {
  203. $this->stream = $this->resourceFactory();
  204. $body = new Body($this->stream);
  205. $body->detach();
  206. $this->assertFalse($body->isReadable());
  207. }
  208. public function isWritableAttachedTrue()
  209. {
  210. $this->stream = $this->resourceFactory();
  211. $body = new Body($this->stream);
  212. $this->assertTrue($body->isWritable());
  213. }
  214. public function isWritableAttachedFalse()
  215. {
  216. $stream = fopen('php://temp', 'r');
  217. $body = new Body($this->stream);
  218. $this->assertFalse($body->isWritable());
  219. fclose($stream);
  220. }
  221. public function testIsWritableDetached()
  222. {
  223. $this->stream = $this->resourceFactory();
  224. $body = new Body($this->stream);
  225. $body->detach();
  226. $this->assertFalse($body->isWritable());
  227. }
  228. public function isSeekableAttachedTrue()
  229. {
  230. $this->stream = $this->resourceFactory();
  231. $body = new Body($this->stream);
  232. $this->assertTrue($body->isSeekable());
  233. }
  234. // TODO: Is seekable is false when attached... how?
  235. public function testIsSeekableDetached()
  236. {
  237. $this->stream = $this->resourceFactory();
  238. $body = new Body($this->stream);
  239. $body->detach();
  240. $this->assertFalse($body->isSeekable());
  241. }
  242. public function testSeekAttached()
  243. {
  244. $this->stream = $this->resourceFactory();
  245. $body = new Body($this->stream);
  246. $body->seek(10);
  247. $this->assertEquals(10, ftell($this->stream));
  248. }
  249. public function testSeekDetachedThrowsRuntimeException()
  250. {
  251. $this->stream = $this->resourceFactory();
  252. $body = new Body($this->stream);
  253. $body->detach();
  254. $this->setExpectedException('\RuntimeException');
  255. $body->seek(10);
  256. }
  257. public function testRewindAttached()
  258. {
  259. $this->stream = $this->resourceFactory();
  260. $body = new Body($this->stream);
  261. fseek($this->stream, 10);
  262. $body->rewind();
  263. $this->assertEquals(0, ftell($this->stream));
  264. }
  265. public function testRewindDetachedThrowsRuntimeException()
  266. {
  267. $this->stream = $this->resourceFactory();
  268. $body = new Body($this->stream);
  269. $body->detach();
  270. $this->setExpectedException('\RuntimeException');
  271. $body->rewind();
  272. }
  273. public function testReadAttached()
  274. {
  275. $this->stream = $this->resourceFactory();
  276. $body = new Body($this->stream);
  277. $this->assertEquals(substr($this->text, 0, 10), $body->read(10));
  278. }
  279. public function testReadDetachedThrowsRuntimeException()
  280. {
  281. $this->stream = $this->resourceFactory();
  282. $body = new Body($this->stream);
  283. $body->detach();
  284. $this->setExpectedException('\RuntimeException');
  285. $body->read(10);
  286. }
  287. public function testWriteAttached()
  288. {
  289. $this->stream = $this->resourceFactory();
  290. $body = new Body($this->stream);
  291. while (feof($this->stream) === false) {
  292. fread($this->stream, 1024);
  293. }
  294. $body->write('foo');
  295. $this->assertEquals($this->text . 'foo', (string)$body);
  296. }
  297. public function testWriteDetachedThrowsRuntimeException()
  298. {
  299. $this->stream = $this->resourceFactory();
  300. $body = new Body($this->stream);
  301. $body->detach();
  302. $this->setExpectedException('\RuntimeException');
  303. $body->write('foo');
  304. }
  305. public function testGetContentsAttached()
  306. {
  307. $this->stream = $this->resourceFactory();
  308. $body = new Body($this->stream);
  309. fseek($this->stream, 10);
  310. $this->assertEquals(substr($this->text, 10), $body->getContents());
  311. }
  312. public function testGetContentsDetachedThrowsRuntimeException()
  313. {
  314. $this->stream = $this->resourceFactory();
  315. $body = new Body($this->stream);
  316. $body->detach();
  317. $this->setExpectedException('\RuntimeException');
  318. $body->getContents();
  319. }
  320. }