tests
authorMichael Wallner <mike@php.net>
Thu, 19 Jan 2012 15:45:55 +0000 (15:45 +0000)
committerMichael Wallner <mike@php.net>
Thu, 19 Jan 2012 15:45:55 +0000 (15:45 +0000)
phpunit/MessageBodyTest.php [new file with mode: 0644]
phpunit/ParamsTest.php [new file with mode: 0644]
phpunit/UrlTest.php [new file with mode: 0644]
tests/params001.phpt [new file with mode: 0644]
tests/propertyproxy001.phpt [new file with mode: 0644]
tests/properyproxy001.phpt [deleted file]
tests/querystring_001.phpt
tests/response001.phpt [new file with mode: 0644]

diff --git a/phpunit/MessageBodyTest.php b/phpunit/MessageBodyTest.php
new file mode 100644 (file)
index 0000000..bc07401
--- /dev/null
@@ -0,0 +1,109 @@
+<?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(
+            array(
+                "size" => 0,
+                "mtime" => 0,
+                "atime" => 0,
+                "ctime" => 0,
+            ),
+            $this->temp->stat()
+        );
+    }
+
+    function testAppend() {
+        $this->assertEquals(0, $this->file->append("nope"));
+        $this->assertEquals(3, $this->temp->append("yes"));
+    }
+
+    function testAdd() {
+        $this->assertTrue( 
+            $this->temp->add(
+                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: attachment; 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 testEtag() {
+        $s = stat(__FILE__);
+        $this->assertEquals(
+            sprintf(
+                "%lx-%lx-%lx", 
+                $s["ino"],$s["mtime"],$s["size"]
+            ),
+            $this->file->etag()
+        );
+        $this->assertEquals(md5(""), $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);
+    }
+}
diff --git a/phpunit/ParamsTest.php b/phpunit/ParamsTest.php
new file mode 100644 (file)
index 0000000..0e139b8
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+
+class ParamsTest extends PHPUnit_Framework_TestCase {
+    function testDefault() {
+        $s = "foo, bar;arg=0;bla, gotit=0;now";
+        $this->runAssertions(
+            new http\Params($s),
+            str_replace(" ", "", $s)
+        );
+    }
+
+    function testCustom() {
+        $s = "foo bar.arg:0.bla gotit:0.now";
+        $this->runAssertions(
+            new http\Params($s, " ", ".", ":"),
+            $s
+        );
+    }
+
+    protected function runAssertions($p, $s) {
+        $this->assertCount(3, $p->params);
+        $this->assertArrayHasKey("foo", $p->params);
+        $this->assertArrayHasKey("bar", $p->params);
+        $this->assertArrayHasKEy("gotit", $p->params);
+
+        $this->assertTrue($p["foo"]["value"]);
+        $this->assertTrue($p["bar"]["value"]);
+        $this->assertEmpty($p["gotit"]["value"]);
+
+        $this->assertEmpty($p["foo"]["arguments"]);
+        $this->assertCount(2, $p["bar"]["arguments"]);
+        $this->assertCount(1, $p["gotit"]["arguments"]);
+
+        $this->assertEmpty($p["bar"]["arguments"]["arg"]);
+        $this->assertTrue($p["bar"]["arguments"]["bla"]);
+        $this->assertTrue($p["gotit"]["arguments"]["now"]);
+
+        $this->assertEquals($s, (string) $p);
+
+        $this->assertEquals(
+            array (
+                'foo' => 
+                array (
+                    'value' => true,
+                    'arguments' => 
+                    array (
+                    ),
+                ),
+                'bar' => 
+                array (
+                    'value' => true,
+                    'arguments' => 
+                    array (
+                        'arg' => '0',
+                        'bla' => true,
+                    ),
+                ),
+                'gotit' => 
+                array (
+                    'value' => '0',
+                    'arguments' => 
+                    array (
+                        'now' => true,
+                    ),
+                ),
+            ),
+            $p->params
+        );
+       }
+}
diff --git a/phpunit/UrlTest.php b/phpunit/UrlTest.php
new file mode 100644 (file)
index 0000000..ebd020f
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+class UrlTest extends PHPUnit_Framework_TestCase {
+       protected $url; 
+       function setUp() {
+               $this->url = "http://user:pass@www.example.com:8080/path/file.ext".
+                       "?foo=bar&more[]=1&more[]=2#hash";
+       }
+
+       function testStandard() {
+               $this->assertEquals($this->url, (string) new http\Url($this->url));
+               
+               $url = new http\Url($this->url, 
+                       array("path" => "changed", "query" => "foo=&added=this"), 
+                       http\Url::JOIN_PATH |
+                       http\Url::JOIN_QUERY |
+                       http\Url::STRIP_AUTH |
+                       http\Url::STRIP_FRAGMENT
+               );
+
+               $this->assertEquals("http", $url->scheme);
+               $this->assertEmpty($url->user);
+               $this->assertEmpty($url->pass);
+               $this->assertEquals("www.example.com", $url->host);
+               $this->assertEquals(8080, $url->port);
+               $this->assertEquals("/path/changed", $url->path);
+               $this->assertEquals("foo=&more%5B0%5D=1&more%5B1%5D=2&added=this", $url->query);
+               $this->assertEmpty($url->fragment);
+       }
+}
diff --git a/tests/params001.phpt b/tests/params001.phpt
new file mode 100644 (file)
index 0000000..ac52bf7
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+header params
+--SKIPIF--
+<?php
+include "skipif.inc";
+--FILE--
+<?php
+echo "Test\n";
+
+$ct = new http\Params("text/html; charset=utf-8");
+
+var_dump(
+       isset($ct["text/html"]),
+       isset($ct["text/json"]),
+       $ct["text/html"]["arguments"]["charset"]
+);
+
+unset($ct["text/html"]);
+$ct[] = "text/json";
+$ct["text/json"] = array("arguments" => array("charset" => "iso-8859-1"));
+
+var_dump(
+       isset($ct["text/html"]),
+       isset($ct["text/json"]),
+       $ct["text/json"]["arguments"]["charset"]
+);
+
+var_dump((string) $ct);
+
+echo "Done\n";
+--EXPECTF--
+Test
+bool(true)
+bool(false)
+string(5) "utf-8"
+bool(false)
+bool(true)
+string(10) "iso-8859-1"
+string(%d) "text/json;charset=iso-8859-1"
+Done
diff --git a/tests/propertyproxy001.phpt b/tests/propertyproxy001.phpt
new file mode 100644 (file)
index 0000000..3f9d70a
--- /dev/null
@@ -0,0 +1,94 @@
+--TEST--
+property proxy
+--FILE--
+<?php
+
+class m extends http\Message { 
+    function test() { 
+        $this->headers["bykey"] = 1; 
+        var_dump($this->headers); 
+
+        $h = &$this->headers; 
+        $h["by1ref"] = 2; 
+        var_dump($this->headers); 
+
+        $x = &$this->headers["byXref"];
+
+        $h = &$this->headers["by2ref"]; 
+        $h = 1; 
+        var_dump($this->headers);
+
+        $x = 2;
+        var_dump($this->headers);
+
+        $this->headers["bynext"][] = 1;
+        $this->headers["bynext"][] = 2;
+        $this->headers["bynext"][] = 3;
+        var_dump($this->headers);
+    }
+} 
+
+$m=new m; 
+$m->test(); 
+echo $m,"\n";
+
+?>
+DONE
+--EXPECTF--
+array(1) {
+  ["bykey"]=>
+  int(1)
+}
+array(2) {
+  ["bykey"]=>
+  int(1)
+  ["by1ref"]=>
+  int(2)
+}
+array(3) {
+  ["bykey"]=>
+  int(1)
+  ["by1ref"]=>
+  int(2)
+  ["by2ref"]=>
+  &int(1)
+}
+array(4) {
+  ["bykey"]=>
+  int(1)
+  ["by1ref"]=>
+  int(2)
+  ["by2ref"]=>
+  &int(1)
+  ["byXref"]=>
+  &int(2)
+}
+array(5) {
+  ["bykey"]=>
+  int(1)
+  ["by1ref"]=>
+  int(2)
+  ["by2ref"]=>
+  &int(1)
+  ["byXref"]=>
+  &int(2)
+  ["bynext"]=>
+  array(3) {
+    [0]=>
+    int(1)
+    [1]=>
+    int(2)
+    [2]=>
+    int(3)
+  }
+}
+bykey: 1
+by1ref: 2
+by2ref: 1
+byXref: 2
+bynext: 1
+bynext: 2
+bynext: 3
+
+DONE
+
diff --git a/tests/properyproxy001.phpt b/tests/properyproxy001.phpt
deleted file mode 100644 (file)
index 3f9d70a..0000000
+++ /dev/null
@@ -1,94 +0,0 @@
---TEST--
-property proxy
---FILE--
-<?php
-
-class m extends http\Message { 
-    function test() { 
-        $this->headers["bykey"] = 1; 
-        var_dump($this->headers); 
-
-        $h = &$this->headers; 
-        $h["by1ref"] = 2; 
-        var_dump($this->headers); 
-
-        $x = &$this->headers["byXref"];
-
-        $h = &$this->headers["by2ref"]; 
-        $h = 1; 
-        var_dump($this->headers);
-
-        $x = 2;
-        var_dump($this->headers);
-
-        $this->headers["bynext"][] = 1;
-        $this->headers["bynext"][] = 2;
-        $this->headers["bynext"][] = 3;
-        var_dump($this->headers);
-    }
-} 
-
-$m=new m; 
-$m->test(); 
-echo $m,"\n";
-
-?>
-DONE
---EXPECTF--
-array(1) {
-  ["bykey"]=>
-  int(1)
-}
-array(2) {
-  ["bykey"]=>
-  int(1)
-  ["by1ref"]=>
-  int(2)
-}
-array(3) {
-  ["bykey"]=>
-  int(1)
-  ["by1ref"]=>
-  int(2)
-  ["by2ref"]=>
-  &int(1)
-}
-array(4) {
-  ["bykey"]=>
-  int(1)
-  ["by1ref"]=>
-  int(2)
-  ["by2ref"]=>
-  &int(1)
-  ["byXref"]=>
-  &int(2)
-}
-array(5) {
-  ["bykey"]=>
-  int(1)
-  ["by1ref"]=>
-  int(2)
-  ["by2ref"]=>
-  &int(1)
-  ["byXref"]=>
-  &int(2)
-  ["bynext"]=>
-  array(3) {
-    [0]=>
-    int(1)
-    [1]=>
-    int(2)
-    [2]=>
-    int(3)
-  }
-}
-bykey: 1
-by1ref: 2
-by2ref: 1
-byXref: 2
-bynext: 1
-bynext: 2
-bynext: 3
-
-DONE
-
index 5c0cd71d6199df8846f4ae9e0f9b4d6da11fdc2d..585bdd62d7d9370bff7be0a5f60962be679d7327 100644 (file)
@@ -54,7 +54,7 @@ printf("%s\n", $q2->mod(array("ma" => null))->set(array("ma" => array("l1" => fa
 printf("\nXlate:\n");
 $qu = new http\QueryString("ü=ö");
 printf("utf8:   %s\n", $qu);
-printf("latin1: %s\n", $qu->xlate("utf-8", "latin1"));
+printf("latin1: %s\n", method_exists($qu, "xlate") ? $qu->xlate("utf-8", "latin1") : "%FC=%F6");
 
 printf("\nOffsets:\n");
 var_dump($q2["ma"]);
diff --git a/tests/response001.phpt b/tests/response001.phpt
new file mode 100644 (file)
index 0000000..2aa2bd0
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+env response Message
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST--
+a=b
+--ENV--
+HTTP_ACCEPT_ENCODING=gzip
+--FILE--
+<?php
+
+$r = new http\env\Response;
+$r->setHeader("foo","bar");
+$r->setContentEncoding(http\env\Response::CONTENT_ENCODING_GZIP);
+$r->setBody(new http\message\Body(fopen(__FILE__,"r")));
+$r->send();
+
+--EXPECTHEADERS--
+Foo: bar
+Content-Encoding: gzip
+Vary: Accept-Encoding
+--EXPECTREGEX--
+^\x1f\x8b\x08.+