querystring test & fix
authorMichael Wallner <mike@php.net>
Wed, 25 Jan 2012 14:41:48 +0000 (14:41 +0000)
committerMichael Wallner <mike@php.net>
Wed, 25 Jan 2012 14:41:48 +0000 (14:41 +0000)
php_http_querystring.c
phpunit/QueryStringTest.php [new file with mode: 0644]
tests/phpunit.phpt

index ead598bbb9a4f19756906c06a6cb9ec6fa463c0c..7c569de763b31414157d01e6c23f27c362a29cda 100644 (file)
@@ -145,23 +145,22 @@ PHP_HTTP_API STATUS php_http_querystring_update(zval *qarray, zval *params, zval
                                                }
                                        }
                                } else {
+                                       zval *entry;
                                        /*
                                         * add
                                         */
                                        if (Z_TYPE_PP(params_entry) == IS_OBJECT) {
-                                               zval *new_array;
-
-                                               MAKE_STD_ZVAL(new_array);
-                                               array_init(new_array);
-                                               php_http_querystring_update(new_array, *params_entry, NULL TSRMLS_CC);
-                                               *params_entry = new_array;
+                                               MAKE_STD_ZVAL(entry);
+                                               array_init(entry);
+                                               php_http_querystring_update(entry, *params_entry, NULL TSRMLS_CC);
                                        } else {
                                                Z_ADDREF_PP(params_entry);
+                                               entry = *params_entry;
                                        }
                                        if (key.type == HASH_KEY_IS_STRING) {
-                                               add_assoc_zval_ex(qarray, key.str, key.len, *params_entry);
+                                               add_assoc_zval_ex(qarray, key.str, key.len, entry);
                                        } else {
-                                               add_index_zval(qarray, key.num, *params_entry);
+                                               add_index_zval(qarray, key.num, entry);
                                        }
                                }
                        }
diff --git a/phpunit/QueryStringTest.php b/phpunit/QueryStringTest.php
new file mode 100644 (file)
index 0000000..2fde23a
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+
+class QueryStringTest extends PHPUnit_Framework_TestCase {
+    protected $q;
+    protected $s = "a=b&r[]=0&r[]=1&r[]=2&rr[][]=00&rr[][]=10&1=2";
+    protected $e = "a=b&r%5B0%5D=0&r%5B1%5D=1&r%5B2%5D=2&rr%5B0%5D%5B0%5D=00&rr%5B1%5D%5B0%5D=10&1=2";
+
+    function setUp() {
+        $this->q = new http\QueryString($this->s);
+    }
+
+    function testSimple() {
+        $this->assertEquals($this->e, (string) $this->q);
+        $this->assertEquals($this->e, $this->q->get());
+    }
+
+    function testGetDefval() {
+        $this->assertEquals("nonexistant", $this->q->get("unknown", "s", "nonexistant"));
+        $this->assertEquals(null, $this->q->get("unknown"));
+    }
+
+    function testGetA() {
+        $this->assertEquals("b", $this->q->get("a"));
+        $this->assertEquals(0, $this->q->get("a", "i"));
+        $this->assertEquals(array("b"), $this->q->get("a", "a"));
+        $this->assertEquals((object)array("scalar" => "b"), $this->q->get("a", "o"));
+    }
+
+    function testGetR() {
+        $this->assertEquals(array(0,1,2), $this->q->get("r"));
+    }
+
+    function testGetRR() {
+        $this->assertEquals(array(array("00"),array("10")), $this->q->get("rr"));
+    }
+
+    function testGet1() {
+        $this->assertEquals(2, $this->q->get(1));
+        $this->assertEquals("2", $this->q->get(1, "s"));
+        $this->assertEquals(2.0, $this->q->get(1, "f"));
+        $this->assertTrue($this->q->get(1, "b"));
+    }
+
+    function testDelA() {
+        $this->assertEquals("b", $this->q->get("a", http\QueryString::TYPE_STRING, null, true));
+        $this->assertEquals(null, $this->q->get("a"));
+    }
+
+    function testDelAll() {
+        $this->q->set(array("a" => null, "r" => null, "rr" => null, 1 => null));
+        $this->assertEquals("", $this->q->toString());
+    }
+
+    function testQSO() {
+        $this->assertEquals($this->e, (string) new http\QueryString($this->q));
+        $this->assertEquals(http_build_query(array("e"=>$this->q->toArray())), (string) new http\QueryString(array("e" => $this->q)));
+    }
+
+    function testIterator() {
+        $this->assertEquals($this->q->toArray(), iterator_to_array($this->q));
+    }
+
+    function testSerialize() {
+        $this->assertEquals($this->e, (string) unserialize(serialize($this->q)));
+    }
+}
index 4289ffdfaf12c54053a97b4f8bed52ae9c901000..6320f61ba1811b90e110ea7dece31dcfdccfd01c 100644 (file)
@@ -12,7 +12,7 @@ require_once "PHPUnit/Autoload.php";
 --EXPECTF--
 PHPUnit %s by Sebastian Bergmann.
 
-%s
+%a
 
 Time: %s, Memory: %s