unfold message body tests
authorMichael Wallner <mike@php.net>
Thu, 10 Jul 2014 11:07:52 +0000 (13:07 +0200)
committerMichael Wallner <mike@php.net>
Thu, 10 Jul 2014 11:07:52 +0000 (13:07 +0200)
phpunit/MessageBodyTest.php [deleted file]
tests/messagebody001.phpt [new file with mode: 0644]
tests/messagebody002.phpt [new file with mode: 0644]
tests/messagebody003.phpt [new file with mode: 0644]
tests/messagebody004.phpt [new file with mode: 0644]
tests/messagebody005.phpt [new file with mode: 0644]
tests/messagebody006.phpt [new file with mode: 0644]
tests/messagebody007.phpt [new file with mode: 0644]
tests/messagebody008.phpt [new file with mode: 0644]
tests/messagebody009.phpt [new file with mode: 0644]
tests/messagebody010.phpt [new file with mode: 0644]

diff --git a/phpunit/MessageBodyTest.php b/phpunit/MessageBodyTest.php
deleted file mode 100644 (file)
index 3aaf3eb..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-<?php
-
-class MessageBodyTest extends PHPUnit_Framework_TestCase {
-    protected $file, $temp;
-
-    function setUp() {
-        $this->file = new http\Message\Body(fopen(__FILE__, "r"));
-        $this->temp = new http\Message\Body();
-    }
-
-    function testStat() {
-        $this->assertEquals(filesize(__FILE__), $this->file->stat("size"));
-        $this->assertEquals(filemtime(__FILE__), $this->file->stat("mtime"));
-        $this->assertEquals(fileatime(__FILE__), $this->file->stat("atime"));
-        $this->assertEquals(filectime(__FILE__), $this->file->stat("ctime"));
-        $this->assertEquals(
-            (object) array(
-                "size" => 0,
-                "mtime" => 0,
-                "atime" => 0,
-                "ctime" => 0,
-            ),
-            $this->temp->stat()
-        );
-    }
-
-    function testAppendError() {
-       $this->setExpectedException("http\Exception\RuntimeException");
-        $this->file->append("nope");
-    }
-    function testAppend() {
-        $this->temp->append("yes");
-    }
-
-    function testAddForm() {
-        $this->temp->addForm(
-            array(
-                "foo" => "bar",
-                "more" => array(
-                    "bah", "baz", "fuz"
-                ),
-            ),
-            array(
-                array(
-                    "file" => __FILE__,
-                    "name" => "upload",
-                    "type" => "text/plain",
-                )
-            )
-        );
-
-        $file = str_replace("%", "%c", file_get_contents(__FILE__));
-        $this->assertStringMatchesFormat(
-            "--%x.%x\r\n".
-            "Content-Disposition: form-data; name=\"foo\"\r\n".
-            "\r\n".
-            "bar\r\n".
-            "--%x.%x\r\n".
-            "Content-Disposition: form-data; name=\"more[0]\"\r\n".
-            "\r\n".
-            "bah\r\n".
-            "--%x.%x\r\n".
-            "Content-Disposition: form-data; name=\"more[1]\"\r\n".
-            "\r\n".
-            "baz\r\n".
-            "--%x.%x\r\n".
-            "Content-Disposition: form-data; name=\"more[2]\"\r\n".
-            "\r\n".
-            "fuz\r\n".
-            "--%x.%x\r\n".
-            "Content-Disposition: form-data; name=\"upload\"; filename=\"MessageBodyTest.php\"\r\n".
-            "Content-Transfer-Encoding: binary\r\n".
-            "Content-Type: text/plain\r\n".
-            "\r\n".
-            "{$file}\r\n".
-            "--%x.%x--\r\n".
-            "",
-            str_replace("\r", "", $this->temp) // phpunit replaces \r\n with \n
-        );
-    }
-
-    function testAddPart() {
-        $this->temp->addPart(new http\Message("This: is a header\n\nand this is the data\n"));
-        $this->assertStringMatchesFormat(
-            "--%x.%x\r\n".
-            "This: is a header\r\n".
-            "Content-Length: 21\r\n".
-            "\r\n".
-            "and this is the data\n\r\n".
-            "--%x.%x--\r\n".
-            "",
-            str_replace("\r", "", $this->temp)
-        );
-    }
-
-    function testEtag() {
-        $s = stat(__FILE__);
-        $this->assertEquals(
-            sprintf(
-                "%lx-%lx-%lx", 
-                $s["ino"],$s["mtime"],$s["size"]
-            ),
-            $this->file->etag()
-        );
-        $this->assertEquals(crc32(""), $this->temp->etag());
-    }
-
-    function testToStream() {
-        $this->file->toStream($f = fopen("php://temp", "w")); 
-        fseek($f, 0, SEEK_SET);
-        $this->assertEquals(
-            file_get_contents(__FILE__), 
-            fread($f, filesize(__FILE__))
-        );
-    }
-
-    function testToCallback() {
-        $s = "";
-        $this->file->toCallback(
-            function($body, $string) use (&$s) { $s.=$string; }
-        );
-        $this->assertEquals($s, (string) $this->file);
-    }
-
-    function testClone() {
-        $this->assertEquals((string) $this->file, (string) clone $this->file);
-    }
-    
-    function testGetResource() {
-       $stream = $this->file->getResource();
-       $this->assertTrue(is_resource($stream));
-       $stat = fstat($stream);
-       $this->assertEquals(filesize(__FILE__), $stat["size"]);
-    }
-}
diff --git a/tests/messagebody001.phpt b/tests/messagebody001.phpt
new file mode 100644 (file)
index 0000000..6f1e10a
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+message body stat
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__, "r"));
+$temp = new http\Message\Body();
+var_dump(filesize(__FILE__) === $file->stat("size"));
+var_dump(filemtime(__FILE__) === $file->stat("mtime"));
+var_dump(fileatime(__FILE__) === $file->stat("atime"));
+var_dump(filectime(__FILE__) === $file->stat("ctime"));
+var_dump(
+       (object) array(
+               "size" => 0,
+               "mtime" => 0,
+               "atime" => 0,
+               "ctime" => 0,
+       ) == $temp->stat()
+);
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+DONE
diff --git a/tests/messagebody002.phpt b/tests/messagebody002.phpt
new file mode 100644 (file)
index 0000000..fe28e28
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+message body append
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body();
+$temp->append("yes");
+
+var_dump((string) $temp);
+
+?>
+DONE
+--EXPECT--
+Test
+string(3) "yes"
+DONE
diff --git a/tests/messagebody003.phpt b/tests/messagebody003.phpt
new file mode 100644 (file)
index 0000000..d6f9ce1
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+message body append error
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__, "r"));
+try {
+       $file->append("nope");
+} catch (Exception $e) {
+       echo $e, "\n";
+}
+
+?>
+DONE
+--EXPECTF--
+Test
+exception 'http\Exception\RuntimeException' with message 'http\Message\Body::append(): Failed to append 4 bytes to body; wrote 0' in /home/mike/src/ext-http.git/tests/messagebody003.php:6
+Stack trace:
+#0 %s(%d): http\Message\Body->append('nope')
+#1 {main}
+DONE
diff --git a/tests/messagebody004.phpt b/tests/messagebody004.phpt
new file mode 100644 (file)
index 0000000..64254d4
--- /dev/null
@@ -0,0 +1,81 @@
+--TEST--
+message body add form
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addForm(
+       array(
+               "foo" => "bar",
+               "more" => array(
+                       "bah", "baz", "fuz"
+               ),
+       ),
+       array(
+               array(
+                       "file" => __FILE__,
+                       "name" => "upload",
+                       "type" => "text/plain",
+               )
+       )
+);
+
+echo $temp;
+
+?>
+DONE
+--EXPECTF--
+Test
+--%x.%x
+Content-Disposition: form-data; name="foo"
+
+bar
+--%x.%x
+Content-Disposition: form-data; name="more[0]"
+
+bah
+--%x.%x
+Content-Disposition: form-data; name="more[1]"
+
+baz
+--%x.%x
+Content-Disposition: form-data; name="more[2]"
+
+fuz
+--%x.%x
+Content-Disposition: form-data; name="upload"; filename="%s"
+Content-Transfer-Encoding: binary
+Content-Type: text/plain
+
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addForm(
+       array(
+               "foo" => "bar",
+               "more" => array(
+                       "bah", "baz", "fuz"
+               ),
+       ),
+       array(
+               array(
+                       "file" => __FILE__,
+                       "name" => "upload",
+                       "type" => "text/plain",
+               )
+       )
+);
+
+echo $temp;
+
+?>
+DONE
+
+--%x.%x--
+DONE
diff --git a/tests/messagebody005.phpt b/tests/messagebody005.phpt
new file mode 100644 (file)
index 0000000..2d1d8b2
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+message body add part
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$temp = new http\Message\Body;
+$temp->addPart(new http\Message("This: is a header\n\nand this is the data\n"));
+echo $temp;
+
+?>
+DONE
+--EXPECTF--
+Test
+--%x.%x
+This: is a header
+Content-Length: 21
+
+and this is the data
+
+--%x.%x--
+DONE
diff --git a/tests/messagebody006.phpt b/tests/messagebody006.phpt
new file mode 100644 (file)
index 0000000..a02057e
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+message body etag
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--INI--
+http.etag.mode = crc32b
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__, "r"));
+$temp = new http\Message\Body;
+$s = stat(__FILE__);
+var_dump(
+       sprintf(
+               "%lx-%lx-%lx", 
+               $s["ino"],$s["mtime"],$s["size"]
+       ) === $file->etag()
+);
+var_dump($temp->etag());
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+string(8) "00000000"
+DONE
diff --git a/tests/messagebody007.phpt b/tests/messagebody007.phpt
new file mode 100644 (file)
index 0000000..dc010da
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+message body to stream
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__,"r"));
+$file->toStream($f = fopen("php://temp", "w")); 
+fseek($f, 0, SEEK_SET);
+var_dump(file_get_contents(__FILE__) === stream_get_contents($f));
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+DONE
diff --git a/tests/messagebody008.phpt b/tests/messagebody008.phpt
new file mode 100644 (file)
index 0000000..cb9612c
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+message body to callback
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__,"r"));
+$s = "";
+$file->toCallback(
+       function($body, $string) use (&$s) { $s.=$string; }
+);
+var_dump($s === (string) $file);
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+DONE
diff --git a/tests/messagebody009.phpt b/tests/messagebody009.phpt
new file mode 100644 (file)
index 0000000..a7a7f43
--- /dev/null
@@ -0,0 +1,19 @@
+--TEST--
+message body clone
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__,"r"));
+var_dump((string) $file === (string) clone $file);
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+DONE
diff --git a/tests/messagebody010.phpt b/tests/messagebody010.phpt
new file mode 100644 (file)
index 0000000..b50898d
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+message body resource
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+$file = new http\Message\Body(fopen(__FILE__,"r"));
+for($i=0;$i<10;++$i) $stream = $file->getResource();
+var_dump(is_resource($stream));
+$stat = fstat($stream);
+var_dump(filesize(__FILE__) === $stat["size"]);
+
+?>
+DONE
+--EXPECT--
+Test
+bool(true)
+bool(true)
+DONE