/tests/BodyTest.php

https://github.com/slimphp/Slim-Psr7 · PHP · 439 lines · 312 code · 94 blank · 33 comment · 3 complexity · e3e7b9e06615a4d0f8a410a025b1d244 MD5 · raw file

  1. <?php
  2. /**
  3. * Slim Framework (https://slimframework.com)
  4. *
  5. * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
  6. */
  7. declare(strict_types=1);
  8. namespace Slim\Tests\Psr7;
  9. use InvalidArgumentException;
  10. use PHPUnit\Framework\TestCase;
  11. use ReflectionProperty;
  12. use RuntimeException;
  13. use Slim\Psr7\Stream;
  14. use function fclose;
  15. use function feof;
  16. use function fopen;
  17. use function fread;
  18. use function fseek;
  19. use function ftell;
  20. use function fwrite;
  21. use function is_array;
  22. use function is_resource;
  23. use function mb_strlen;
  24. use function rewind;
  25. use function substr;
  26. class BodyTest extends TestCase
  27. {
  28. // @codingStandardsIgnoreStart
  29. /**
  30. * @var string
  31. */
  32. 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.';
  33. // @codingStandardsIgnoreEnd
  34. /**
  35. * @var resource
  36. */
  37. protected $stream;
  38. protected function tearDown(): void
  39. {
  40. if (is_resource($this->stream) === true) {
  41. fclose($this->stream);
  42. }
  43. }
  44. /**
  45. * @param string $mode
  46. *
  47. * @return resource
  48. */
  49. public function resourceFactory($mode = 'r+')
  50. {
  51. $stream = fopen('php://temp', $mode);
  52. fwrite($stream, $this->text);
  53. rewind($stream);
  54. return $stream;
  55. }
  56. public function testConstructorAttachesStream()
  57. {
  58. $this->stream = $this->resourceFactory();
  59. $body = new Stream($this->stream);
  60. $bodyStream = new ReflectionProperty($body, 'stream');
  61. $bodyStream->setAccessible(true);
  62. $this->assertSame($this->stream, $bodyStream->getValue($body));
  63. }
  64. /**
  65. */
  66. public function testConstructorInvalidStream()
  67. {
  68. $this->expectException(InvalidArgumentException::class);
  69. $this->stream = 'foo';
  70. $body = new Stream($this->stream);
  71. }
  72. public function testGetMetadata()
  73. {
  74. $this->stream = $this->resourceFactory();
  75. $body = new Stream($this->stream);
  76. $this->assertTrue(is_array($body->getMetadata()));
  77. }
  78. public function testGetMetadataKey()
  79. {
  80. $this->stream = $this->resourceFactory();
  81. $body = new Stream($this->stream);
  82. $this->assertEquals('php://temp', $body->getMetadata('uri'));
  83. }
  84. public function testGetMetadataKeyNotFound()
  85. {
  86. $this->stream = $this->resourceFactory();
  87. $body = new Stream($this->stream);
  88. $this->assertNull($body->getMetadata('foo'));
  89. }
  90. public function testDetach()
  91. {
  92. $this->stream = $this->resourceFactory();
  93. $body = new Stream($this->stream);
  94. $bodyStream = new ReflectionProperty($body, 'stream');
  95. $bodyStream->setAccessible(true);
  96. $bodyMetadata = new ReflectionProperty($body, 'meta');
  97. $bodyMetadata->setAccessible(true);
  98. $bodyReadable = new ReflectionProperty($body, 'readable');
  99. $bodyReadable->setAccessible(true);
  100. $bodyWritable = new ReflectionProperty($body, 'writable');
  101. $bodyWritable->setAccessible(true);
  102. $bodySeekable = new ReflectionProperty($body, 'seekable');
  103. $bodySeekable->setAccessible(true);
  104. $result = $body->detach();
  105. $this->assertSame($this->stream, $result);
  106. $this->assertNull($bodyStream->getValue($body));
  107. $this->assertNull($bodyMetadata->getValue($body));
  108. $this->assertNull($bodyReadable->getValue($body));
  109. $this->assertNull($bodyWritable->getValue($body));
  110. $this->assertNull($bodySeekable->getValue($body));
  111. }
  112. public function testToStringAttached()
  113. {
  114. $this->stream = $this->resourceFactory();
  115. $body = new Stream($this->stream);
  116. $this->assertEquals($this->text, (string) $body);
  117. }
  118. public function testToStringAttachedRewindsFirst()
  119. {
  120. $this->stream = $this->resourceFactory();
  121. $body = new Stream($this->stream);
  122. $this->assertEquals($this->text, (string) $body);
  123. $this->assertEquals($this->text, (string) $body);
  124. $this->assertEquals($this->text, (string) $body);
  125. }
  126. public function testToStringDetached()
  127. {
  128. $this->stream = $this->resourceFactory();
  129. $body = new Stream($this->stream);
  130. $bodyStream = new ReflectionProperty($body, 'stream');
  131. $bodyStream->setAccessible(true);
  132. $bodyStream->setValue($body, null);
  133. $this->assertEquals('', (string) $body);
  134. }
  135. public function testClose()
  136. {
  137. $this->stream = $this->resourceFactory();
  138. $body = new Stream($this->stream);
  139. $body->close();
  140. $bodyStream = new ReflectionProperty($body, 'stream');
  141. $bodyStream->setAccessible(true);
  142. $this->assertNull($bodyStream->getValue($body));
  143. }
  144. public function testGetSizeAttached()
  145. {
  146. $this->stream = $this->resourceFactory();
  147. $body = new Stream($this->stream);
  148. $this->assertEquals(mb_strlen($this->text), $body->getSize());
  149. }
  150. public function testGetSizeDetached()
  151. {
  152. $this->stream = $this->resourceFactory();
  153. $body = new Stream($this->stream);
  154. $bodyStream = new ReflectionProperty($body, 'stream');
  155. $bodyStream->setAccessible(true);
  156. $bodyStream->setValue($body, null);
  157. $this->assertNull($body->getSize());
  158. }
  159. public function testTellAttached()
  160. {
  161. $this->stream = $this->resourceFactory();
  162. $body = new Stream($this->stream);
  163. fseek($this->stream, 10);
  164. $this->assertEquals(10, $body->tell());
  165. }
  166. /**
  167. */
  168. public function testTellDetachedThrowsRuntimeException()
  169. {
  170. $this->expectException(RuntimeException::class);
  171. $this->stream = $this->resourceFactory();
  172. $body = new Stream($this->stream);
  173. $bodyStream = new ReflectionProperty($body, 'stream');
  174. $bodyStream->setAccessible(true);
  175. $bodyStream->setValue($body, null);
  176. $body->tell();
  177. }
  178. public function testEofAttachedFalse()
  179. {
  180. $this->stream = $this->resourceFactory();
  181. $body = new Stream($this->stream);
  182. fseek($this->stream, 10);
  183. $this->assertFalse($body->eof());
  184. }
  185. public function testEofAttachedTrue()
  186. {
  187. $this->stream = $this->resourceFactory();
  188. $body = new Stream($this->stream);
  189. while (feof($this->stream) === false) {
  190. fread($this->stream, 1024);
  191. }
  192. $this->assertTrue($body->eof());
  193. }
  194. public function testEofDetached()
  195. {
  196. $this->stream = $this->resourceFactory();
  197. $body = new Stream($this->stream);
  198. $bodyStream = new ReflectionProperty($body, 'stream');
  199. $bodyStream->setAccessible(true);
  200. $bodyStream->setValue($body, null);
  201. $this->assertTrue($body->eof());
  202. }
  203. public function isReadableAttachedTrue()
  204. {
  205. $this->stream = $this->resourceFactory();
  206. $body = new Stream($this->stream);
  207. $this->assertTrue($body->isReadable());
  208. }
  209. public function isReadableAttachedFalse()
  210. {
  211. $stream = fopen('php://temp', 'w');
  212. $body = new Stream($this->stream);
  213. $this->assertFalse($body->isReadable());
  214. fclose($stream);
  215. }
  216. public function testIsReadableDetached()
  217. {
  218. $this->stream = $this->resourceFactory();
  219. $body = new Stream($this->stream);
  220. $body->detach();
  221. $this->assertFalse($body->isReadable());
  222. }
  223. public function isWritableAttachedTrue()
  224. {
  225. $this->stream = $this->resourceFactory();
  226. $body = new Stream($this->stream);
  227. $this->assertTrue($body->isWritable());
  228. }
  229. public function isWritableAttachedFalse()
  230. {
  231. $stream = fopen('php://temp', 'r');
  232. $body = new Stream($this->stream);
  233. $this->assertFalse($body->isWritable());
  234. fclose($stream);
  235. }
  236. public function testIsWritableDetached()
  237. {
  238. $this->stream = $this->resourceFactory();
  239. $body = new Stream($this->stream);
  240. $body->detach();
  241. $this->assertFalse($body->isWritable());
  242. }
  243. public function isSeekableAttachedTrue()
  244. {
  245. $this->stream = $this->resourceFactory();
  246. $body = new Stream($this->stream);
  247. $this->assertTrue($body->isSeekable());
  248. }
  249. // TODO: Is seekable is false when attached... how?
  250. public function testIsSeekableDetached()
  251. {
  252. $this->stream = $this->resourceFactory();
  253. $body = new Stream($this->stream);
  254. $body->detach();
  255. $this->assertFalse($body->isSeekable());
  256. }
  257. public function testSeekAttached()
  258. {
  259. $this->stream = $this->resourceFactory();
  260. $body = new Stream($this->stream);
  261. $body->seek(10);
  262. $this->assertEquals(10, ftell($this->stream));
  263. }
  264. /**
  265. */
  266. public function testSeekDetachedThrowsRuntimeException()
  267. {
  268. $this->expectException(RuntimeException::class);
  269. $this->stream = $this->resourceFactory();
  270. $body = new Stream($this->stream);
  271. $body->detach();
  272. $body->seek(10);
  273. }
  274. public function testRewindAttached()
  275. {
  276. $this->stream = $this->resourceFactory();
  277. $body = new Stream($this->stream);
  278. fseek($this->stream, 10);
  279. $body->rewind();
  280. $this->assertEquals(0, ftell($this->stream));
  281. }
  282. /**
  283. */
  284. public function testRewindDetachedThrowsRuntimeException()
  285. {
  286. $this->expectException(RuntimeException::class);
  287. $this->stream = $this->resourceFactory();
  288. $body = new Stream($this->stream);
  289. $body->detach();
  290. $body->rewind();
  291. }
  292. public function testReadAttached()
  293. {
  294. $this->stream = $this->resourceFactory();
  295. $body = new Stream($this->stream);
  296. $this->assertEquals(substr($this->text, 0, 10), $body->read(10));
  297. }
  298. /**
  299. */
  300. public function testReadDetachedThrowsRuntimeException()
  301. {
  302. $this->expectException(RuntimeException::class);
  303. $this->stream = $this->resourceFactory();
  304. $body = new Stream($this->stream);
  305. $body->detach();
  306. $body->read(10);
  307. }
  308. public function testWriteAttached()
  309. {
  310. $this->stream = $this->resourceFactory();
  311. $body = new Stream($this->stream);
  312. while (feof($this->stream) === false) {
  313. fread($this->stream, 1024);
  314. }
  315. $body->write('foo');
  316. $this->assertEquals($this->text . 'foo', (string) $body);
  317. }
  318. /**
  319. */
  320. public function testWriteDetachedThrowsRuntimeException()
  321. {
  322. $this->expectException(RuntimeException::class);
  323. $this->stream = $this->resourceFactory();
  324. $body = new Stream($this->stream);
  325. $body->detach();
  326. $body->write('foo');
  327. }
  328. public function testGetContentsAttached()
  329. {
  330. $this->stream = $this->resourceFactory();
  331. $body = new Stream($this->stream);
  332. fseek($this->stream, 10);
  333. $this->assertEquals(substr($this->text, 10), $body->getContents());
  334. }
  335. /**
  336. */
  337. public function testGetContentsDetachedThrowsRuntimeException()
  338. {
  339. $this->expectException(RuntimeException::class);
  340. $this->stream = $this->resourceFactory();
  341. $body = new Stream($this->stream);
  342. $body->detach();
  343. $body->getContents();
  344. }
  345. }