unfold encoding tests
authorMichael Wallner <mike@php.net>
Wed, 9 Jul 2014 15:21:48 +0000 (17:21 +0200)
committerMichael Wallner <mike@php.net>
Wed, 9 Jul 2014 15:21:48 +0000 (17:21 +0200)
phpunit/EncodingTest.php [deleted file]
tests/encstream001.phpt [new file with mode: 0644]
tests/encstream002.phpt [new file with mode: 0644]
tests/encstream003.phpt [new file with mode: 0644]
tests/encstream004.phpt [new file with mode: 0644]
tests/encstream005.phpt [new file with mode: 0644]
tests/encstream006.phpt [new file with mode: 0644]
tests/encstream007.phpt [new file with mode: 0644]
tests/encstream008.phpt [new file with mode: 0644]
tests/encstream009.phpt [new file with mode: 0644]

diff --git a/phpunit/EncodingTest.php b/phpunit/EncodingTest.php
deleted file mode 100644 (file)
index 4642613..0000000
+++ /dev/null
@@ -1,172 +0,0 @@
-<?php
-
-class EncodingStreamTest extends PHPUnit_Framework_TestCase {
-    function testChunkStatic() {
-        $file = file(__FILE__);
-        $cenc = array_reduce(
-            $file,
-            function($data, $line) {
-                return $data . sprintf("%lx\r\n%s\r\n", strlen($line), $line);
-            }
-        ) . "0\r\n";
-
-        $this->assertEquals(implode("", $file), http\Encoding\Stream\Dechunk::decode($cenc));
-    }
-
-    function testChunkNoteEncoded() {
-        $s = "this is apparently not encodded\n";
-        $this->assertEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
-    }
-
-    function testChunkNotEncodedNotice() {
-           error_reporting(E_ALL);
-        $this->setExpectedException("PHPUnit_Framework_Error_Notice", 
-            "Data does not seem to be chunked encoded");
-        $s = "this is apparently not encodded\n";
-        $this->assertEquals($s, http\Encoding\Stream\Dechunk::decode($s));
-    }
-
-    function testChunkNotEncodedFail() {
-        $s = "3\nis \nbetter than\n1\n";
-        $this->assertNotEquals($s, @http\Encoding\Stream\Dechunk::decode($s));
-    }
-
-    function testChunkNotEncodedWarning1() {
-        $this->setExpectedException("PHPUnit_Framework_Error_Warning", 
-            "Expected LF at pos 8 of 20 but got 0x74");
-        $s = "3\nis \nbetter than\n1\n";
-        http\Encoding\Stream\Dechunk::decode($s);
-    }
-
-    function testChunkNotEncodedWarning2() {
-        $this->setExpectedException("PHPUnit_Framework_Error_Warning", 
-            "Expected CRLF at pos 10 of 24 but got 0x74 0x74");
-        $s = "3\r\nis \r\nbetter than\r\n1\r\n";
-        http\Encoding\Stream\Dechunk::decode($s);
-    }
-
-    function testChunkNotEncodedWarning3() {
-        $this->setExpectedException("PHPUnit_Framework_Error_Warning", 
-            "Expected chunk size at pos 6 of 27 but got trash");
-        $s = "3\nis \nreally better than\n1\n";
-        http\Encoding\Stream\Dechunk::decode($s);
-    }
-
-    function testChunkFlush() {
-        $dech = new http\Encoding\Stream\Dechunk(http\Encoding\Stream::FLUSH_FULL);
-        $file = file(__FILE__);
-        $data = "";
-        foreach ($file as $i => $line) {
-            $dech = clone $dech;
-            if ($i % 2) {
-                $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
-            } else {
-                $data .= $dech->update(sprintf("%lx\r\n", strlen($line)));
-                $data .= $dech->flush();
-                $data .= $dech->update($line);
-                $data .= $dech->flush();
-                $data .= $dech->update("\r\n");
-            }
-            $dech->flush();
-            $this->assertFalse($dech->done());
-        }
-        $data .= $dech->update("0\r\n");
-        $this->assertTrue($dech->done());
-        $data .= $dech->finish();
-        $this->assertEquals(implode("", $file), $data);
-    }
-
-    function testZlibStatic() {
-        $file = file_get_contents(__FILE__);
-        $this->assertEquals($file, 
-            http\Encoding\Stream\Inflate::decode(
-                http\Encoding\Stream\Deflate::encode(
-                    $file, http\Encoding\Stream\Deflate::TYPE_GZIP
-                )
-            )
-        );
-        $this->assertEquals($file, 
-            http\Encoding\Stream\Inflate::decode(
-                http\Encoding\Stream\Deflate::encode(
-                    $file, http\Encoding\Stream\Deflate::TYPE_ZLIB
-                )
-            )
-        );
-        $this->assertEquals($file, 
-            http\Encoding\Stream\Inflate::decode(
-                http\Encoding\Stream\Deflate::encode(
-                    $file, http\Encoding\Stream\Deflate::TYPE_RAW
-                )
-            )
-        );
-    }
-
-    function testZlibAutoFlush() {
-        $defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL);
-        $infl = new http\Encoding\Stream\Inflate;
-
-        for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) {
-            $infl = clone $infl;
-            $defl = clone $defl;
-            if (isset($data)) {
-                $this->assertEquals($data, $infl->update($defl->update($data)));
-            }
-        }
-
-        echo $infl->update($defl->finish());
-        echo $infl->finish();
-    }
-
-    function testZlibWithoutFlush() {
-        $defl = new http\Encoding\Stream\Deflate;
-        $infl = new http\Encoding\Stream\Inflate;
-        $file = file(__FILE__);
-        $data = "";
-        foreach ($file as $line) {
-            $infl = clone $infl;
-            $defl = clone $defl;
-            if (strlen($temp = $defl->update($line))) {
-                foreach(str_split($temp) as $byte) {
-                    $data .= $infl->update($byte);
-                }
-            }
-        }
-        if (strlen($temp = $defl->finish())) {
-            $data .= $infl->update($temp);
-        }
-        $data .= $infl->finish();
-        $this->assertEquals(implode("", $file), $data);
-    }
-
-    function testZlibWithExplicitFlush() {
-        $defl = new http\Encoding\Stream\Deflate;
-        $infl = new http\Encoding\Stream\Inflate;
-        $file = file(__FILE__);
-        $data = "";
-        foreach ($file as $line) {
-               $data .= $infl->flush();
-            if (strlen($temp = $defl->update($line))) {
-                $data .= $infl->update($temp);
-                       $data .= $infl->flush();
-            }
-            if (strlen($temp = $defl->flush())) {
-                $data .= $infl->update($temp);
-                       $data .= $infl->flush();
-            }
-            $this->assertTrue($defl->done());
-        }
-        if (strlen($temp = $defl->finish())) {
-            $data .= $infl->update($temp);
-        }
-        $this->assertTrue($defl->done());
-        $data .= $infl->finish();
-        $this->assertTrue($infl->done());
-        $this->assertEquals(implode("", $file), $data);
-    }
-
-    function testInflateError() {
-        $this->setExpectedException("PHPUnit_Framework_Error_Warning",
-            "Could not inflate data: data error");
-        http\Encoding\Stream\Inflate::decode("if this goes through, something's pretty wrong");
-    }
-}
diff --git a/tests/encstream001.phpt b/tests/encstream001.phpt
new file mode 100644 (file)
index 0000000..5dc13d3
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+encoding stream chunked static
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = file(__FILE__);
+$cenc = array_reduce(
+       $file,
+       function($data, $line) {
+               return $data . sprintf("%lx\r\n%s\r\n", strlen($line), $line);
+       }
+) . "0\r\n";
+
+var_dump(implode("", $file) === http\Encoding\Stream\Dechunk::decode($cenc));
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+DONE
+
diff --git a/tests/encstream002.phpt b/tests/encstream002.phpt
new file mode 100644 (file)
index 0000000..3be0aca
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+encoding stream chunked not encoded
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$s = "this is apparently not encodded\n";
+var_dump($s === http\Encoding\Stream\Dechunk::decode($s));
+
+?>
+DONE
+--EXPECTF--
+Test
+
+Notice: http\Encoding\Stream\Dechunk::decode(): Data does not seem to be chunked encoded in %s on line %d
+bool(true)
+DONE
diff --git a/tests/encstream003.phpt b/tests/encstream003.phpt
new file mode 100644 (file)
index 0000000..ad8c737
--- /dev/null
@@ -0,0 +1,38 @@
+--TEST--
+encoding stream chunked error
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$s = "3\nis \nbetter than\n1\n";
+var_dump(http\Encoding\Stream\Dechunk::decode($s));
+$s = "3\r\nis \r\nbetter than\r\n1\r\n";
+var_dump(http\Encoding\Stream\Dechunk::decode($s));
+$s = "3\nis \nreally better than\n1\n";
+var_dump(http\Encoding\Stream\Dechunk::decode($s));
+?>
+DONE
+--EXPECTF--
+Test
+
+Warning: http\Encoding\Stream\Dechunk::decode(): Expected LF at pos 8 of 20 but got 0x74 in %s on line %d
+
+Warning: http\Encoding\Stream\Dechunk::decode(): Truncated message: chunk size 190 exceeds remaining data size 11 at pos 9 of 20 in %s on line %d
+string(14) "is ter than
+1
+"
+
+Warning: http\Encoding\Stream\Dechunk::decode(): Expected CRLF at pos 10 of 24 but got 0x74 0x74 in %s on line %d
+
+Warning: http\Encoding\Stream\Dechunk::decode(): Truncated message: chunk size 190 exceeds remaining data size 12 at pos 12 of 24 in %s on line %d
+string(15) "is er than
+1
+"
+
+Warning: http\Encoding\Stream\Dechunk::decode(): Expected chunk size at pos 6 of 27 but got trash in %s on line %d
+bool(false)
+DONE
diff --git a/tests/encstream004.phpt b/tests/encstream004.phpt
new file mode 100644 (file)
index 0000000..eb9da0f
--- /dev/null
@@ -0,0 +1,38 @@
+--TEST--
+encoding stream chunked flush
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$dech = new http\Encoding\Stream\Dechunk(http\Encoding\Stream::FLUSH_FULL);
+$file = file(__FILE__);
+$data = "";
+foreach ($file as $i => $line) {
+       $dech = clone $dech;
+       if ($i % 2) {
+               $data .= $dech->update(sprintf("%lx\r\n%s\r\n", strlen($line), $line));
+       } else {
+               $data .= $dech->update(sprintf("%lx\r\n", strlen($line)));
+               $data .= $dech->flush();
+               $data .= $dech->update($line);
+               $data .= $dech->flush();
+               $data .= $dech->update("\r\n");
+       }
+       $dech->flush();
+       $dech->done() and printf("uh-oh done() reported true!\n");
+}
+$data .= $dech->update("0\r\n");
+var_dump($dech->done());
+$data .= $dech->finish();
+var_dump(implode("", $file) === $data);
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+bool(true)
+DONE
diff --git a/tests/encstream005.phpt b/tests/encstream005.phpt
new file mode 100644 (file)
index 0000000..6455ed4
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+encoding stream zlib static
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = file_get_contents(__FILE__);
+var_dump($file ===
+       http\Encoding\Stream\Inflate::decode(
+               http\Encoding\Stream\Deflate::encode(
+                       $file, http\Encoding\Stream\Deflate::TYPE_GZIP
+               )
+       )
+);
+var_dump($file ===
+       http\Encoding\Stream\Inflate::decode(
+               http\Encoding\Stream\Deflate::encode(
+                       $file, http\Encoding\Stream\Deflate::TYPE_ZLIB
+               )
+       )
+);
+var_dump($file ===
+       http\Encoding\Stream\Inflate::decode(
+               http\Encoding\Stream\Deflate::encode(
+                       $file, http\Encoding\Stream\Deflate::TYPE_RAW
+               )
+       )
+);
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+bool(true)
+bool(true)
+DONE
diff --git a/tests/encstream006.phpt b/tests/encstream006.phpt
new file mode 100644 (file)
index 0000000..5430bfa
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+encoding stream zlib auto flush
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$defl = new http\Encoding\Stream\Deflate(http\Encoding\Stream::FLUSH_FULL);
+$infl = new http\Encoding\Stream\Inflate;
+
+for ($f = fopen(__FILE__, "rb"); !feof($f); $data = fread($f, 0x100)) {
+       $infl = clone $infl;
+       $defl = clone $defl;
+       if (isset($data)) {
+               if ($data !== $d=$infl->update($defl->update($data))) {
+                       printf("uh-oh »%s« != »%s«\n", $data, $d);
+               }
+       }
+}
+
+echo $infl->update($defl->finish());
+echo $infl->finish();
+?>
+DONE
+--EXPECT--
+Test
+DONE
diff --git a/tests/encstream007.phpt b/tests/encstream007.phpt
new file mode 100644 (file)
index 0000000..5fdeffe
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+encoding stream zlib without flush
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$defl = new http\Encoding\Stream\Deflate;
+$infl = new http\Encoding\Stream\Inflate;
+$file = file(__FILE__);
+$data = "";
+foreach ($file as $line) {
+       $infl = clone $infl;
+       $defl = clone $defl;
+       if (strlen($temp = $defl->update($line))) {
+               foreach(str_split($temp) as $byte) {
+                       $data .= $infl->update($byte);
+               }
+       }
+}
+if (strlen($temp = $defl->finish())) {
+       $data .= $infl->update($temp);
+}
+$data .= $infl->finish();
+var_dump(implode("", $file) === $data);
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+DONE
diff --git a/tests/encstream008.phpt b/tests/encstream008.phpt
new file mode 100644 (file)
index 0000000..a46f9f0
--- /dev/null
@@ -0,0 +1,42 @@
+--TEST--
+encoding stream zlib with explicit flush
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$defl = new http\Encoding\Stream\Deflate;
+$infl = new http\Encoding\Stream\Inflate;
+$file = file(__FILE__);
+$data = "";
+foreach ($file as $line) {
+       $data .= $infl->flush();
+       if (strlen($temp = $defl->update($line))) {
+               $data .= $infl->update($temp);
+               $data .= $infl->flush();
+       }
+       if (strlen($temp = $defl->flush())) {
+               $data .= $infl->update($temp);
+               $data .= $infl->flush();
+       }
+       $defl->done() or printf("uh-oh stream's not done yet!\n");
+}
+if (strlen($temp = $defl->finish())) {
+       $data .= $infl->update($temp);
+}
+var_dump($defl->done());
+$data .= $infl->finish();
+var_dump($infl->done());
+var_dump(implode("", $file) === $data);
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+bool(true)
+bool(true)
+DONE
diff --git a/tests/encstream009.phpt b/tests/encstream009.phpt
new file mode 100644 (file)
index 0000000..fa51a8a
--- /dev/null
@@ -0,0 +1,20 @@
+--TEST--
+encoding stream zlib error
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+var_dump(http\Encoding\Stream\Inflate::decode("if this goes through, something's pretty wrong"));
+
+?>
+DONE
+--EXPECTF--
+Test
+
+Warning: http\Encoding\Stream\Inflate::decode(): Could not inflate data: data error in %s on line %d
+bool(false)
+DONE