FunZlibTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. use Clue\StreamFilter;
  3. class BuiltInZlibTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function testFunZlibDeflateHelloWorld()
  6. {
  7. $deflate = StreamFilter\fun('zlib.deflate');
  8. $data = $deflate('hello') . $deflate(' ') . $deflate('world') . $deflate();
  9. $this->assertEquals(gzdeflate('hello world'), $data);
  10. }
  11. public function testFunZlibDeflateEmpty()
  12. {
  13. if (PHP_VERSION >= 7) $this->markTestSkipped('Not supported on PHP7 (empty string does not invoke filter)');
  14. $deflate = StreamFilter\fun('zlib.deflate');
  15. //$data = gzdeflate('');
  16. $data = $deflate();
  17. $this->assertEquals("\x03\x00", $data);
  18. }
  19. public function testFunZlibDeflateBig()
  20. {
  21. $deflate = StreamFilter\fun('zlib.deflate');
  22. $n = 1000;
  23. $expected = str_repeat('hello', $n);
  24. $bytes = '';
  25. for ($i = 0; $i < $n; ++$i) {
  26. $bytes .= $deflate('hello');
  27. }
  28. $bytes .= $deflate();
  29. $this->assertEquals($expected, gzinflate($bytes));
  30. }
  31. public function testFunZlibInflateHelloWorld()
  32. {
  33. $inflate = StreamFilter\fun('zlib.inflate');
  34. $data = $inflate(gzdeflate('hello world')) . $inflate();
  35. $this->assertEquals('hello world', $data);
  36. }
  37. public function testFunZlibInflateEmpty()
  38. {
  39. $inflate = StreamFilter\fun('zlib.inflate');
  40. $data = $inflate("\x03\x00") . $inflate();
  41. $this->assertEquals('', $data);
  42. }
  43. public function testFunZlibInflateBig()
  44. {
  45. if (defined('HHVM_VERSION')) $this->markTestSkipped('Not supported on HHVM (final chunk will not be emitted)');
  46. $inflate = StreamFilter\fun('zlib.inflate');
  47. $expected = str_repeat('hello', 10);
  48. $bytes = gzdeflate($expected);
  49. $ret = '';
  50. foreach (str_split($bytes, 2) as $chunk) {
  51. $ret .= $inflate($chunk);
  52. }
  53. $ret .= $inflate();
  54. $this->assertEquals($expected, $ret);
  55. }
  56. }